How to replace adapter list of data without refreshing the recyclerview

Viewed 21

i have recyclerView in my fragment and i want to change the list of data in the recyclerView adapter without refreshing the recyclerView i am using this two functions like that

mAdapter.clearList() mAdapter.addItems(newList)

but there is a quick refresh because of clearList() function anyone have better function to use

fun addItems(items: List<T>) {
        val myList = adapterItems()
        var count = myList.size
        // Remove loading indicator dummy item
        if (count > 0 && hasMore()) {
            count--
            myList.removeAt(count)
            notifyItemRemoved(count)
        }
        // Insert extra data
        myList.addAll(items)
        notifyItemRangeInserted(count, items.size)
    } 

 fun clearList() {
        val myList = adapterItems()
        val count = myList.size
        myList.clear()
        notifyItemRangeRemoved(0, count)
    }
1 Answers

You could make a setItems function that clears and then adds before notifying the RecyclerView with notifyDataSetChanged() - I'm not sure if that would matter though, I feel like it should all be resolved before you get a layout pass and the screen updates. A glitch you can see feels like one thing happens and then another later - I'm not sure how it's all handled internally though, if these things are queued up or not. Worth a try!

Another thing you could try is using notifyDataSetChanged() in clearList() instead of notifyItemRangeRemoved. Since you're throwing out everything it makes more sense to just use the "everything has changed" notify call. I'm not sure if using a rangeRemoved call followed by a rangeInserted one might cause problems - it shouldn't, but depending on how it's resolved, there could be some quirks.

The other thing to check is that you're definitely only updating your adapter once - if whatever's causing that update fires twice in a short space of time, you could see it glitch

Related