当我单击该图像视图时,我想将可绘制对象加载到该图像视图中
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);
}
}
}
答案 0 :(得分:2)
您可以在方法标题中声明所有者final
public void onBindViewHolder(final ProductViewHolder holder, int position) {
Your code here
}
答案 1 :(得分:2)
有几种解决方案:
1)让您的持有人做最后的决定
IF /I %OSV% LSS 3 (
IF "!M%MAP%%1:~0,1!" == "0" (call :checkplayer %2 %%G %X% || CALL :C 00 " ")
IF "!M%MAP%%1:~1,1!" == "1" (call :checkplayer %2 %%G %X% || CALL :C 2A "w")
IF "!M%MAP%%1:~2,1!" == "2" (call :checkplayer %2 %%G %X% || CALL :C 87 "S")
...
)
:checkplayer
IF "%1" == "1" IF "%2" == "%3" (
CALL :C 0C "i"
exit /b 0
) ELSE (
exit /b 1
)
或使用其他变量
@Override
public void onBindViewHolder(final ProductViewHolder holder, int position)
2)在特定视图上设置ClickListener时,请确保只有那个视图将触发操作,以便您可以使用该知识
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);
}
});