While using a ListAdapter I noticed that after updating an item the DiffUtil.ItemCallback areContentsTheSame() method was always returning true. Debugging the code I realized that the old and the new item were exactly the same (the old state disappeared). Therefore the ListAdapter onBindViewHolder() method wasn't being called to update the respective row on the list (only the order of the items changed with a nice animation).
Checking other questions on Stackoverflow looks like it's a common issue that many developers have faced:
ListAdapter not updating item in reyclerview
ListAdapter not updating when editing content
DiffUtil.Callback not working as expected
Items in Recyclerview + Listadapter won't redraw on update
ListAdapter with DiffUtil.ItemCallback always considers objects the same
However none of the above answers (if any) have provided a correct solution.
The only way it worked for me was by calling notifyDataSetChanged() on the ListAdapter everytime the observer emitted a new result.
But what's the whole point of using a ListAdapter and submitList() to notify changes if all the great performance you could get is thrown away by forcing the RecyclerView to redraw its content (all the items it has showed so far)?
notifyDataSetChanged()
:
Even though it works, certainly is not the proper approach to go with if you decided to use a ListAdapter in first place.
viewModel.getObjects().observe(this, listOfObjects -> listAdapter.submitList(new ArrayList<>(listOfObjects)));
:
Didn't work.
After updating a item in the list I would like to see the respective changes taking place on the UI (not only the sorting order) for the corresponding row as expected.