1

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

Mehmet Gür
  • 503
  • 7
  • 20

3 Answers3

2

@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.

Mehmet Gür
  • 503
  • 7
  • 20
  • 1
    Yes, it is correct. After this you can create an interface. You can listen your activity or fragment. I hope my project helps you. Good luck... – Kasım Özdemir Mar 28 '20 at 17:35
1

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();
    }
}
Kasım Özdemir
  • 5,414
  • 3
  • 18
  • 35
  • Thanks for answer. Could you explain what's the differnce this from mine ? So is this better for performance or more memmory-friendly ? – Mehmet Gür Mar 28 '20 at 10:33
  • Set an event from a viewer context is not a useful app. Don't miss that onBindViewHolder is called as your recyclerview is filled during scrolling. This way, you will make many calls to setOnClickListener. Again and again. – Kasım Özdemir Mar 28 '20 at 11:34
  • But I can't reach list from my ViewHolder. Because I have 2 ViewHolder(for different views) and they are outside of My RecyclerViewAdapter. I can only reach the list from Adapter so onBindViewHolder(). – Mehmet Gür Mar 28 '20 at 12:17
  • 1
    It doesn't matter. You can look my [adapter](https://github.com/kasimoz/Exchange_Android/blob/master/app/src/main/java/com/kasim/exchangeandroid/adapters/SettingsAdapter.kt). I have two view holders. Header and item. I added listener one of them. If I wanted I would add both. – Kasım Özdemir Mar 28 '20 at 12:56
  • I have an issue about nested classes. Some people say nested classes in RecyclerAdapter must be static to avoid OOM. But I couldn't find official doc. So I have looked your adapter code but it's Kotlin(i.e. your nested classes are already static automatically) – Mehmet Gür May 05 '20 at 03:33
  • so could you tell me should I use static for nested classes in Adapter to avoid memory leaks ? Thanks again. @Kasım Özdemir – Mehmet Gür May 05 '20 at 03:35
  • 1
    [resource1](https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html), [resource2](https://stackoverflow.com/a/31302613/8956604), [resource3](https://stackoverflow.com/a/10968689/8956604). Java proposes to use nested class. But there is nothing definite for the memory leak. Using static class has always remained a suggestion. – Kasım Özdemir May 05 '20 at 08:11
0

Make changes:

holder.button.layout....

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

Prajwal Waingankar
  • 2,534
  • 2
  • 13
  • 20