如何更新分页列表中的特定项目

时间:2020-08-20 16:33:16

标签: android android-paging

我正在使用Android的分页库从Web服务器加载数据并将其显示在列表中。

面对“添加到收藏夹”功能的问题。为此,我必须向我的Web服务器发送一个拉取请求,之后我必须更新页面列表中的该特定项目,以显示UI的差异以表示该项目已添加到“收藏夹”中。

我尝试了dataSource.invalidate()方法,但是它刷新了整个列表。我无法直接在列表中更新此特定项目,因为分页库无法直接访问列表。

如何刷新页面列表中的一项?

class AdsDataSource(
        private val token: String?,
        private val categoryUrl: String?,
        private val subcategoryUrl: String?,
        private val fullPath: String?,
        private val lang: String,
        private val apiV2: APIV2,
        private val compositeDisposable: CompositeDisposable
) : PageKeyedDataSource<Int, AdsListParent>() {
private val pLoadAdsStatus = MutableLiveData<ELiveDataStatuses>(ELiveDataStatuses.IDLE)
    val loadAdsStatus: LiveData<ELiveDataStatuses> = pLoadAdsStatus

    private val pLoadAdsError = MutableLiveData<Throwable?>(null)
    val loadAdsError: LiveData<Throwable?> = pLoadAdsError

    private var retryCompletable: Completable? = null

override fun loadInitial(params: LoadInitialParams<Int>, callback: LoadInitialCallback<Int, AdsListParent>) {
        pLoadAdsStatus.postValue(ELiveDataStatuses.WAITING)

        val adsRequest = apiV2.getAdsBySubcategory(categoryUrl, subcategoryUrl, token, INITIAL_PAGE, PAGE_SIZE, lang)
        compositeDisposable.add(
                adsRequest
                        .subscribeOn(Schedulers.io())
                        .map { adsFromSubcategory ->
                            adsFromSubcategory
                                    .mapToAdsListParent()
                                    .toMutableList()
                                    .apply {
                                        if (size >= AD_LIST_MIN_SIZE)
                                            add(AD_LIST_MIN_SIZE, AdsListParent.AdsListAd(adsFromSubcategory.admixerAndroid))
                                    }
                                    .toList()
                        }
                        .subscribe({
                            retryCompletable = null

                            pLoadAdsStatus.postValue(ELiveDataStatuses.DONE)

                            pLoadAdsError.postValue(null)

                            callback.onResult(it, null, INITIAL_PAGE.inc())
                        }, {
                            pLoadAdsStatus.postValue(ELiveDataStatuses.ERROR)

                            pLoadAdsError.postValue(it)

                            retryCompletable = Completable.fromAction {
                                loadInitial(params, callback)
                            }
                        })
        )
    }

    override fun loadAfter(params: LoadParams<Int>, callback: LoadCallback<Int, AdsListParent>) {
        pLoadAdsStatus.postValue(ELiveDataStatuses.WAITING)
        val adsRequest = apiV2.getAdsBySubcategory(categoryUrl, subcategoryUrl, token, params.key, PAGE_SIZE, lang)
        compositeDisposable.add(
                adsRequest
                        .subscribeOn(Schedulers.io())
                        .subscribe({
                            retryCompletable = null

                            pLoadAdsStatus.postValue(ELiveDataStatuses.DONE)

                            pLoadAdsError.postValue(null)

                            callback.onResult(it, params.key.inc())
                        }, {
                            pLoadAdsStatus.postValue(ELiveDataStatuses.ERROR)

                            pLoadAdsError.postValue(it)

                            retryCompletable = Completable.fromAction {
                                loadAfter(params, callback)
                            }
                        })
        )
    }
}

0 个答案:

没有答案