In case,
When there is a list with 100 items and wanna change only some of them, should I need to create a new list for comparison with DiffUtil?
fun fetchDynamicItems() {
val items = repository.fetchOnlyDynamicItems()
replaceDynamicItems(items)
}
fun replaceDynamicItems(dynamicItems: List<DynamicItem>) {
val oldList = getCurrentList()
val newList = getCurrentList().map {
when (it) {
is DynamicItem -> dynamicItems.get(matchedIndex)
else -> it
}
}
// newList will be a copied list except DynamicItem
adapter.submitList(newList)
}
Is there a good way to use DiffUtil to change only a few items without creating a new List?
No, you have to create a new list. Otherwise, it can't compare old and new versions.
Note that generally, creating a new list is trivial. The items that are the same instance in both lists are only taking up one more word of memory each, since the Lists merely hold references to the same instances.