所以問題是onClick只會被調用imageButton而不是while itemView。這裏是我的ViewHolder類Android RecycleView OnClick不適用於整個itemView
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
protected TextView title;
protected TextView rank;
protected ImageView image;
protected ImageButton share;
public ViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.main_title);
rank = (TextView) itemView.findViewById(R.id.rank_text);
image = (ImageView) itemView.findViewById(R.id.main_image);
share = (ImageButton) itemView.findViewById(R.id.main_share);
share.setOnClickListener(this);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Log.d("click", "clicked at" + getAdapterPosition());
}
}
UPDATE:
因爲我是用cardviews我recycleview,我最終改變我的viewholder代碼這個
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
protected TextView title;
protected TextView rank;
protected ImageView image;
protected ImageButton share;
protected CardView cardView;
public ViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.main_title);
rank = (TextView) itemView.findViewById(R.id.rank_text);
image = (ImageView) itemView.findViewById(R.id.main_image);
share = (ImageButton) itemView.findViewById(R.id.main_share);
cardView = (CardView) itemView.findViewById(R.id.main_card_view);
share.setOnClickListener(this);
cardView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Log.d("test", "test" + getAdapterPosition());
}
}
和它的作品
看看這個:http://stackoverflow.com/questions/24471109/recyclerview-onclick –
@Garr ett請按照上面的url –