Different Methods of attaching click listeners in recycler view
Hey there, today I’ll be sharing my views on the different methods of handling click events for an item in the recycler view.
If you are getting started with native android development. Check this series of articles written by me.
This article contains a list of methods I have come across and used to handle click events in recycler views.
The methods are different in terms of ease of implementation, reusability of code, etc.
About the project —
- The project uses ViewBinding in place of findViewById. More Details
- The aim of this project is to display a list of users.
- Each user object contains the name and age of the user.
- 3 separate adapter classes have been created in order to show the implementation of the 3 methods.
- Functions to initiate and set all 3 adapters have been written in the MainActivity.kt. Uncomment one and comment on others to attach the adapter to the recycler view.
Method 1: Handling click events inside the adapter class
Adapter’s Link in the source code
Key points :
- This implementation doesn't require any lambda function or an interface to handle the click event.
root.setOnClickListener {
itemClickListener(userItem)
}
- In this case, we handle the click listener’s logic inside the adapter itself.
- To figure out the action that happens on click of an item in recycler view a person has to go through the whole source. (here it’s a small piece of code, but in a big project an adapter contains many more lines of code).
Pros of this approach :
- Removes the dependencies on a lambda function or interface compared to other methods.
- The adapter’s constructor doesn’t require any more extra parameters to be passed.
Con’s of this approach :
- To figure out the handling of click events, one has to go through the adapter’s source code.
- In many cases, one has to parse global variables to the next screen (in case navigation is supposed to happen). Here, the variable has been passed on the adapter as a parameter to the constructor.
- In my opinion, the Recycler view’s adapter already handles a lot of complex logic (in the case of rendering multiple types of views) and hence it shouldn’t contain more complex logic for handling events.
- This approach also reduces code reusability, as for each new adapter class, the same piece of code has to be written again and again.
Screenshot of the implementation and Logcat on click —
Method 2: Handling click events with a lambda function
Adapter’s Link in the source code
Key points :
- This implementation requires a lambda function to be passed in the adapter constructor’s adapter.
- In this case, we handle the click listener’s logic inside the activity/fragment where the adapter object is declared.
- The lambda’s function can be used to return any data type depending on the requirement.
Pros of this approach :
- Reduces the business logic inside the adapter class and makes the click event more readable.
- The function return type can easily be changed depending on the use case.
- The business logic for the click event is handled inside the activity/fragment hence making it easier to use and handle events.
Con’s of this approach :
- In my opinion, the click listener can be used with different views and the returned value can be used to check which view was clicked, hence handling multi-view item clicks inside a recycler view item can become easier.
- Decrease code reusability, as for each adapter the function has to be passed separately.
Screenshot of the implementation and Logcat on click —
Method 3: Handling click events using an interface
Adapter’s Link in the source code
Key points :
- This method handles click events for an item using an interface.
/**
* This interface contains the function which is called when
* an item is clicked in the recyclerview
* @since Only works in method 3
*/
interface ItemClickListener {
fun getItemClicked(userItem: UserItem)
}
- In this case, also we can handle the click event’s logic inside the fragment or activity.
- The activity/fragment needs to implement the interface function.
Pros of this approach :
- The click logic and its related actions are handled inside the activity/fragment itself.
class MainActivity : AppCompatActivity(), ItemClickListener {// also contains other necessary functions/**
* This is the implementation for the
* click method in the interface
*
* Handle the action that needs to be done when item is clicked
*/
override fun getItemClicked(userItem: UserItem) {
// replace this log by your use case
Log.d(
TAG_FOR_CLICK,
"Clicked using interface -> user name " + userItem.name + " age " + userItem.age
)
}
}
- Rather than telling the adapter’s object what needs to be done on each click, the same implemented function from the activity/fragment can be reused.
val adapter = AdapterWithInterfaceClickListener(listOfDemoData, this)
- Increase code readability and reusability.
- Multiple functions can be added to the interface to handle click events for different views in the recycler view’s item (eg — Button, ImageView, TextView).
Con’s of this approach :
- The activity/fragment needs to implement the interface (screenshot attached below).
- The implemented interface function needs to pass in the fragment as a constructor argument.
class AdapterWithInterfaceClickListener(
private val listOfUsers: List<UserItem>,
private val getItemClickedListener : ItemClickListener
)
The snippet of the implementation and Logcat on click —
In my opinion, the 3rd method i.e. handling the click event by using the interface is the best method out there. It reduces code redundancy and also ensures code reusability.
These were my views on different methods of handling click events for an item in the recycler view. Feel free to drop your opinions/ suggestions.