Toper
Toper

Reputation: 273

DiffUtils do not display new items

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))

Upvotes: 13

Views: 7349

Answers (1)

D_Alpha
D_Alpha

Reputation: 4069

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.

Upvotes: 21

Related Questions