android - 如何通过将键设置为字符串来使用recyclerview-selection?
问题描述
使用 android 库androidx.recyclerview.selection
,我尝试按照此处和此处RecyclerView
的教程实现多项选择。
但是,我希望我key
成为一个String
,而不是Long
,但我面临两个错误,如下所示:
tracker = SelectionTracker.Builder<String>(
"selection_id",
recyclerView,
StableIdKeyProvider(recyclerView), // this line shows error
MyItemDetailsLookup(recyclerView),
StorageStrategy.createStringStorage() // this line shows error
).withSelectionPredicate(
SelectionPredicates.createSelectAnything()
).build()
我想要一些关于如何ItemKeyProvider
为 a 实现的细节String
,其次,
StorageStrategy.createStringStorage() // this shows error
StorageStrategy.createLongStorage() // this doesn't show error
为什么会发生这种情况,当我到处都将泛型类型替换为Long
to 时String
?
解决方案
根据文档,StorageStrategy
用于存储已保存状态的密钥,
/* for Long keys */ StorageStrategy.createLongStorage()
/* for String keys */ StorageStrategy.createStringStorage()
/* for Parcelable keys */ StorageStrategy.createParcelableStorage(Class)
此外,根据docs,StableIdKeyProvider
提供了 type 的键Long
。这就是为什么您StorageStrategy
显示错误,因为它需要Long
密钥。
要提供String
密钥,您必须创建自己的ItemKeyProvider
类。有关 的更多详细信息ItemKeyProvider
,您可以在此处参考文档。
这是您可以为键实现ItemKeyProvider
类的方式:String
class MyItemKeyProvider(private val rvAdapter: MyAdapter): ItemKeyProvider<String>(SCOPE_CACHED) {
override fun getKey(position: Int): String = rvAdapter.getItem(position).myKey
override fun getPosition(key: String): Int = rvAdapter.getPosition(key)
}
并在MyAdapter
:
class MyAdapter(private val myList: ArrayList<MyModel>): RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
// functions used in MyItemKeyProvider
fun getItem(position: Int) = myList[position]
fun getPosition(key: String) = myList.indexOfFirst { it.myKey == key }
// other functions
}
MyModel
像这样的东西在哪里:
data class MyModel (
val myKey: String,
// other data
)
现在,您可以SelectionTracker
像这样简单地构建您的:
myTracker = SelectionTracker.Builder(
"my_selection_id",
recyclerView,
MyItemKeyProvider(rvAdapter),
MyItemDetailsLookup(recyclerView),
StorageStrategy.createStringStorage()
).withSelectionPredicate(
SelectionPredicates.createSelectAnything()
).build()
请注意,Adapter
如果您不使用,则不应在您的代码中编写以下代码StableIdKeyProvider
:
init { setHasStableIds(true) }
否则它将显示此错误:
Attempt to invoke virtual method 'boolean androidx.recyclerview.widget.RecyclerView$ViewHolder.shouldIgnore()' on a null object reference
本教程展示了如何recyclerview-selection
使用Long
键实现,还展示了如何实现您自己ItemKeyProvider
的键类Long
。
要recyclerview-selection
使用Parcelable
键实现,我在这里找到了示例代码。
推荐阅读
- jboss - ejb 2 FinderException 表或视图不存在
- java - 将现有库的升级版本发布到 maven Central 时,是否必须创建 Sonatype JIRA 票证?
- javascript - 单击轮播项目内的按钮如何转到下一个轮播项目
- ios - 如何获取我在 TableViewController 中单击的行(单元格)的索引
- amazon-s3 - 在 AWS S3 存储桶更改上触发 Spinnaker 管道
- python - 计算百分比差异 - python
- python - 多热标签编码
- mysql5 - MySQL - 排名前 5 的畅销计划或课程
- sql - 字符串到日期 SQL
- mysql - MySQL:无法使用 dba.configureLocalInstance() 为 InnoDB Cluster 设置 MySQL 服务器