DiffUtils.dispatchUpdatesTo(adapter) not updating the size of my array in recyclerView
my result has size more than current itemList, but displays only current size
How to display changes in size of my array by DiffUtils ? I think use notifyDatasetChanged() is not correct.
Here's how i do this now in adapter:
fun update(items: List<ITEM>) {
updateAdapterWithDiffResult(calculateDiff(items))
}
private fun updateAdapterWithDiffResult(result: DiffUtil.DiffResult) {
result.dispatchUpdatesTo(this)
}
private fun calculateDiff(newItems: List<ITEM>) =
DiffUtil.calculateDiff(DiffUtilCallback(itemList, newItems))
Your list size is not updating, because you are not adding new items in your itemList
update your code like this-
class YourAdapter(
private val itemList: MutableList<ITEM>
) : RecyclerView.Adapter<YourAdapter.ViewHolder>() {
.
.
.
fun updateList(newList: MutableList<ITEM>) {
val diffResult = DiffUtil.calculateDiff(
DiffUtilCallback(this.itemList, newList))
itemList.clear()
itemList.addAll(newList)
diffResult.dispatchUpdatesTo(this)
}
}
hope it will work for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With