#The Problem In the way of replacing ListViews with RecyclerView I hit this obstacle where there is no onItemClickListener in RecyclerView ??? And when googled, I found almost all the posts saying we have to implement this using the GestureDetector. And some of them had used interfaces which is what I too thought of using in the first place. But even they were so much confusing and complex to understand.
#Solution
- Interfaces (Donot worry if you never used one. But they are very simple concept) - CustomItemClickListener.java
- ViewHolder Adapter - ItemsListAdapter.java
- ViewHolder Item Class - ItemsListSingleItem.java
- The RecyclerView Itself - ItemsList (See Setting up the RecyclerView)
Consider for example you have a list of thumbnails and a title to be listed. I would do the following to keep stuff simple and working. :)
Checkout the sample project here
This solution didn't work for me - every time my ClickListener returned index "-1".
The solution was change the place of implementing internal onClick method (in ItemViewHolder internal class, not in Adapter), like:
`public class MovieViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
// Log.d("ItemID", String.valueOf(this.getItemId()));
// Log.d("ItemLayoutPosition", String.valueOf(this.getLayoutPosition()));
// Log.d("ItemAdapterPosition", String.valueOf(this.getAdapterPosition()));
}