RecyclerView onClick for multiple buttons and handling from Activity

后端 未结 5 683
深忆病人
深忆病人 2021-01-31 22:01

I\'m using RecyclerView with CardView and inside the CardView have 2 buttons. Now, have implemented the onClick events by imp

5条回答
  •  温柔的废话
    2021-01-31 23:03

    Try this

    In your activity do like this

    CustomItemClickListener listner;
    
    listner=YourActivityName.this   // in OnCreate Method
    

    and pass this listner in your adapter

    adapter = new ItemsListAdapter(getActivity(), data, listner);
    

    And in your Adapter use it like this

    CustomItemClickListener listner;
    
    this.listner=listner; // In your Adapter COnstructor
    

    and last in your onBindViewHolder do like this

    holder.button1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listner.onItemClick(holder.getLayoutPosition()); //getting position
                }
            });
    
    holder.button2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listner.onItemClick(holder.getLayoutPosition()); //getting position
                }
            });
    

提交回复
热议问题