Handling Item Clicks on RecyclerView

As you know RecyclerView doesn’t have item click listener. In this tutorial, we will learn how to handle item clicks on RecyclerView. We will keep going from previous tutorial – Making Album List 2. So if you don’t have source code of previous tutorial, download it from here. We will write all codes on it.

  • Lets do it. Remember that you need previous tutorial’s project. If you don’t have it, download from above link on introduction paragraph.

 

  • As always, first of all we define resources.
    Open your res/values/colors.xml and add below color items.

 

  • Open res/values/strings.xml and add below string items.

 

  • Items are highlighted on clicks. So we need to define that effects.
    Right click on res/drawable folder and create a new drawable resource. Name it as card_default_background. This drawable file represents states without any clicking or some action like that.  
    Screen Shot 2015-12-27 at 20.32.28
    Screen Shot 2015-12-27 at 20.34.17

 

  • Open res/drawable/card_default_background.xml and write below.

     
  • We defined a background of items for default state. Next is highlighted one. Again right click on res/drawable folder and create a new drawable resource. Name it as card_highlighted_background. This drawable file represents clicking states. Open res/drawable/card_highlighted_background.xml and write below.


     
  • Of course we need to declare which background will be active when clicking or not. Again right click on res/drawable folder and create a new drawable resource. Name it as selector_card. This drawable file chooses a background which is relevant state. Open res/drawable/selector_card.xml and write below.

     
  • We have to make the album card use backgrounds. Open res/values/styles.xml file and add below style item. (Don’t touch other style items)

     
  • Lets set our new style to album card. Open your res/layout/album_card.xml. Delete cardCornerRadius – cardElevation couple and set the style like below.

     
  • We are done with album card’s highlight effect. Lets begin our actual work. Creating custom item click listener for recyclerview.

 

  •  We will set an OnItemClickListener to recyclerview in MainActivity. But item views which will be affected from that listener are created by adapter. So we need communication between activity class and adapter class. Anytime you need this kind of communication, you create an interface class. So right click on your java package and create a new class. Name it as RecyclerViewItemClickListener
    Screen Shot 2015-12-27 at 21.09.12
    Screen Shot 2015-12-27 at 21.07.09

 

  • Open RecyclerViewItemClickListener and write below.

 

  • To send this interface object to adapter, we need a set method in adapter. So open RecyclerViewAdapter and add below signed rows.

 

  • Right now, adapter has necessary listener object but do not use it. Lets make it use. In ViewHolder class we can access the item view. We can set listener to item view there but we don’t have position (item’s index) data. As you know, when any item is clicked, we need to know which one. If we put an integer to represent position data in ViewHolder class, problem will be solved. In RecyclerViewAdapter add below signed rows.

 

  • Final touch is sending a proper listener object to adapter. Open MainActivity and add below signed rows.

 

download_source_icon Download Source Code
If you don’t know how to open an existing Android Studio Project, follow this tutorial.

Bir Cevap Yazın