I am facing an issue where a change in an item is not reflected when using submitList in ListAdapter.
I know that below code works in case I want to remove an item when using ListAdapter as inside submitList the framework checks whether lists are similar. It is also demonstrated in this sample from one of the developers working at Google https://github.com/android/views-widgets-samples/blob/main/RecyclerViewKotlin/app/src/main/java/com/example/recyclersample/data/DataSource.kt
fun removeItem(position: Int)
{
val copiedList = adapter.currentList.toMutableList()
copiedList.removeAt(position)
adapter.submitList(copiedList)
}
The problem is when you want to change something using same method it won't work because toMutableList() creates a shallow copy of the list.
and thus when I do this,
fun changeItemAtPositionWith(isEnabled: Boolean, position: Int) {
val copiedList = adapter.currentList.toMutableList()
copiedList[position].isEnabled = !copiedList[position].isEnabled
adapter.submitList(copiedList)
}
It doesn't work because content of both lists (copied and non-copied) refer to the same items and while comparing ListAdapter doesn't find any change.
I don't think creating deep copy for every change is a good idea at all.