0

I have a RecyclerView in my app that displays a row containing pictures and photographs downloaded from the internet. There is a photo and a button on screen. When the user clicks a button, I want to make another view visible on top of the on screen photo.

I have a CustomViewHolder that holds all the views for the RecyclerView and a CustomRecyclerViewAdapter that displays the viewholder views. The onClick method for the button is overridden in the CustomViewHolder as below -

public static class CustomViewHolder extends RecyclerView.ViewHolder 
implements OnClickListener {

public Button mButton;
public ImageView mImageView1;
public ImageView mImageView2;

public CustomViewHolder(View v)
{
super(v);
mButton = (Button) ...           // Initializing views
mImageView1 = (ImageView) ...
mImageView2 = (ImageView) ...
}
@Override
    public void onClick(View v) {
        int id = v.getId();
        if(id == R.id.buttonid)
        {
             // TODO mImageView2.setVisibility(View.VISIBLE);
             // Any ideas on how to get the mImageView2 of this same item so it can be made visible?
        }
}

I need a way to get a reference of the mImageView2 view at the same position as the clicked button. Does anybody know how to do this? I'd really appreciate any help.

Advait Saravade
  • 3,029
  • 29
  • 34

2 Answers2

0

Try getAdapterPosition method of the ViewHolder. it will return the position of the Holder in the Recycler/Adapter

EE66
  • 4,601
  • 1
  • 17
  • 20
0

Another way is implement onBindViewHolder(ViewHolder holder, final int position) in your Adapter

This method already have position as a third argument.

LIKE

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

     holder.mImageView2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
M D
  • 47,665
  • 9
  • 93
  • 114
  • you CANNOT use the position in the callback because it may change. Instead, use ViewHolder#getAdapterPosition and also add the callback in onCreateViewHolder to avoid churning click listener objects. See the documentation for details: https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#onBindViewHolder(VH, int) – yigit Jun 21 '15 at 06:57
  • @yigit Ya i knew that one.. And i must tell you there are lots of ways to getting this. `If you have enough knowledge.` – M D Jun 21 '15 at 07:01
  • This is not advised. Read the first comment http://stackoverflow.com/a/29841832/4443323 – Tomasz Mularczyk Jun 22 '15 at 09:40