ListAdapter with DiffUtil.ItemCallback always considers objects the same
LiveData returns the same instances in the List.
Solution I found - create new List with copy of Items:
val nameObserver = Observer<List<Item>> {
val newList = mutableListOf<Item>()
it.forEach { item -> newList.add(item.copy()) }
adapter.submitList(newList)
}
Since LiveData
returns the same List
you have to create a new one.
Here is a shorter answer to the original answer by using toList()
.
recycler.observe(this, Observer{
adapter.submitList(it.toList())
})
If you rather use a kotlin extension you can do something like this:
fun <T> MutableLiveData<List<T>>.add(item: T) {
val updatedItems = this.value?.toMutableList()
updatedItems?.add(item)
this.value = updatedItems
}
That way you don't have to add the toList()
and just use the extension.
Kotlin with Data Classes
adapter.submutList(list.map { it.copy() })