MVP 中的 Paging3 和 Rxjava 问题

问题描述 投票:0回答:1

在我的项目中我想使用分页3。

在将分页添加到我的项目中之前,我可以从服务器获取数据并显示到我的 RecyclerView 中 但添加分页后我遇到了这个问题:

在我的分页源课程中:

class RepoPagingSource @Inject constructor(
private val repository: ApiRepository,
val context: Context) : PagingSource<Int, RepositoryResponse>() {

private lateinit var sharedPref: SharedPref
private lateinit var data : MutableList<RepositoryResponse>
private lateinit var responseCode : String

override suspend fun load(params: LoadParams<Int>): LoadResult<Int, RepositoryResponse> {
    sharedPref = SharedPref(context)
    val responseData = mutableListOf<RepositoryResponse>()

    return try {
        val currentPage = params.key ?: 1
        val response = repository
            .getRepositories("bearer ${sharedPref.accessToken}", currentPage)
            .applyIoScheduler()
            .subscribe { response ->
                responseCode=response.code().toString()
                data = response.body()!!
                Log.d("RepoPagingSource",responseCode)
                Log.d("RepoPagingSource",data.size.toString())
                Log.d("RepoPagingSource",data.toString())
            }


        responseData.addAll(data)
        LoadResult.Page(
            data = responseData,
            prevKey = if (currentPage == 1) null else -1,
            nextKey = currentPage.plus(1)
        )


    } catch (e: Exception) {
        LoadResult.Error(e)
    }
}

override fun getRefreshKey(state: PagingState<Int, RepositoryResponse>): Int? {
    return null
}

}

这些日志显示了正确的数据:

Log.d("RepoPagingSource",responseCode)
Log.d("RepoPagingSource",data.size.toString())
Log.d("RepoPagingSource",data.toString())

这些日志的结果:

RepoPagingSource: 200
RepoPagingSource: 2
RepoPagingSource: [RepositoryResponse(id=5246349....

但是我的回收器视图是空的,我在调试模式下检查了代码 这里 : 响应数据.addAll(数据) 数据为空!

提前感谢您的帮助

android rx-java android-paging-3 android-paging-library
1个回答
0
投票

我已经这样做了:

class RepoPagingSource @Inject constructor(
private val repository: ApiRepository,
val context: Context ) : RxPagingSource<Int, RepositoryResponse>() {

private lateinit var sharedPref: SharedPref

override fun loadSingle(params: LoadParams<Int>): Single<LoadResult<Int, RepositoryResponse>> {
    sharedPref = SharedPref(context)

    var nextPageNumber = params.key
    if (nextPageNumber == null) {
        nextPageNumber = 1
    }

    return repository.getRepositories("bearer ${sharedPref.accessToken}", nextPageNumber)
        .subscribeOn(Schedulers.io())
        .map { response: Response<MutableList<RepositoryResponse>> -> response.body()?.let { toLoadResult(it, nextPageNumber) } }
        .onErrorReturn { LoadResult.Error(it) }
}

private fun toLoadResult(
    response: MutableList<RepositoryResponse>,
    position:Int
): LoadResult<Int, RepositoryResponse> {

    return LoadResult.Page(
        response,
        null,
        position + 1,
        COUNT_UNDEFINED,
        COUNT_UNDEFINED
    )

}

override fun getRefreshKey(state: PagingState<Int, RepositoryResponse>): Int? {
    return null
}}

在它为我工作的过程中,我也将我的 Rx 库版本更改为 rxjava2

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.