Home > Mobile >  ListAdapter, update items like notifyDataSetChange
ListAdapter, update items like notifyDataSetChange

Time:06-11

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.

CodePudding user response:

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

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

CodePudding user response:

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)

CodePudding user response:

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
    }
  • Related