gpt4 book ai didi

Android Paging Library - 更新当前页面上的数据

转载 作者:行者123 更新时间:2023-12-03 23:13:25 26 4
gpt4 key购买 nike

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

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

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.

关于Android Paging Library - 更新当前页面上的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55110670/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com