Home » Android » How to apply setItemChecked(position,true) with RecyclerView in Android?

How to apply setItemChecked(position,true) with RecyclerView in Android?

Posted by: admin June 16, 2020 Leave a comment


I am developing a Material Design Navigation Drawer. I’ve created a new class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener in it to handle the user’s click on the list items. I use the class this way within the MainActivity class’ onCreate method:

            new RecyclerItemClickListener(this, mRecyclerView, new RecyclerItemClickListener.OnItemClickListener() {
                public void onItemClick(View view, int position) {// do whatever
                        setItemChecked(position, true);
                        boolean isSelected = view.isSelected();

                public void onItemLongClick(View view, int position){
                    // ...

I based this code from this blog post: RecyclerView part 2 but it’s not getting the job done, and to me it’s not clear at all on how am I supposed to get it working.
I’ve also checked out this seemingly easy solution: Innodroid – Tracking Selected Item in RecyclerView (also quoted in this answer) but it’s not clear on how I am supposed to derive my MyAdapter class to the TrackSelectionAdapter class.

What’s the best way to highlight list items? I’m stuck.

Please help.

How to&Answers:

I think, I’ve found the best tutorial on how to use the RecyclerView with all functions we need and with no libraries (single+multiselection, hightlight, ripple, click and remove in multiselection, etc…). From a first look it seems well explained.

Here it is –> http://enoent.fr/blog/2015/01/18/recyclerview-basics/

[EDIT] I finally found the time to try it out and I even created my own more flexible, everybody can benefit my improvements: https://github.com/davideas/FlexibleAdapter.
In this link I also explain how it works. Please have a look and feel free to add it to your project.