How to implement different clicks for each item in Recyclerview?

Asked

Viewed 421 times

0

I’m developing an app, and it has recyclerView conjugated with CardView. And all the information is stored within the app itself to be presented on recyclerView. One part of the structure is this:

Album a = new Album("True Romance", 13, covers[0]);
        albumList.add(a);

        a = new Album("Xscpae", 8, covers[1]);
        albumList.add(a);

        a = new Album("Maroon 5", 11, covers[2]);
        albumList.add(a);

        a = new Album("Born to Die", 12, covers[3]);
        albumList.add(a);

        a = new Album("Honeymoon", 14, covers[4]);
        albumList.add(a);

        a = new Album("I Need a Doctor", 1, covers[5]);
        albumList.add(a);

        a = new Album("Loud", 11, covers[6]);
        albumList.add(a);

        a = new Album("Legend", 14, covers[7]);
        albumList.add(a);

        a = new Album("Hello", 11, covers[8]);
        albumList.add(a);

        a = new Album("Greatest Hits", 17, covers[9]);
        albumList.add(a);

        adapter.notifyDataSetChanged();
    }

And onClick comes right after:

recyclerView.addOnItemTouchListener(  
    new RecyclerItemClickListener(context, new RecyclerItemClickListener.OnItemClickListener() {
      @Override public void onItemClick(View view, int position) {
        // TODO Handle item click
      }
    })
); 

How do I adapt each item with a different click? For example:

Clicked on "True Romance" go to a new activity, "Xscpae" go to another activity...

What is the logic? I can do this?

  • What is the need to go to a different screen? Wouldn’t it be easier to have a single screen that receives the data and then there values the text and etc with the data? If the screen really needs to be different, you can still pass a parameter in onclick and go to a fixed screen and on that screen you read this parameter and gives a setContentView in a different layout for each different parameter. But I point out the first option.

  • Hello, thank you for answering. In this case, the basic information is displayed in this recyclerView. Really I need to send to a new screen, because there will be different interactions. Nothing fixed.

1 answer

1

One option is to have access to the clicked object, given its position in the method onItemClick.

You can create a method for this in Adapter that is associated with your Recyclerview, for example, assuming that in your Adapter you saved the list of albuns in a variable called albumList:

public Album getItem(int position) {
  return albumList.get(position);
}

So in your onItemClick method, just recover and implement the action in the best way for each album:

recyclerView.addOnItemTouchListener(  
    new RecyclerItemClickListener(context, new RecyclerItemClickListener.OnItemClickListener() {
      @Override public void onItemClick(View view, int position) {
        Album selectedAlbum = adapter.getItem(position);
        // Faca algo com o album
      }
    })
);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.