【问题标题】:Android Paging Library - Updating the data on the current PageAndroid Paging Library - 更新当前页面上的数据
【发布时间】:2019-08-02 06:53:45
【问题描述】:

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

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

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

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

有没有其他建议可以在不使用本地商店的情况下解决这个问题。

【问题讨论】:

    标签: android android-architecture-components android-jetpack android-paging android-paging-library


    【解决方案1】:

    摆脱这个问题的唯一方法是在 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

    如果您有更精细的更新信号,例如网络 API 表示更新列表中的单个项目,建议 将数据从网络加载到内存中。然后将该数据呈现给 PagedList 通过包装内存快照的 DataSource。每一次 内存中的副本更改,使先前的 DataSource 无效,并且 可以创建一个包装新状态的新快照。

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

    在构建可观察的 PagedList 对象时,请考虑内容如何 更新工作。如果您直接从 Room 数据库加载数据 更新会自动推送到您应用的 UI。

    【讨论】:

      猜你喜欢
      • 2021-02-19
      • 1970-01-01
      • 2018-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-01
      相关资源
      最近更新 更多