如何在 Android 中使用 RecyclerView 应用 setItemChecked(position,true)? [英] How to apply setItemChecked(position,true) with RecyclerView in Android?
问题描述
我正在开发一个 Material Design Navigation Drawer.我创建了一个新类 RecyclerItemClickListener 在其中实现 RecyclerView.OnItemTouchListener
来处理用户对列表项的点击.我在 MainActivity
类的 onCreate
方法中以这种方式使用该类:
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){
// ...
}
})
);
我基于这篇博文中的代码:RecyclerView part 2 但它并没有完成工作,对我来说,我根本不清楚我应该如何让它工作.我还检查了这个看似简单的解决方案:Innodroid - Tracking Selected Item在 RecyclerView(也在这个 answer 中引用)但不清楚我应该如何导出我的 MyAdapter
类到 TrackSelectionAdapter
类.
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.
请帮忙.
推荐答案
我想,我找到了关于如何使用 RecyclerView 和我们需要的所有功能并且没有库(单选+多选、高亮、波纹)的最佳教程,单击并在多选中删除等...).乍一看似乎很好解释.
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.
这里是--> http://enoent.fr/blog/2015/01/18/recyclerview-basics/
我终于有时间尝试一下,我什至创建了自己的更灵活,每个人都可以受益于我的改进:https://github.com/davideas/FlexibleAdapter.在这个链接中,我还解释了它是如何工作的.请查看并随时将其添加到您的项目中.
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.
这篇关于如何在 Android 中使用 RecyclerView 应用 setItemChecked(position,true)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!