Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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))
like image 680
Toper Avatar asked Jul 05 '18 10:07

Toper


1 Answers

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.

like image 175
D_Alpha Avatar answered Nov 09 '22 17:11

D_Alpha