RecyclerView onClick for multiple buttons and handling from Activity

后端 未结 5 688
深忆病人
深忆病人 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 22:44

    I do it this way.

    1. Set an OnClickListener on your Button in the ViewHolder

    2. Create a method in your activity with the button position as a parameter

    3. Pass the position of the clicked entry and do stuff

    ViewHolder

    yourButton.setOnClickListener(new View.OnClickListener()
    {
         @Override
         public void onClick(View v) 
         {
    
            int position = getAdapterPosition(); //Clicked entry in your List
    
            //Method from your activity
            yourActivity.methodOnBtnClick(position);
         }
    

    Activity

    public void methodOnBtnClick(int position)
    {
        yourList.remove(position);
        recycler.getAdapter().notifyDataSetChanged();
    }
    

提交回复
热议问题