如何更新PagedListAdapter中的单个项目 [英] How to update a single item in a PagedListAdapter

查看:391
本文介绍了如何更新PagedListAdapter中的单个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用ItemKeyedDataSource实现了新的PagedListAdapter.我的列表包含带有喜欢"按钮的供稿项目.单击赞"按钮应刷新该项目.

I've implemented the new PagedListAdapter with an ItemKeyedDataSource. My list contains feed items with a like button. A click on the like button should refresh the item.

要通过单击类似按钮刷新我的列表项,我更新了Firestore后端的状态,并在ItemKeyedDataSource上调用了invalidate.但这会刷新我的整个列表,同时又跳回到列表的顶部.

To refresh my list item on a like button click, i update the state in my Firestore back-end, and call invalidate on my ItemKeyedDataSource. But this will refresh my whole list while jumping back to the top of my list.

我不确定这是否是正常的行为,所以我去调试并检查了我的DiffUtil.ItemCallback是否是问题所在.但是,永远不会调用这两个方法(areItemsTheSame和areContentsTheSame).

I wasn't sure if this is normal behaviour, so i went debugging and checking maiby my DiffUtil.ItemCallback was the problem. But the two methods (areItemsTheSame and areContentsTheSame) are never being called.

那么在DataSource上调用invalidate是触发单个项目更新的方法吗? DiffUtil是否可以帮助我在更新后跟踪列表中的位置,还是我看错了方向?

So is calling invalidate on the DataSource the way to go to trigger an update of a single item? And will the DiffUtil help me keep track of my position in the list after an update, or am i looking in the wrong direction?

推荐答案

经过一番挖掘,我发现了以下解决方案:

After some digging around, i found the following solution:

要更新单个项目

您可以在适配器上调用notifyItemChanged.这将从数据源获取更新并更新列表项.

You can call notifyItemChanged on the adapter. This will fetch the update from the datasource and updates the list item.

adapter.notifyItemChanged(selectedItemPosition)

刷新所有数据

您可以在数据源上调用无效

You can call invalidate on the DataSource

feedDataSourceFactory.feedLiveData.value!!.invalidate()

这篇关于如何更新PagedListAdapter中的单个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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