如何在单击项目后从RecyclerView的适配器内的drawable更改imageView的图像?

问题描述 投票:1回答:2
  1. 我从SERVER中获取数据以填充RECYCLER视图
  2. 我在每个recyclerview行都有一个imageview
  3. 当我点击该imageview时,我想将我的drawable加载到imageview中 holder.imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Drawable placeholder = holder.imageView.getContext().getResources().getDrawable(R.drawable.app); holder.imageView.setImageDrawable(placeholder); } });

以上就是我尝试但是它说“持有者”需要被宣布为最终,但实际上,我甚至没有提到它可以写“最终持有者”或其他任何东西。

我的适配器代码:

public class ProductsAdapter extends 
RecyclerView.Adapter<ProductsAdapter.ProductViewHolder> {


private Context mCtx;
private List<Product> productList;

public ProductsAdapter(Context mCtx, List<Product> productList) {
    this.mCtx = mCtx;
    this.productList = productList;
}

@Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(mCtx);
    View view = inflater.inflate(R.layout.product_list, null);
    return new ProductViewHolder(view);
}

@Override
public void onBindViewHolder(ProductViewHolder holder, int position) {
    final Product product = productList.get(position);

    //loading the image
    Glide.with(mCtx)
            .load(product.getImage())
            .into(holder.imageView);

    holder.textViewTitle.setText(product.getTitle());
    holder.textViewShortDesc.setText(product.getShortdesc());
    holder.textViewRating.setText(String.valueOf(product.getRating()));
    holder.textViewPrice.setText(String.valueOf(product.getPrice()));


    holder.imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            Drawable placeholder = holder.imageView.getContext().getResources().getDrawable(R.drawable.app);
            holder.imageView.setImageDrawable(placeholder);

        }
    });



        }

@Override
public int getItemCount() {
    return productList.size();
}

class ProductViewHolder extends RecyclerView.ViewHolder {

    TextView textViewTitle, textViewShortDesc, textViewRating, textViewPrice;
    ImageView imageView;

    public ProductViewHolder(View itemView) {
        super(itemView);

        textViewTitle = itemView.findViewById(R.id.textViewTitle);
        textViewShortDesc = itemView.findViewById(R.id.textViewShortDesc);
        textViewRating = itemView.findViewById(R.id.textViewRating);
        textViewPrice = itemView.findViewById(R.id.textViewPrice);
        imageView = itemView.findViewById(R.id.imageView);
    }
}
}
java android android-recyclerview
2个回答
2
投票

您可以在方法标题中声明持有者final

public void onBindViewHolder(final ProductViewHolder holder, int position) {
    Your code here
}

2
投票

有几种解决方案:

1)让你的持有人做最后的事情

@Override
public void onBindViewHolder(final ProductViewHolder holder, int position) 

或使用其他变量

final Holder finalHolder = holder;
holder.imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            Drawable placeholder = finalHolder.imageView.getContext().getResources().getDrawable(R.drawable.app);
            holder.imageView.setImageDrawable(placeholder);

        }
    });

2)在特定视图上设置ClickListener时,您确信只有该视图才会触发操作,因此您可以使用该知识

holder.imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            ImageView imgView = (ImageView) view;
            Drawable placeholder = imgView.getContext().getResources().getDrawable(R.drawable.app);
            imgView.setImageDrawable(placeholder);

        }
    });

推荐问答