Handle click item in Recycleview

后端 未结 5 1810
不知归路
不知归路 2021-01-07 08:52

I have a horizontal recycleview inside vertical recycleview.

and i add list cardview inside horizontal rec

5条回答
  •  生来不讨喜
    2021-01-07 09:13

    First, you create an interface like :

    public interface OnItemClickListener{
        void onItemClick(View view, int position)
    }
    

    Declare on global :

    private OnItemClickListener mOnItemClickListener;
    

    Next, you need a contructor like :

    public MyAdapter(Context context, List data, OnItemClickListener mOnItemClickListener){
    //...
    this.mOnItemClickListener= mOnItemClickListener;
    

    }

    And in onBindViewHolder

    @Override
    public void onBindViewHolder(final FolderViewHolder holder, final int position) {
      //....
      holder.image.setOnClickListener(new View.OnItemClickListener){
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClick(v,position);    
            }
        });
    

    }

    Finally, you can implement Callback in class use adapter like :

    public class A extends AppCombatActivity implements MyAdapter.OnItemClickListener{
     //exist code
        @Override
        onItemClick(View view, int position){
         // Do something with view + position
      }
    
     }
    

提交回复
热议问题