0

Is there any way to update ListAdapter items in a way that doesn't refresh the whole list? For now, if you call adapter.submitList(newITems) again, it will scroll the list to the top again, not just replacing current items with new ones.

Alireza Akbari
  • 2,153
  • 2
  • 28
  • 53
  • 1
    Clearly you have not implemented DiffUtil correctly, otherwise RecyclerView would have kept the topmost visible item with a matching ID visible. Include your class and your DiffUtil implementation. – ianhanniballake Jun 11 '22 at 05:57

3 Answers3

0

You can save the scroll position prior to calling submitList() and then restore it afterwards:

https://stackoverflow.com/a/32526482/7434090

Gavin Wright
  • 3,124
  • 3
  • 14
  • 35
0

Yes, It is possible to update Adapter items in a way that doesn't refresh the whole list. Suppose you created an adapter and the name of the adapter is "ListAdapter".If you wanna update the adapter items that means you wanna change the value of a certain position. Here you just pass the position.

listAdapter.notifyItemChanged(position:Int)

Maybe you inserted values in a position,

listAdapter.notifyItemInserted(position:Int)

or you may remove a value from a position,

listAdapter.notifyItemRemoved(position:Int)
Amit guha
  • 71
  • 2
0

As ianhanniballake mentioned, I was implementing the DiffUtil incorrectly. I was checking the whole new and old values in areItemsTheSame function, while should only check a unique field (like id).

    override fun areItemsTheSame(oldItem: FuelSubsidyEntity, newItem: FuelSubsidyEntity): Boolean {
        return oldItem.reportId == newItem.reportId
    }
Alireza Akbari
  • 2,153
  • 2
  • 28
  • 53