我在项目中使用 Android Paging 3 库来逐页加载数据。在这个项目中,我没有使用数据库,这意味着我正在使用network only requests. 问题在于,它不是根据用户滚动加载页面,而是首先加载所有页面。这是我用于项目分页部分的代码:
我的 RxPagingSource 类:
class ReviewPagingSource constructor(
private val network: Network,
private val filter: ReviewFilter,
private val config: Config,
private val cacheFirstResponse: Boolean
) : RxPagingSource<Int, Review>() {
override fun loadSingle(params: LoadParams<Int>): Single<LoadResult<Int, Review>> {
return network.getReviews(
filter.copy(page = (params.key ?: 0) , pageSize = params.loadSize),
config,
cacheFirstResponse
)
.subscribeOn(Schedulers.io())
.map { toLoadResultPage(it, params) }
.onErrorResumeNext {
when (it) {
is TimeoutException,
is NoInternetException, is NetworkException, is UnexpectedResponseException -> Single.just(
LoadResult.Error(it) …Run Code Online (Sandbox Code Playgroud) android kotlin android-architecture-components android-paging-3