在RecyclerView选择模式?模式、RecyclerView

2023-09-07 08:36:59 作者:病秧羔子

我试图找出如何达到同样的效果。

I'm trying to figure out how to achieve the same effect of

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操作指示选择(点击某一行?点击一个单选按钮的行吗?等等)。

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 处理,在与 SingleChoiceMode 类实现结合一个 ChoiceMode 策略。

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.

在作出选择,更新的新选择行以反映选择的和的更新previously选择的行以反映它不再是选择。 findViewHolderForPosition() RecyclerView 能有所帮助 - 如果你跟踪位置, findViewHolderForPosition()可以给你的 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.