【问题标题】:RecyclerView event when dataset is changed数据集更改时的 RecyclerView 事件
【发布时间】:2021-10-20 20:37:35
【问题描述】:

当recycler view数据改变时是否有可能处理事件?我正在更新应用程序的一部分中的列表,我打电话给notifyDataSetChanged。在应用程序的其他部分,我参考了回收站视图和适配器,我想处理此事件。

【问题讨论】:

    标签: java android kotlin android-recyclerview


    【解决方案1】:

    要获得这样的事件,您需要覆盖onCurrentListChanged(previousList, currentList),如下所示

    val myAdapter = object : ListAdapter<Type> {
     override fun onCurrentListChanged(previousList: MutableList<Type>, currentList: 
     MutableList<Type>) {
        ...
     }
    
      // other overrides
     }
     
    

    懒人

    private val myAdapter by lazy {
    object : MyAdapter() {
        override fun onCurrentListChanged(previousList: MutableList<Type>,  currentList: MutableList<Type>) {
            
        }
      }
    }
    

    【讨论】:

    • @Uknowledge_1 如果您能接受答案,那就太好了,因为它也会帮助社区中的其他人。谢谢
    【解决方案2】:

    每次 RV 的列表更改时都会调用一个 onCurrentListChanged(previousList, currentList) 回调。此回调是ListAdapter 的一部分。您可以将自己的回调传递给ListAdapter,然后在覆盖的onCurrentListChanged 中调用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      • 1970-01-01
      相关资源
      最近更新 更多