#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
good solution, but I'm not sure about how the position gets where the CustomItemClickListener passed out as an argument in HomeActivity.(the code snippet below)
can anyone leave an explanation?
adapter = new ItemsListAdapter(getActivity(), data, new CustomItemClickListener() { @Override public void onItemClick(View v, int position) { Log.d(TAG, "clicked position:" + position); long postId = data.get(position).getID(); // do what ever you want to do with it } }); itemsList.setAdapter(adapter); }