Sunday, 4 December 2016

Android How to Implement OnClickListener On RecyclerView

Recyclerview and Listview in two things that have several differences. One of these difference is How to implement onClickListener in both of them.


In Listview, we just do something like this :

It means, we just do something like code above on class that declare the listview. It's a little bit simple than Recyclerview.

How about on Recyclerview?

Actually there are many ways to implement onClickListener in RecyclerView. If you ask at Stackoverflow, they will give many answers to do this.

From this article i will give you one of many way how to implement onClickListener in RecyclerView.

See also : how to put different view/layout on recyclerview's item.

I do this on Adapter class. For simply, give an ID on RelativeLayout or LinearLayour in your Adapter's layout.

After that, define the LinearLayour or RelativeLayout in your Holder. Next, do the OnClick in onBindViewHolder method.

For simply, is using this project for example,  inside OnBindViewHolder method just do like below :


Done! Hope it helps, thank you.


EmoticonEmoticon