I have a RecyclerView adapter with DiffUtil functionality. I wanted to use DataObserver to notify fragment when DiffUtil finishes its magic. However, looks like DiffUtil doesn't call the observers.
Adapter
private val items: MutableList<Message>
private val updater = PublishSubject.create<MutableList<Message>>()
init {
items = ArrayList()
updater
.debounce(500, TimeUnit.MILLISECONDS)
.subscribeOn(Schedulers.computation())
.map { Pair(it, DiffUtil.calculateDiff(DiffUtilCallback(it, items), true)) }
.doOnNext { setItems(it.first) }
.map { it.second }
.observeOn(AndroidSchedulers.mainThread())
.subscribe { diffResult.dispatchUpdatesTo(this) }
}
fun updateData(msgs: MutableList<Message>) = updater.onNext(msgs)
private fun setItems(newItems: MutableList<Message>) {
items.clear()
items.addAll(newItems)
}
Fragment
adapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
override fun onChanged() {
//is not called
}
})
How do I make the AdapterDataObserver work? What is wrong with my (trying to be reactive) approach?
I found out the problem. I assumed onChanged() is called everytime something changes. But it is 1:1 with notify methods.
So notifyDatasetChanged() invokes onChanged(), but DiffUtil never calls it (that is the purpose of DiffUtil), which means all the other methods (onItemRangeRemoved(), onItemRangeMoved(), etc.) gotta be implemented.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With