Answer the question
In order to leave comments, you need to log in
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
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)
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question