First of all, set onclickListener for each view you want to click on. A good place to do this is in the viewHolderConstructor. eg,
public class GalleryManyViewHolder extends RecyclerView.ViewHolder { @BindView(R.id.im_img) RoundedCornersImageView imImg; @BindView(R.id.tv_title) MyTextView tvTitle; @BindView(R.id.tv_label) MyTextView tvLabel; @BindView(R.id.tv_date) MyTextView tvDate; @BindView(R.id.im_gallery_one) RoundedCornersImageView imGalleryOne; @BindView(R.id.im_gallery_two) RoundedCornersImageView imGalleryTwo; @BindView(R.id.im_gallery_three) RoundedCornersImageView imGalleryThree; @BindView(R.id.im_gallery_four) RoundedCornersImageView imGalleryFour; @BindView(R.id.tv_more) MyTextView tvMore; @BindView(R.id.root) RelativeLayout root; public GalleryManyViewHolder(View view) { super(view); ButterKnife.bind(this, view); view.setOnClickListener(onClickListener); imGalleryOne.setOnClickListener(onClickListener); imGalleryTwo.setOnClickListener(onClickListener); imGalleryThree.setOnClickListener(onClickListener); imGalleryFour.setOnClickListener(onClickListener); view.setTag(this); }
As a rule, you donβt need to do anything specific with this view, for example, set tags (also some useful libraries, such as Glied, which set their own tags in images, will not work correctly if you set your own tag. clickListener find the position of the adapter view to be able to retrieve the corresponding data
View.OnClickListener onClickListener = new View.OnClickListener() { @Override public void onClick(View v) { View view = v; View parent = (View) v.getParent(); while (!(parent instanceof RecyclerView)){ view=parent; parent = (View) parent.getParent(); } int position = recyclerView.getChildAdapterPosition(view); }
as described here Then, but by checking the id, evaluate what you want to do
switch (v.getId()) { case R.id.im_gallery_one: { p = 0; } break; case R.id.im_gallery_two: { p = 1; } break; case R.id.im_gallery_three: { p = 2; } break; case R.id.im_gallery_four: { p = 3; } break; }
source share