我的适配器里有这个ViewHolder。
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView optionImage;
ImageView optionAddFavorites;
BMLTextView optionText;
RelativeLayout lay_icon;
ViewHolder(View itemView) {
super(itemView);
optionImage = (ImageView) itemView.findViewById(R.id.img_option);
optionAddFavorites = (ImageView) itemView.findViewById(R.id.iv_add_favorites);
optionText = (BMLTextView) itemView.findViewById(R.id.txt_option_description);
lay_icon = (RelativeLayout) itemView.findViewById(R.id.lay_icon);
itemView.setOnClickListener(this);
this.setIsRecyclable(false);
}
@Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}
后来,在我的活动中,我实现了:
public class OperateOptionSectionItemHelperImpl
implements OperateOptionRVAdapter.ItemClickListener, OperateOptionSectionItemHelper {
我已经完成了方法onItemClick的覆盖,如下所示:
@Override
public void onItemClick(View view, int position) {
if (isEditingFavorites) {
switch (view.getId()) {
case R.id.iv_add_favorites:
Log.e("onItemClick", "Add to favorites iv_add_favorites: " + R.id.iv_add_favorites + " parent: " + view.getId());
break;
case R.id.img_option:
Log.e("onItemClick", "Add to favorites img_option: " + R.id.img_option + " parent: " + view.getId());
break;
default:
Log.e("onItemClick", "Default");
}
} else {
Log.e("onItemClick", "Rest of the view");
}
}
}
我没有点击视图的 ID...我做错了什么?
这是因为您将 ClickListener 设置为 itemView。在脸上,您应该将"单击侦听器"设置为"选项图像"和"选项添加收藏夹" 改变
itemView.setOnClickListener(this);
自
optionImage.setOnClickListener(this);
optionAddFavorites.setOnClickListener(this);