【问题标题】:How to use recyclerview-selection by setting the key as a string?如何通过将键设置为字符串来使用recyclerview-selection?
【发布时间】:2019-06-23 19:00:28
【问题描述】:

使用android库androidx.recyclerview.selection,我尝试按照教程herehereRecyclerView中实现多选。

但是,我希望我的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()

我想要一些关于如何为String 实现ItemKeyProvider 的详细信息,其次,

StorageStrategy.createStringStorage() // this shows error
StorageStrategy.createLongStorage()   // this doesn't show error

当我到处都将泛型类型从 Long 替换为 String 时,为什么会发生这种情况?

【问题讨论】:

    标签: android kotlin android-recyclerview


    【解决方案1】:

    根据docsStorageStrategy用于存储keys的保存状态,

    /* for    Long    keys */    StorageStrategy.createLongStorage()
    /* for   String   keys */    StorageStrategy.createStringStorage()
    /* for Parcelable keys */    StorageStrategy.createParcelableStorage(Class)
    

    另外,根据docsStableIdKeyProvider 提供Long 类型的键。这就是为什么您的 StorageStrategy 显示错误,因为它需要 Long 键。

    要提供String 密钥,您必须创建自己的ItemKeyProvider 类。有关ItemKeyProvider的更多详细信息,您可以在此处参考docs

    这是您可以为String 键实现ItemKeyProvider 类的方法:

    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()
    

    请注意,如果您不使用StableIdKeyProvider,则不应在Adapter 中编写以下代码:

    init { setHasStableIds(true) }
    

    否则会显示这个错误:

    Attempt to invoke virtual method 'boolean androidx.recyclerview.widget.RecyclerView$ViewHolder.shouldIgnore()' on a null object reference
    

    这个tutorial 展示了如何使用Long 键实现recyclerview-selection,还展示了如何为Long 键实现您自己的ItemKeyProvider 类。

    要使用Parcelable 键实现recyclerview-selection,我找到了示例代码here

    【讨论】:

      猜你喜欢
      • 2023-03-17
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-13
      • 2017-05-07
      • 2022-01-06
      相关资源
      最近更新 更多