在ListAdapter上更新项目后,DiffUtil ItemCallback areContentsTheSame()始终返回true [英] DiffUtil ItemCallback areContentsTheSame() always returns true after updating an item on the ListAdapter

查看:942
本文介绍了在ListAdapter上更新项目后,DiffUtil ItemCallback areContentsTheSame()始终返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用ListAdapter时,我注意到在更新项目之后, DiffUtil.ItemCallback areContentsTheSame()方法始终返回true.调试代码后,我意识到旧项目和新项目完全相同(旧状态消失了).因此,没有调用 ListAdapter onBindViewHolder()方法来更新列表中的相应行(仅通过漂亮的动画更改了项目的顺序).

检查有关Stackoverflow的其他问题似乎是许多开发人员面临的常见问题:

ListAdapter不更新reyclerview中的项目

在编辑内容时ListAdapter不更新

DiffUtil.Callback无法正常工作

Recyclerview + Listadapter中的项目不会在更新时重新绘制

带有DiffUtil.ItemCallback的ListAdapter始终将对象视为相同

提交表单时到RecyclerView ListAdapter的新列表,差异检查始终为areContentsTheSame()返回true

但是以上所有答案(如果有)都没有提供正确的解决方案.

它对我有用的唯一方法是每次观察者发出新结果时都在ListAdapter上调用 notifyDataSetChanged().

但是,如果通过强制RecyclerView重绘其内容(它具有的所有项)而扔掉了所有可能获得的出色性能,那么使用ListAdapter和commitList()通知更改的全部意义是什么?显示到目前为止)?

  1. notifyDataSetChanged():

即使它可以工作,但是如果您决定首先使用ListAdapter,那么肯定不是正确的方法.

  1. viewModel.getObjects().observe(this, listOfObjects -> listAdapter.submitList(new ArrayList<>(listOfObjects)));:

没有用.

更新列表中的项目后,我希望看到UI上相应行的相应更改(不仅是排序顺序)按预期进行.

解决方案

我花了很多时间,直到发现自己遇到了与本帖所述相同的问题.我尝试了许多解决方案,但最终我意识到自己是在不知不觉中搞砸了.如果您遇到同样的问题,请检查是否没有先更新UI并调用write命令来更新存储库中的值. 就我而言,我正在更新LiveData对象,然后在Firebase数据库上更新该值.造成此问题的原因是,我的UI正在侦听该LiveData对象上的更改,因此我的Firebase数据库观察器检测到了更改,调用了ListAdapter并且看起来没有任何更改.

为解决该问题,我删除了用于更新LiveData对象的行,而仅调用Firebase数据库发送更改.

While using a ListAdapter I noticed that after updating an item the DiffUtil.ItemCallback areContentsTheSame() method was always returning true. Debugging the code I realized that the old and the new item were exactly the same (the old state disappeared). Therefore the ListAdapter onBindViewHolder() method wasn't being called to update the respective row on the list (only the order of the items changed with a nice animation).

Checking other questions on Stackoverflow looks like it's a common issue that many developers have faced:

ListAdapter not updating item in reyclerview

ListAdapter not updating when editing content

DiffUtil.Callback not working as expected

Items in Recyclerview + Listadapter won't redraw on update

ListAdapter with DiffUtil.ItemCallback always considers objects the same

When submitting a new list to RecyclerView ListAdapter the diff check always returns true for areContentsTheSame()

However none of the above answers (if any) have provided a correct solution.

The only way it worked for me was by calling notifyDataSetChanged() on the ListAdapter everytime the observer emitted a new result.

But what's the whole point of using a ListAdapter and submitList() to notify changes if all the great performance you could get is thrown away by forcing the RecyclerView to redraw its content (all the items it has showed so far)?

  1. notifyDataSetChanged():

Even though it works, certainly is not the proper approach to go with if you decided to use a ListAdapter in first place.

  1. viewModel.getObjects().observe(this, listOfObjects -> listAdapter.submitList(new ArrayList<>(listOfObjects)));:

Didn't work.

After updating a item in the list I would like to see the respective changes taking place on the UI (not only the sorting order) for the corresponding row as expected.

解决方案

I spent a lot of time until I found out that I was having the same issue as described on this post. I tried many solutions, but in the end I realised that I was messing it up without knowing. If you're having this same issue, check if you aren't updating the UI first and calling a write command to update the value on your repository. In my case, I was updating the LiveData object and then updating that value on Firebase Database. The issue was being caused because my UI was listening to changes on that LiveData object, so my Firebase Database observer detected changes, the ListAdapter was called and it looked like nothing has been changed.

To solve the problem, I dropped the lines that were updating the LiveData object and I only called Firebase Database sending the changes.

这篇关于在ListAdapter上更新项目后,DiffUtil ItemCallback areContentsTheSame()始终返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆