Android 分页库 - 更新当前页面上的数据

Android Paging Library - Updating the data on the current Page

我正在使用 Android Jetpack 的新 Paging 库来显示从 API 收到的项目列表。 我希望当前页面(比如第 3 页)上的数据每隔几分钟刷新一次,而不用使用 invalidate() 函数刷新整个列表,因为这会将 Recycler 视图带到它的第一页。

我在这里看到的问题是 Paging 库假设数据是不可变的。

https://developer.android.com/reference/android/arch/paging/DataSource#updating-paged-data

在谷歌搜索中,我可以看到解决方法是使用 Room 并根据来自 API 调用的数据库更新显示 UI。

是否有任何其他建议可以在不使用本地商店的情况下解决此问题。

解决这个问题的唯一方法是在 Room 中引入一个持久层。 https://github.com/googlesamples/android-architecture-components/tree/master/PagingWithNetworkSample#paging-with-database-and-network

根据此处的文档:https://developer.android.com/reference/androidx/paging/DataSource#updating-paged-data

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 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.

https://developer.android.com/topic/libraries/architecture/paging/data#consider-content-updates

As you construct observable PagedList objects, consider how content updates work. If you're loading data directly from a Room database updates get pushed to your app's UI automatically.