D
D
Dima Dema2021-05-04 20:01:16
Java
Dima Dema, 2021-05-04 20:01:16

Why doesn't DiffUtils update items position in adapter after remove item (JAVA)?

When i remove item in my list and use diffUtils for update my adapter, my adapter doesn't change position for items which are behind him in adapter. How can i do that?

for (AppsItem appsItem : appsItems) {
      if (appsItem.isChecked()) {
                        appsItems.remove(appsItem);
                        break;
                    }
                }

                AppsDiffUtil appsDiffUtil =
                        new AppsDiffUtil(appsAdapter.getAppsList(), appsItems);
                DiffUtil.DiffResult appsDiffResult = DiffUtil.calculateDiff(appsDiffUtil,true);
                appsAdapter.setAppsList(appsItems);
                appsDiffResult.dispatchUpdatesTo(appsAdapter);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
foonfyrick, 2021-05-06
@DemaDima

I don’t see your code that clears the list, you need to transfer only updated list elements, those that have changed, and you transfer everything over again.
I have code like this encapsulated in an adapter:

//при инициализации, создаю список и добавляю
fun insertData(list:ArrayList<Content>){
        val dfc = MyDiffUril(listOfContent,list)
        val difResult = DiffUtil.calculateDiff(dfc)
        listOfContent.addAll(list)
        difResult.dispatchUpdatesTo(this)
    }
//по нажатию на вью холдер обновляю список
    fun updateData(list:ArrayList<Content>){
        val dfc = MyDiffUril(listOfContent,list)
        val difResult = DiffUtil.calculateDiff(dfc)
        listOfContent.clear()
        listOfContent.addAll(list)
        difResult.dispatchUpdatesTo(this)
    }

listOfContent - a list in the recycler adapter
list - a list that I manually fill in the main activity
the first argument of MyDiffUtil is a list in the recycler adapter, aka the "old list", the second argument is a new list, this is the list I created and filled in the main activity .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question