Android 第二个页面立即加载,而其他页面不加载';一点也不装。第3页

Android 第二个页面立即加载,而其他页面不加载';一点也不装。第3页,android,android-paging-3,Android,Android Paging 3,我试图用Paging3库实现分页。为此,我在请求类中创建了假请求: fun getBerubyMerchantsByFilter(body: BerubyMerchantsByFilterRequest) = Single.just( BerubyMerchantsByFilterResponse( merchants = mutableListOf( BerubiExclusiveOfferResponse( (b

我试图用Paging3库实现分页。为此,我在
请求
类中创建了假请求:

fun getBerubyMerchantsByFilter(body: BerubyMerchantsByFilterRequest) = Single.just(
    BerubyMerchantsByFilterResponse(
        merchants = mutableListOf(
            BerubiExclusiveOfferResponse(
                (body.page ?: 0) * 11 + 1,
                "name",
                "category",
                1,
                "cashback",
                "https://www.lomsnesvet.ca/wp-content/uploads/sites/21/2019/08/Kitten-Blog-683x1024.jpg"
            ),
            BerubiExclusiveOfferResponse(
                (body.page ?: 0) * 11 + 2,
                "name",
                "category",
                1,
                "cashback",
                "https://www.lomsnesvet.ca/wp-content/uploads/sites/21/2019/08/Kitten-Blog-683x1024.jpg"
            ),
            BerubiExclusiveOfferResponse(
                (body.page ?: 0) * 11 + 3,
                "name",
                "category",
                1,
                "cashback",
                "https://www.lomsnesvet.ca/wp-content/uploads/sites/21/2019/08/Kitten-Blog-683x1024.jpg"
            ),
            BerubiExclusiveOfferResponse(
                (body.page ?: 0) * 5 + 4,
                "name",
                "category",
                1,
                "cashback",
                "https://www.lomsnesvet.ca/wp-content/uploads/sites/21/2019/08/Kitten-Blog-683x1024.jpg"
            ),
            BerubiExclusiveOfferResponse(
                (body.page ?: 0) * 11 + 5,
                "name",
                "category",
                1,
                "cashback",
                "https://www.lomsnesvet.ca/wp-content/uploads/sites/21/2019/08/Kitten-Blog-683x1024.jpg"
            ),
            BerubiExclusiveOfferResponse(
                (body.page ?: 0) * 11 + 6,
                "name",
                "category",
                1,
                "cashback",
                "https://www.lomsnesvet.ca/wp-content/uploads/sites/21/2019/08/Kitten-Blog-683x1024.jpg"
            ),
            BerubiExclusiveOfferResponse(
                (body.page ?: 0) * 11 + 7,
                "name",
                "category",
                1,
                "cashback",
                "https://www.lomsnesvet.ca/wp-content/uploads/sites/21/2019/08/Kitten-Blog-683x1024.jpg"
            ),
            BerubiExclusiveOfferResponse(
                (body.page ?: 0) * 11 + 8,
                "name",
                "category",
                1,
                "cashback",
                "https://www.lomsnesvet.ca/wp-content/uploads/sites/21/2019/08/Kitten-Blog-683x1024.jpg"
            ),
            BerubiExclusiveOfferResponse(
                (body.page ?: 0) * 11 + 9,
                "name",
                "category",
                1,
                "cashback",
                "https://www.lomsnesvet.ca/wp-content/uploads/sites/21/2019/08/Kitten-Blog-683x1024.jpg"
            ),
            BerubiExclusiveOfferResponse(
                (body.page ?: 0) * 11 + 10,
                "name",
                "category",
                1,
                "cashback",
                "https://www.lomsnesvet.ca/wp-content/uploads/sites/21/2019/08/Kitten-Blog-683x1024.jpg"
            )
        )
    )
)
传递给构造函数的第一个参数是id。因此,在本例中,我希望收到无限列表,因为每个请求将返回充满新元素的页面。然后我创建了
数据源

class BerubyMerchantsDataSource constructor(
    private val requests: Requests,
    private val requestBody: BerubyMerchantsByFilterRequest
) : RxPagingSource<Int, BerubiExclusiveOfferResponse>() {

    override fun getRefreshKey(state: PagingState<Int, BerubiExclusiveOfferResponse>) =
        state.anchorPosition?.let { anchorPosition ->
            val anchorPage = state.closestPageToPosition(anchorPosition)
            anchorPage?.prevKey?.plus(1) ?: anchorPage?.nextKey?.minus(1)
        }

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

        requestBody.page = page
        return requests.getBerubyMerchantsByFilter(requestBody)
            .subscribeOn(Schedulers.io())
            .map {
                toLoadResult(it, page, 10)
            }.onErrorReturn {
                LoadResult.Error(it)
            }
    }

    private fun toLoadResult(
        response: BerubyMerchantsByFilterResponse,
        currentPage: Int,
        pageSize: Int
    ): LoadResult<Int, BerubiExclusiveOfferResponse> {
        Timber.d("Loading page $currentPage")
        val nextKey = if ((response.merchants?.size ?: 0) < pageSize) null else currentPage + 1
        val prevKey = if (currentPage == 1) null else currentPage - 1
        return LoadResult.Page(
            response.merchants ?: listOf(),
            prevKey,
            nextKey
        )
    }

}
要在演示者中观察到,请执行以下操作:

@ExperimentalCoroutinesApi
override fun getMerchants() = Pager(PagingConfig(10)) {
    BerubyMerchantsDataSource(requests, requestModel)
}.observable
我还创建了适配器:

class BerubyMerchantsAdapter(diffCallback: DiffUtil.ItemCallback<BerubiExclusiveOfferResponse>) :
    PagingDataAdapter<BerubiExclusiveOfferResponse, BerubyMerchantsAdapter.ViewHolder>(diffCallback) {

    @SuppressLint("NonConstantResourceId")
    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {

        @BindView(R.id.iv_merchant_logo)
        internal lateinit var ivMerchantLogo: AppCompatImageView

        @BindView(R.id.tv_merchant_name)
        internal lateinit var tvMerchantName: AppCompatTextView

        @BindView(R.id.tv_merchant_category)
        internal lateinit var tvMerchantCategory: AppCompatTextView

        @BindView(R.id.tv_merchant_cashback)
        internal lateinit var tvMerchantCashback: AppCompatTextView

        init {
            ButterKnife.bind(this, itemView)
        }

        fun bindData(item: BerubiExclusiveOfferResponse) {
            Glide.with(itemView.context).load(item.logoUrl).into(ivMerchantLogo)
            tvMerchantName.text = item.name
            tvMerchantCategory.text = item.category
            tvMerchantCashback.text = item.cashback
        }
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        getItem(position)?.let(holder::bindData)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ViewHolder(
        LayoutInflater.from(parent.context).inflate(
            R.layout.beruby_merchant_item, parent, false
        )
    )
}
类BerubyMerchantsAdapter(diffCallback:DiffUtil.ItemCallback): PagingDataAdapter(diffCallback){ @SuppressLint(“非恒定源ID”) 内部类ViewHolder(视图:视图):RecyclerView.ViewHolder(视图){ @BindView(R.id.iv_商户_标志) 内部lateinit变量ivMerchantLogo:AppCompatImageView @BindView(R.id.tv\u商户名称) 内部lateinit变量tvMerchantName:AppCompatTextView @BindView(R.id.tv\商户\类别) 内部lateinit变量tvMerchantCategory:AppCompatTextView @BindView(R.id.tv\商户\现金返还) 内部lateinit变量tvMerchantCashback:AppCompatTextView 初始化{ ButterKnife.bind(此,itemView) } fun bindData(项目:BerubiExclusiveOfferResponse){ Glide.with(itemView.context).load(item.logoUrl).into(ivMerchantLogo) tvMerchantName.text=item.name tvMerchantCategory.text=item.category tvMerchantCashback.text=item.cashback } } 覆盖BindViewHolder(holder:ViewHolder,位置:Int){ getItem(位置)?.let(持有人::bindData) } override onCreateViewHolder(父级:ViewGroup,viewType:Int)=ViewHolder( LayoutFlater.from(parent.context).充气( R.layout.beruby\u商户\u项目,父项,错误 ) ) } 并通过调用
submitList()

但当我运行应用程序时,我看到前两个页面立即被加载,而当我滚动到底部时,没有加载新页面。那么,怎么了?我做错了什么?提前感谢您的帮助

class BerubyMerchantsAdapter(diffCallback: DiffUtil.ItemCallback<BerubiExclusiveOfferResponse>) :
    PagingDataAdapter<BerubiExclusiveOfferResponse, BerubyMerchantsAdapter.ViewHolder>(diffCallback) {

    @SuppressLint("NonConstantResourceId")
    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {

        @BindView(R.id.iv_merchant_logo)
        internal lateinit var ivMerchantLogo: AppCompatImageView

        @BindView(R.id.tv_merchant_name)
        internal lateinit var tvMerchantName: AppCompatTextView

        @BindView(R.id.tv_merchant_category)
        internal lateinit var tvMerchantCategory: AppCompatTextView

        @BindView(R.id.tv_merchant_cashback)
        internal lateinit var tvMerchantCashback: AppCompatTextView

        init {
            ButterKnife.bind(this, itemView)
        }

        fun bindData(item: BerubiExclusiveOfferResponse) {
            Glide.with(itemView.context).load(item.logoUrl).into(ivMerchantLogo)
            tvMerchantName.text = item.name
            tvMerchantCategory.text = item.category
            tvMerchantCashback.text = item.cashback
        }
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        getItem(position)?.let(holder::bindData)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ViewHolder(
        LayoutInflater.from(parent.context).inflate(
            R.layout.beruby_merchant_item, parent, false
        )
    )
}