Mehmet Gür
Mehmet Gür

Reputation: 522

RecyclerView Child Item Click in onBindViewHolder()

I have a RecyclerView and a like button inside of it. For example I want to make a Toast message when I clicked the button. Sample code:

class ViewHolder extends RecyclerView.ViewHolder{

    Button button;

  public ViewHolder(@NonNull View itemView) {
    super(itemView);

    button = itemView.findViewById(R.id.button);

    }
}

 @Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {

     holder.button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Toast.makeText(context, ""+list.get(position).getId(), Toast.LENGTH_SHORT).show();
        }
    });

}

So is this way correct ? I'm not sure this must be inside of onBindViewHolder() because of performance issue? So can you tell me is there a better way for child item click event in RecyclerView? By the way these are only example codes. In real I'm dealing more complex things when I clicked the button(request to server).

Upvotes: 1

Views: 926

Answers (3)

Mehmet Gür
Mehmet Gür

Reputation: 522

@Kasim Özdemir now my code like this:

class ViewHolder extends RecyclerView.ViewHolder{

Button button;

public ViewHolderMultiple(@NonNull View itemView) {
  super(itemView);

    like_button = itemView.findViewById(R.id.like_button);
    comment_button = itemView.findViewById(R.id.comment_button);

     like_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                RecyclerViewClicksUtils.likeClick(context,list,getAdapterPosition());
            }
        });

      comment_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                RecyclerViewClicksUtils.likeClick(context,list,getAdapterPosition());
            }
        });
     }

      //I write other codes in RecyclerViewClicksUtils to avoid complexity in Adapter

 public static void likeClick(final Context context, final List<PostDetailsPojo> list, 
 final int position){

    Toast.makeText(context, ""+list.get(position).getId(), Toast.LENGTH_SHORT).show();
}

I hope this must be correct.

Upvotes: 2

Kasım &#214;zdemir
Kasım &#214;zdemir

Reputation: 5644

You can simply do like this:

class ViewHolder extends RecyclerView.ViewHolder{

Button button;

public ViewHolderMultiple(@NonNull View itemView) {
  super(itemView);

  button = itemView.findViewById(R.id.button);

  button.setOnClickListener(this);
}

@Override
public void onClick(View view) {
   Toast.makeText(context, ""+list.get(getAdapterPosition()).getId(), Toast.LENGTH_SHORT).show();
    }
}

Upvotes: 1

Prajwal Waingankar
Prajwal Waingankar

Reputation: 2718

Make changes:

holder.button.layout....

You need to get the instance of that holder and place it on your views for each button click.

Upvotes: 0

Related Questions