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

試著忘記壹切 提交于 2019-12-30 06:18:11

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/29695811/how-to-apply-setitemcheckedposition-true-with-recyclerview-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!