How To Handle Click Events In Android RecyclerViews

According to the documentation, a RecyclerView is a flexible view for providing a limited window into a large data set. If you have done any android development before, you definitely know the popular ListView class used to display lists of things. After its release by Google however, most of us have moved on and now use RecyclerViews. In this post, I want to show you how to handle click events like regular touch event and also long click in recyclerviews because you cannot just set OnClickListeners like you would for a ListView.


How I do RecyclerViews Click Events

In a given project, I am sure there will be more than one instances where I will need to use recyclerviews and so I will need to handle click events! So, to have a solution that can be reused, I normally create a class:

Now, we are ready to implement this code in our recyclerview reference; could be inside your fragment or activity! Here is how you do that:


So now that we have a reusable class, you can listen for clicks on those recyclerviews and respond appropriately since you have the position handed to you by the interface!

I hope you found this post helpful and please consider sharing and subscribing for future posts from yours truly!

Meanwhile, happy coding my people!

Be The First To Get New Posts From Simple Developer…

Written By Elisha Chirchir

Elisha Chirchir is a software developer. He is also the founder of Simple Developer and co-founder of Instinctive Software Solutions. On any given day, he works on both Android and Web Development. During his 'free time', he offers training to those interested in learning how to code in php, java, python, javaScript etc. You can easily find him on StackOverflow Android chatroom or on Twitter @Eenvincible

Leave a Reply

Your email address will not be published. Required fields are marked *