RecyclerView 中的选择模式? [英] Choice Mode in a RecyclerView?

查看:48
本文介绍了RecyclerView 中的选择模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究如何达到与

mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

在 RecyclerView 实现中.请帮忙.

in a RecyclerView implementation. Please help.

推荐答案

RecyclerView 没有对选择模式"结构的内置支持.您的选择是自己推出它或使用提供它的第三方库.DynamicRecyclerView 库 提供了选择模式,但我还没有尝试过.

There is no built-in support for a "choice mode" structure with RecyclerView. Your options are to either roll it yourself or use a third-party library that offers it. The DynamicRecyclerView library offers choice modes, but I have not tried it.

这个示例应用演示了自己实现它,在这种情况下使用激活状态指示哪个是当前选择.总体格局是:

This sample app demonstrates implementing it yourself, in this case using the activated state to indicate which is the current choice. The overall pattern is:

  • 让您的 RecyclerView.ViewHolder 检测指示选择的 UI 操作(单击行?单击行中的 RadioButton?等).

  • Have your RecyclerView.ViewHolder detect a UI operation that indicates a choice (click on a row? click on a RadioButton in the row? etc.).

在您的 RecyclerView.Adapter 级别跟踪选择.就我而言,ChoiceCapableAdapter 与实现 ChoiceMode 策略的 SingleChoiceMode 类一起处理该问题.

Keep track of the selection at the level of your RecyclerView.Adapter. In my case, a ChoiceCapableAdapter handles that, in conjunction with a SingleChoiceMode class that implements a ChoiceMode strategy.

当做出选择时,更新新选择的行以反映选择更新先前选择的行以反映它不再被选择.RecyclerView 上的 findViewHolderForPosition() 在这里可以提供帮助 -- 如果您跟踪最后一个选择的 positionfindViewHolderForPosition() 可以为您提供该选择的 ViewHolder,因此您可以取消选择"它.

When a choice is made, update the newly-chosen row to reflect the choice and update the previously-chosen row to reflect that it is no longer chosen. findViewHolderForPosition() on RecyclerView can help here -- if you track the position of the last choice, findViewHolderForPosition() can give you the ViewHolder for that choice, so you can "un-choose" it.

通过将选择置于管理 RecyclerView 的活动或片段的已保存实例状态,跟踪配置更改中的选择.

Keep track of the choice across configuration changes, by putting it in the saved instance state of the activity or fragment that is managing the RecyclerView.

这篇关于RecyclerView 中的选择模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆