How to get other views in same ViewHolder at same

2019-08-27 14:17发布

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.

2条回答
Lonely孤独者°
2楼-- · 2019-08-27 14:32

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) {

        }
    });
查看更多
对你真心纯属浪费
3楼-- · 2019-08-27 14:35

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

查看更多
登录 后发表回答