An elegant way to handle recycler-view item on-click in android

Devrath
2 min readMar 15, 2022

--

When we use recycler views in android, There are many ways we can handle the on-click of the item in the recycler view.

Some of the many ways are

  • Handling the onBindViewHolder method of the adapter and providing the navigation.
  • Have a listener set up in the adapter and handle the callback where the listener is implemented
  • Have it in a separate ViewHolderclass and handle it there.

After the usage of kotlin in android development, I have come across passing the action in the constructor so that we can handle the call in the view layer activity\fragment . This way is clean and the layers are decoupled from each other.

  • I have provided complete gist sample code of how I will be using it in the coming days in my projects. I have not explained the code in detail to keep the article simple.
  • I hope this snippet of code helps others to write clean android code

--

--