Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 835
Cris Avatar asked Apr 17 '15 09:04

Cris


1 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.

like image 60
Davideas Avatar answered Nov 04 '22 04:11

Davideas