我可以从服务器获取记录总数,所以我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()