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!

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

0 Comments on “How To Handle Click Events In Android RecyclerViews

  1. Ronald Reply

    October 5, 2016 at 6:37

    Thanks man. Your code rally helped a great loads.

  2. almukathar Reply

    November 9, 2016 at 5:35

    how can i remove an onclicklistener from a recyclerview assigned as explained above

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.