Home > Back-end >  RecyclerView event when dataset is changed
RecyclerView event when dataset is changed

Time:10-21

Is there any possibility to handle event when recycler view data is changed? I am updating list in one part of the app and I call notifyDataSetChanged. In other part of the app where I have reference to the recycler view and adapter I want to handle this event.

CodePudding user response:

There is an onCurrentListChanged(previousList, currentList) callback that's invoked each time the RV's list changes. This callback is part of the ListAdapter. You can pass your own callback to the ListAdapter and then invoke it in overriden onCurrentListChanged.

CodePudding user response:

To get such an event you need to override the onCurrentListChanged(previousList, currentList) as below

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

  // other overrides
 }
 

By Lazy

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