【发布时间】:2021-10-20 20:37:35
【问题描述】:
当recycler view数据改变时是否有可能处理事件?我正在更新应用程序的一部分中的列表,我打电话给notifyDataSetChanged。在应用程序的其他部分,我参考了回收站视图和适配器,我想处理此事件。
【问题讨论】:
标签: java android kotlin android-recyclerview
当recycler view数据改变时是否有可能处理事件?我正在更新应用程序的一部分中的列表,我打电话给notifyDataSetChanged。在应用程序的其他部分,我参考了回收站视图和适配器,我想处理此事件。
【问题讨论】:
标签: java android kotlin android-recyclerview
要获得这样的事件,您需要覆盖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>) {
}
}
}
【讨论】:
每次 RV 的列表更改时都会调用一个 onCurrentListChanged(previousList, currentList) 回调。此回调是ListAdapter 的一部分。您可以将自己的回调传递给ListAdapter,然后在覆盖的onCurrentListChanged 中调用它。
【讨论】: