I'm currently using the DiffUtil class to calculate and dispatch updates to my adapter using a ListUpdateCallback
as follows:
inner class NotificationCallback(private val onInsert: () -> Unit) : ListUpdateCallback {
override fun onInserted(position: Int, count: Int) {
notifyItemRangeInserted(position, count)
Log.d("NotificationCallback", "onInserted position: $position count: $count")
onInsert()
}
override fun onChanged(position: Int, count: Int, payload: Any?) {
notifyItemRangeChanged(position, count)
Log.d("NotificationCallback", "onChanged position: $position count: $count")
}
override fun onMoved(fromPosition: Int, toPosition: Int) {
notifyItemMoved(fromPosition, toPosition)
Log.d("NotificationCallback", "onMoved from: $fromPosition to: $toPosition")
}
override fun onRemoved(position: Int, count: Int) {
notifyItemRangeRemoved(position, count)
Log.d("NotificationCallback", "onRemoved position: $position count: $count")
}
}
As you can see from this code, I am updating the adapter with the relevant methods, but I want run some code only when something is inserted. This does work, however, because other notification methods can (and are) called after the insert code, this causes some problems with the code I am running when something is inserted. I ideally need to know when all updates have been dispatched to the adapter so that I can then run the relevant code if required. Is there such a method or way to know this?
The solution I have currently is to use postDelayed
to execute this code, and hope the adapter updates have completed by the time it runs. Not pretty, or ideal!