(recyclerview-selection)在运行时更改SelectionTracker

时间:2019-04-09 11:26:03

标签: android android-recyclerview android-support-library androidx

我必须通过单击按钮来更改SelectionPredicates.createSelectSingleAnything()SelectionPredicates.createSelectSingleAnything()

enter image description here

当我在SelectionPredicates上更改Runtime时显示此错误

首先在onCreate()中创建SelectionBuilder对象

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ...
         selectionBuilder = SelectionTracker.Builder<Long>(
            "mySelection",
            recyclerView,
            StableIdKeyProvider(recyclerView),
            MyItemDetailsLookup(recyclerView),
            StorageStrategy.createLongStorage()
        )
        tracker = customerBuild.build()
        adapter.tracker = tracker
        ...
 } 

selectoinBuilder中,我没有在此处输入任何关于SelectionPredicate的字样,因为当用户选择其中一个按钮时,它将配置为打开。

 single.setOnClickListener {
            tracker?.clearSelection()
            tracker = selectionBuilder.withSelectionPredicate(
                SelectionPredicates.createSelectSingleAnything()
            ).build()
            adapter.tracker = tracker
        }



multi.setOnClickListener {
                tracker?.clearSelection()
                tracker =selectionBuilder.withSelectionPredicate(
                    SelectionPredicates.createSelectAnything()
                ).build()
                adapter.tracker = tracker
        }
tracker?.addObserver(object : SelectionTracker.SelectionObserver<Long>() {
            override fun onItemStateChanged(key: Long, selected: Boolean) {
                super.onItemStateChanged(key, selected)
                Log.i("Hello", "changes is index $key, and value is $selected")
            }
        })

您可以在GitHub上看到。谢谢!

1 个答案:

答案 0 :(得分:1)

在selectionBuilder的StableIdKeyProvider中,将参数从recyclerView更改为adapter,因为keyProviders是选择键的源,也就是适配器。