An Idiot’s guide to Android RecyclerView Part 2

sanjeev yadav
2 min readJun 21, 2017

--

Step by step tutorial on handling RecylerView item click listener using best practices. We will be continuing our code base from part 1. Go checkout :

We will display a Toast message whenever a item is clicked.

  1. Inside your Adapter class (MyListAdapter.java)
//TODO: (1) Declare an interface name ListItemClickListener 
//TODO: (2) Declare a method onListItemClick that take a integer in its parameter
//TODO: (3) Create a global varaible of type ListItemClickListener
//TODO: (4) Accept ListItemClickListener object in the constructor

2. Inside your RecyclerView.ViewHolder class (MyViewHolder.java)

//TODO: (1) Implement View.onClickListener to your class
//TODO: (2) Inside your constructor, register the view that you want to react to click by calling setOnClickListener and pass this in parameter
//TODO: (3) Override the onClick function that takes View as a parameter
//ToDO: (4) Inside onClick call itemClickListener.onListItemClick with correct position

3. Inside your MainActivity.java

//TODO: (1) Implement MyListAdapter.ListItemClickListener
//TODO: (2) Override onListItemClick function and define what should happen when it is clicked
//TODO: (3) Where you have declared your adapter send 'this' as an argument along with other parameters required

And you are done! Do like and share if you found this article helpful and there’s more article on the way. And if you have some request comment down below.

--

--

sanjeev yadav

Front end & Android Developer, in deep love with learning about CS and a travel enthusiast :) https://github.com/alexakasanjeev