Android PagedList更新 [英] Android PagedList updates

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

问题描述

我的问题是如何更新PagedList中的项目?

My question is how to update item in PagedList?

就我而言,有ListActivity和DetailsActivity. 列表活动使用分页组件从网络获取帖子(仅),并使用分页适配器在回收器视图中显示它. 当用户按下某个帖子时,我需要获取帖子详细信息并将其显示在DetailsActivity上.我向服务器提出了另一个请求,它返回了我的帖子详细信息.在该调用之后,服务器增加了该帖子的viewsCount值,并且当用户返回到帖子列表时,我需要在列表项中更新该计数器.

In my case, there are ListActivity and DetailsActivity. List activity is using Paging component to get posts from network(only) and shows it in recycler view using paged adapter. When user is pressing on some post, I need to get post details and show it at the DetailsActivity. I am making another request to the server and it returns me post details. After that call, server increases viewsCount value of that post and when user returns to the posts list, I need to update that counter at the list item.

问题是,如何在该PagedList中更新单个项目(发布),因为我不需要从头开始重新加载所有列表,而只需更新一个单个项目.

The question is, how to update single item (post), in that PagedList, cause I don't need to reload all list from the beginning just to update one single item.

推荐答案

PagedList是不可变的

我建议您不要使用分页库,因为它假定您的数据是不可变的,这在实际用例中几乎是不可能的.

I would suggest you do not use the paging library because it assumes that your data is immutable which is almost impossible in a practical use-case.

您可以在官方文档中进行检查.它说:

You can check it in the official docs. It says:

如果您有更细致的更新信号,例如网络API发出对列表中单个项目的更新信号,则建议将数据从网络加载到内存中.然后通过包装内存中快照的数据源将该数据提供给PagedList.每次更改内存中的副本时,都将使先前的数据源无效,然后可以创建一个新的包装快照的新状态.

If you have more granular update signals, such as a network API signaling an update to a single item in the list, it's recommended to load data from the network into memory. Then present that data to the PagedList via a DataSource that wraps an in-memory snapshot. Each time the in-memory copy changes, invalidate the previous DataSource, and a new one wrapping the new state of the snapshot can be created.

您必须从服务器获取数据,然后使用会议室库将其存储到本地数据库中,然后每当需要更新任何项目时,将其更新到本地数据库中,然后 room 将这些更改反映到UI中(使用LiveData).

You have to fetch the data from the server and then store it into local DB using room library and then whenever you have to update any item, update the item into local DB and in turn room will reflect those changes into the UI(using LiveData).

但是,您必须使用本地DB进行分页.
如果有房间,您将拥有类似的东西.

But then you have to do the paging from using local DB.
In case of room, you'll have something like this.

@Dao
interface FeedDao {
@Query("SELECT * FROM feed ")
fun selectPaged(): DataSource.Factory<Int, Feed>
}

但是随后您必须处理更多类似的事情:

But then you have to take care of many more things like :

如果一个实体从远程服务器中删除,该怎么办.我们将如何注意到这一点?
我们还必须从本地数据库中删除它.

what if one entity gets deleted from the remote server. How are we going to notice that?
And we have to remove it from our local DB also.

您可以阅读文章来解决此问题.它说明了分页库中的所有问题以及如果您真的想使用分页库的可能的解决方法.

You can read this article to solve this problem. It explains all the problems in paging library and a possible workaround if you really want to use the paging library.

我的建议是使用库并实现您自己的分页.

My suggestion is to use this library and implement your own pagination.

这篇关于Android PagedList更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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