将 Paging 3 alpha 更新为稳定导致索引问题 Android [英] Updating Paging 3 alpha to stable cause indexing issue Android

查看:24
本文介绍了将 Paging 3 alpha 更新为稳定导致索引问题 Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我正在使用 Paging 3 库和 ViewPager 2.它加载无限数据.

Hey I am using Paging 3 library with ViewPager 2. In which it loads unlimited data.

implementation "androidx.paging:paging-runtime-ktx:3.0.0-alpha07"

DataSource.kt

package com.example.viewpagerexample

import java.util.*

class DataSource(
    private val size: Int = 5,
    private val currentDate: Date,
    private val limitDate: Date?
) {

    fun returnData(pageNumber: Int): List<Date> {

        val dateList = mutableListOf<Date>()
        val startDateForPage = startDate(pageNumber)
        val tempCalendar = Calendar.getInstance()

        tempCalendar.time = startDateForPage
        val lastDateForPage = endDate(startDateForPage)

        while (tempCalendar.time < lastDateForPage) {
            if (limitDate == null ||
                tempCalendar.time.before(limitDate) ||
                tempCalendar.time == limitDate
            ) {
                dateList.add(tempCalendar.time)
                tempCalendar.add(Calendar.DATE, 1)
            } else {
                break
            }
        }
        return dateList
    }

    private fun startDate(pageNumber: Int): Date {
        if (pageNumber == 0) {
            return currentDate
        } else {
            Calendar.getInstance().let {
                it.time = currentDate
                it.add(Calendar.DATE, pageNumber * size)
                return it.time
            }
        }
    }

    private fun endDate(firstDateForPage: Date): Date {
        Calendar.getInstance().let {
            it.time = firstDateForPage
            it.add(Calendar.DATE, size)
            return it.time
        }
    }
}

ViewPagerPagingSource.kt

class ViewPagerPagingSource(
    private val dataSource: DataSource
) : PagingSource<Int, Date>() {

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Date> {
        val position = params.key ?: 0

        return try {
            val data = dataSource.returnData(position)
            LoadResult.Page(
                data = data,
                prevKey = if (data.isEmpty()) null else position - 1,
                nextKey = if (data.isEmpty()) null else position + 1,
                itemsBefore = LoadResult.Page.COUNT_UNDEFINED,
                itemsAfter = LoadResult.Page.COUNT_UNDEFINED
            )
        } catch (exception: IOException) {
            LoadResult.Error(exception)
        }
    }

}

所有代码工作正常.当应用程序启动时,它会加载当前日期页面.

All code working fine. when application starts it loads current date page.

现在当我将库更新到此版本时

Now when I am updating the library to this version

implementation "androidx.paging:paging-runtime-ktx:3.0.1"

它跳转到 -1 页面,我猜是这样的

It jumping to -1 page I guess and look like this

我不明白为什么会出现这个问题,我还编辑了 ViewPagerPagingSource 以实现另一种方法

I don't understand why this is causing the issue and also i edited ViewPagerPagingSource to implement another method

override fun getRefreshKey(state: PagingState<Int, Date>): Int? {
        return state.anchorPosition
}

我不明白更新库后是什么导致了问题.我正在添加我的 github 存储库 示例.请有人建议我代码出了什么问题?

I don't understand what is causing the issue after updating the library. I am adding my github repository Example. Please someone suggest me what is going wrong in code?

更新

我尝试学习分页概念并更新我的代码.另外,按照@dlam 的建议进行更改.

I tried to learn the paging concept and update my code. Also, do changes as per @dlam suggestion.

如果我将日期前几天作为当前日期,则再次跳转 1 页.Github

Again is jumping 1 page if I pass few days before date as current date. Github

2021-11-21 21:20:40.820 5460-5460/com.example.viewpagerexample E/Page -1: [06/11/2021, 07/11/2021, 08/11/2021, 09/11/2021, 10/11/2021]
2021-11-21 21:20:40.821 5460-5460/com.example.viewpagerexample E/Page 0: [11/11/2021, 12/11/2021, 13/11/2021, 14/11/2021, 15/11/2021]
2021-11-21 21:20:40.821 5460-5460/com.example.viewpagerexample E/Page 1: [16/11/2021, 17/11/2021, 18/11/2021, 19/11/2021, 20/11/2021]

推荐答案

我注意到你在这里提交了一个错误:https://issuetracker.google.com/204328119,但我也想为将来在 SO 上阅读此问题的其他人更新此答案.

I noticed you filed a bug here: https://issuetracker.google.com/204328119, but I also wanted to update this answer for other future people reading this issue on SO.

核心问题是您在 ViewPager 准备开始绑定这些项目之前就开始在 Paging 上收集.您应该使用 lifecycleScope.launchWhenCreated 而不是 lifecycleScope.launc 来解决这个问题:

The core issue is that you are starting collection on Paging before ViewPager is ready to start binding this items. You should use lifecycleScope.launchWhenCreated instead of lifecycleScope.launc to fix this:

lifecycleScope.launchWhenCreated {
  viewModel.dataList.collectLatest {
    adapter.submitData(it)
  }
}

我注意到的另一个问题(也可以解决此问题)是您启用了占位符,但没有在 中传入 itemsBeforeitemsAfterLoadResult.Page.启用占位符并具有静态计数也会为您的视图提供要绑定的列表大小,但是由于您传递了 COUNT_UNDEFINED,因此 Paging 无法使用 null 占位符正确填充列表因为它不知道要添加多少.

The other issue I noticed (which would also workaround this), is that you have placeholders enabled, but aren't passing in itemsBefore or itemsAfter in LoadResult.Page. Enabling placeholders and having a static count would also give your view a list size to bind, but since you pass COUNT_UNDEFINED, Paging is not able to pad the list with null placeholders properly since it has no idea how many to add.

这篇关于将 Paging 3 alpha 更新为稳定导致索引问题 Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆