How to get other views in same ViewHolder at same position as the onClicked view (use in a RecyclerView Adapter)

巧了我就是萌 提交于 2019-12-12 04:03:34

问题


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.


回答1:


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

        }
    });



回答2:


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



来源:https://stackoverflow.com/questions/30951551/how-to-get-other-views-in-same-viewholder-at-same-position-as-the-onclicked-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!