Android 分页库 - PositionalDataSource 问题

Android Paging Library - PositionalDataSource issue

我可以从服务器获取记录总数,所以我使用 PositionalDataSource 进行网络调用。一切正常,但有一个问题:即使我不滚动,分页库也会继续从服务器获取所有数据。如何解决这个问题呢? 使用:androidx.paging:paging-runtime-ktx:2.1.1

示例:

override fun loadInitial(params: LoadInitialParams, callback: LoadInitialCallback<Doctor>) {
    fetchDataFromApi(params.requestedStartPosition, params.pageSize) { list, totalCount ->
        callback.onResult(list, 0, totalCount)
    }
}
override fun loadRange(params: LoadRangeParams, callback: LoadRangeCallback<Doctor>) {
    fetchDataFromApi(params.startPosition, params.loadSize) { list, totalCount ->
        callback.onResult(list)
    }
}

PagedList.Config.Builder()
        .setPageSize(14)
        .setPrefetchDistance(4)
        .setInitialLoadSizeHint(14)
        .setEnablePlaceholders(true)
        .build()

这听起来像是我过去两天研究的一个类似问题。结果对我来说,我把我的 RecyclerView 包裹在 NestedScrollView 里了。 但是,根据此 issue on Google tracker.

,分页库没有对此的内置支持

when you put recyclerview inside an infinite scrolling parent, it will layout all of its children because the parent provides infinite dimensions.

基本上 NestedScrollView 继续触发获取更多数据,就好像屏幕和完整列表一样长

如果您的情况也是如此,则有两种选择:

  1. 将布局设置更改为不需要 NestedScrollView

  2. 这里有一个 code example gist 帮助我创建了自定义 [SmartNestedScrollView]。请注意,您还必须在此自定义容器上设置 android:fillViewport="true" 并将 RecyclerView 更新为具有 wrap_content 的高度并从中删除 nestedScrollingEnabled 标志。