4

我想实现分页以在我的应用程序中显示我想要的项目视图的一大块列表。这就是为什么我选择使用 Google 新发布的分页库,即分页库 3。我在我的应用程序中使用 Rxjava、Livedata 和 ViewModel。实现分页库后,我面临一个奇怪的问题。当我调用获取列表的方法时,它一次又一次地调用并且没有停止调用。事实上,虽然我没有滚动列表,但它会自动增加页码。这是我尝试过的代码

JobListRestApi.kt

interface JobListRestApi {
    @GET("job/list")
    fun getJobResponse(
        @Query("page") pageNumber: Int
    ): Single<Response<JobResponse>>
}

JobListDataSource.kt

class JobListDataSource @Inject constructor(
    private val jobListRestApi: JobListRestApi
): RxPagingSource<Int, Job>() {

    override fun loadSingle(params: LoadParams<Int>): Single<LoadResult<Int, Job>> {
        val position = params.key ?: 1

        return jobListRestApi.getJobResponse(position).toSingle()
            .subscribeOn(Schedulers.io())
            .map { jobResponse -> jobResponse.jobData.jobs }
            .map { jobs -> toLoadResult(jobs, position) }
            .onErrorReturn { LoadResult.Error(it) }
    }

    private fun toLoadResult(data: ArrayList<Job>, position: Int): LoadResult<Int, Job> {
        val prevKey = if (position == 1) null else position-1
        val nextKey = if (position == data.size) null else position+1

        return LoadResult.Page(data, prevKey, nextKey)
    }

}

JobListRepositoryImpl.kt

class JobListRepositoryImpl @Inject constructor(
    private val jobListDataSource: JobListDataSource
): JobListRepository {

    override fun getJobs(): Flowable<PagingData<Job>> {
        return Pager(PagingConfig(pageSize = 20)) {
            jobListDataSource
        }.flowable
    }
}

JobListViewModel.kt

class JobListViewModel @Inject constructor(
    private val jobListRepository: JobListRepository
): BaseViewModel() {

    val jobs: MutableLiveData<PagingData<Job>> = MutableLiveData()

    fun getJobs() {
        if (jobs.value == null) {
            compositeDisposable += jobListRepository.getJobs()
                .subscribe({
                    jobs.value = it
                }, {
                    handleException(it)
                })
        }
    }

}

JobListFragment.kt

class JobListFragment : BaseFragment<JobListViewModel>() {

    private val jobAdapter: JobAdapter by lazy {
        JobAdapter { }
    }

    override fun getLayoutResource() = R.layout.fragment_job_list

    override fun initWidget() {
        job_recycler_view.adapter = jobAdapter
    }

    override fun onResume() {
        super.onResume()
        viewModel.getJobs()
    }

    override fun observeLiveData() {
        observe(viewModel.jobs) {
            jobAdapter.submitData(lifecycle, it)
        }
    }
}

并且输出日志是 https://base-url/job/list?page=1

https://base-url/job/list?page=2

https://base-url/job/list?page=3

https://base-url/job/list?page=4

https://base-url/job/list?page=5

https://base-url/job/list?page=6

https://base-url/job/list?page=7

除非我转到 RecyclerView 中块的最后一项并滚动列表,否则如何停止调用串行 api

4

2 回答 2

4

null您通过返回forprevKeynextKeyin来告诉 Paging 不再需要加载LoadResult.Page

由于它无限附加,看起来您从未设置nextKeynull. 也许你的意思是检查data.isEmpty()而不是key == data.size

于 2020-08-25T09:20:03.183 回答
2

最终,我得到了错误。事实上,问题出在我的 Http 响应数据中。更新后的 DataSource 如下所示

JobListDataSource.kt

class JobListDataSource @Inject constructor(
    private val jobListRestApi: JobListRestApi
): RxPagingSource<Int, Job>() {

    override fun loadSingle(params: LoadParams<Int>): Single<LoadResult<Int, Job>> {
        val position = params.key ?: 1

        return jobListRestApi.getJobResponse(position).toSingle()
            .subscribeOn(Schedulers.io())
            .map { jobResponse -> jobResponse.jobData }
            .map { jobData -> toLoadResult(jobData, position) }
            .onErrorReturn { LoadResult.Error(it) }
    }

    private fun toLoadResult(data: JobData, position: Int): LoadResult<Int, Job> {
        val prevKey = if (position == 1) null else position-1
        val nextKey = if (data.hasMore) position+1 else null

        return LoadResult.Page(data.jobs, prevKey, nextKey)
    }

}
于 2020-08-25T11:29:09.787 回答