A
A
alelam2021-12-18 11:37:40
Android
alelam, 2021-12-18 11:37:40

Magic RV, ListAdapter and DiffUtil?

Hello!
I did pagination up/down according to the standard template - RV, ListAdapter, DiffUtli(itemsTheSame + contentTheSame) with the addition of my ItemLoader to the adapter when approaching the top/bottom edge of the list and subsequent request to the repository.

The data that comes to the adapter is a sorted list of 50 objects, in which a maximum of the 20 first or last changes with each update. No more than 7-8 pieces are visible on the screen. Those. it is guaranteed that each time the list is updated, the currently visible items are present in the new list, have not changed, and have retained their relative position to each other.

When scrolling down, the loader was added like this:

val cachedList = currentData.toMutableList()
            cachedList.add(item)
            updateData(cachedList.toList())

when scrolling up like this:
val cachedList = currentData.toMutableList()
            cachedList.add(0, item)
            updateData(cachedList.toList())

updateData() is just:
open fun updateData(data: List<AdapterItem>) {
        currentData = data
        submitList(data)
    }


Everything worked smoothly except for one moment - when scrolling up quickly (i.e. a case in which the data did not have time to update seamlessly and the user had time to see the loader), the adapter lost the current item and jumped to the beginning of the new data. The poke method solved it as follows:
val cachedList = currentData.toMutableList()
            cachedList[0] = item
            updateData(cachedList.toList())


I tried to dive into DiffUtil and sort of subtracted that due to adding itemLoader to the beginning of the intermediate list when scrolling up, all indexes in it went and DiffUtils called notifyItemRangeChanged when it received updated data with redrawing everything and everything. But I can’t understand why if the loader is not added at all, without touching the old sheet before the update, everything starts to go again, although there is no internal logic associated with the presence of the loader item in the list in RV.

Sorry if it's confusing, but how could I :)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question