虽然使用ListAdapter,但我注意到在更新项目之后,DiffUtil.ItemCallback areContentsTheSame()方法始终返回true。调试代码后,我意识到旧项目和新项目完全相同(旧状态消失了)。因此,没有调用ListAdapter onBindViewHolder()方法来更新列表中的相应行(仅通过漂亮的动画更改了项目的顺序)。
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
但是以上答案(如果有)均未提供正确的解决方案。
它对我有用的唯一方法是每次观察者发出新结果时,都在ListAdapter上调用notifyDataSetChanged()。
但是,如果通过强制RecyclerView重绘其内容(到目前为止显示的所有项目)而扔掉了您可以获得的所有出色性能,那么使用ListAdapter和SubmitList()通知更改的全部意义是什么? )?
notifyDataSetChanged()
:即使它可以工作,但是如果您决定首先使用ListAdapter,肯定不是正确的方法。
viewModel.getObjects().observe(this, listOfObjects -> listAdapter.submitList(new ArrayList<>(listOfObjects)));
:没用。
更新列表中的项目后,我希望看到相应行在UI上发生的相应更改(不仅仅是排序顺序)。