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

1

8

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:

mRecyclerView.addOnItemTouchListener(
            new RecyclerItemClickListener(this, mRecyclerView, new RecyclerItemClickListener.OnItemClickListener() {
                @Override
                public void onItemClick(View view, int position) {// do whatever
                    if(position!=0){
                        setItemChecked(position, true);
                        setSelectable(true);
                        boolean isSelected = view.isSelected();
                        view.setActivated(isSelected);

                        selectItem(position);
                    }
                }
                @Override
                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.

Arnone answered 17/4, 2015 at 9:46 Comment(0)
R
8

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.

Rockies answered 29/4, 2015 at 17:45 Comment(1)
Thank you so much for this tutorial, it helped me a lot! I've implemented the MyAdapter class and then extended the CickListener interface with the 2 methods onItemClick and onItemLongClick and worked like a charm!Arnone

© 2022 - 2024 — McMap. All rights reserved.