PagingSource LoadResult Page Paging中如何使用itemAfter或itemBefore 3

How to use itemAfter or itemBefore in PagingSource LoadResult Page Paging 3

嘿,我正在使用 Viewpager 2Paging 3 库 处理无限数据。在分页源的帮助下,我成功地创建了创建无限页面的逻辑。我在两个方向上通过按钮滑动单个页面。

要观看的视频 Link :- Youtube Video

Github 来源:- ViewPager Repository

我得到的问题

当我在上一个按钮上缓慢单击时,它会滑动单个页面,但如果我在上一个按钮上快速单击多次,它开始滚动的次数超过我们单击的次数,并且直到我们单击 viewpager 时才会停止。这是一种竞争条件。

我在问题跟踪器中询问过,他们建议我使用 itemBeforeitemAfter IssueTracker。我实现了这个,但是我没能取得好的结果。

MainActivity.kt

package com.example.viewpagerexample

import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.example.viewpagerexample.databinding.ActivityMainBinding
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {

    private val viewModel by viewModels<ViewPagerViewModel>()
    private lateinit var binding : ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val adapter = ViewPagerAdapter()
        lifecycleScope.launch {
            viewModel.dataList.collectLatest {
                adapter.submitData(it)
            }
        }
        binding.viewpager.adapter = adapter

        binding.next.setOnClickListener {
            binding.viewpager.setCurrentItem(binding.viewpager.currentItem.plus(1), true)
        }

        binding.previous.setOnClickListener {
            binding.viewpager.setCurrentItem(binding.viewpager.currentItem.minus(1), true)
        }
    }
}

ViewPagerDataSource.kt

package com.example.viewpagerexample

import android.util.Log
import java.util.*

class ViewPagerDataSource(
    private val pageSize: Int,
    private val currentDate: Date
) {
    fun loadDataSource(pageNumber: Int): List<Date> {
        val dates = mutableListOf<Date>()
        val startingDate = startingDate(pageNumber)
        val tempCalendar = Calendar.getInstance()
        tempCalendar.time = startingDate

        Log.e("loadData startingDate", "" + startingDate)

        var index = 0;
        while (index++ < pageSize) {
            dates.add(tempCalendar.time)
            tempCalendar.add(Calendar.DATE, 1)
        }
        return dates
    }

    private fun startingDate(pageNumber: Int): Date {
        Calendar.getInstance().let {
            it.time = currentDate
            it.add(Calendar.DATE, (pageNumber * pageSize) + pageSize)
            return it.time
        }
    }
}

ViewPagerPagingSource.kt

这是我的分页源,我不知道如何在我的情况下使用 itembeforeitemAfter。我也没有得到 getRefreshKey 这个有什么用?如果我通过 null 是否可以,或者我还需要通过什么。

package com.example.viewpagerexample

import androidx.paging.PagingSource
import androidx.paging.PagingState
import java.io.IOException
import java.util.*

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

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

        return try {
            val data = dataSource.loadDataSource(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)
        }
    }

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

ViewPagerViewModel.kt

package com.example.viewpagerexample

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.paging.Pager
import androidx.paging.PagingConfig
import java.util.*

class ViewPagerViewModel(app: Application) : AndroidViewModel(app) {

    private val dataSource = ViewPagerDataSource(10, currentDate())

    val dataList =
        Pager(config = PagingConfig(
            pageSize = 10,
            enablePlaceholders = true
        ), pagingSourceFactory = {
            ViewPagerPagingSource(dataSource)
        }).flow

    private fun currentDate(): Date {
        val calendar = Calendar.getInstance()
        return calendar.time
    }
}

适配器和支架已附在 Github Link 中,如果您需要它们,请查找。

build.Gradle

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.viewpagerexample"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        viewBinding true
    }
}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.6.0'
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.0'

    implementation "androidx.viewpager2:viewpager2:1.0.0"

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

    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.1"
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.3.1"
    implementation "android.arch.lifecycle:extensions:1.1.1"
    implementation 'androidx.fragment:fragment-ktx:1.3.6'

    //noinspection GradleDynamicVersion
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

发生这种情况是因为您使用的是平滑滚动的数据绑定: 平滑滚动需要时间来执行,当您尝试非常快速地单击时,绑定在滚动时进入下一页并开始无限滚动:

有两种解决方法:

1 - 使平滑滚动错误:

binding.viewpager.setCurrentItem(binding.viewpager.currentItem.plus(1), false)

2- 将绑定与滚动代码分开:

val nextPage = binding.viewpager.currentItem+1
binding.viewpager.setCurrentItem(nextPage, true)

我没有深入挖掘,我不知道为什么只有当您以编程方式滚动到上一页时才会出现此问题。据我所知,使用 itemBefore 和 itemAfter 的唯一原因是当你想在数据仍在加载时显示占位符并且你知道确切的数据长度。关于 getRefreshKey() 方法 - 这是我在官方文档中找到的 -

if items are loaded based on integer position keys, you can return state.anchorPosition

所以我无法解释为什么会出现这个问题,但我可以给你一段可能会解决它的代码:

 binding.previous.setOnClickListener {
            if (binding.viewpager.scrollState == SCROLL_STATE_IDLE) {
                binding.viewpager.setCurrentItem(binding.viewpager.currentItem.minus(1), true)
            }
        }

我查看了ViewPager2的源代码,发现关于滚动,分别有view.post()这样的调用,这是多线程的缘故。正如我所说,我没有深入研究问题,而是找到了一个常量,用于比较 ViewPager2 的当前滚动状态(SCROLL_STATE_IDLE - 表示 ViewPager2 处于空闲、稳定状态) .

编辑: 例如,要向前移动 10 天并向后无限移动,您应该这样做:

class ViewPagerViewModel(app: Application) : AndroidViewModel(app) {

    private val dataSource = ViewPagerDataSource(5, currentDate(), endDate())

    val dataList =
        Pager(config = PagingConfig(
            pageSize = 5,
            enablePlaceholders = false,
            jumpThreshold =5
        ), pagingSourceFactory = {
            ViewPagerPagingSource(dataSource)
        }).flow

    private fun currentDate(): Date {
        val calendar = Calendar.getInstance()
        return calendar.time
    }

    private fun endDate(): Date {
        val calendar = Calendar.getInstance()
        calendar.time = Date()
        calendar.add(Calendar.DATE, 10)
        return calendar.time
    }
}

然后-

class ViewPagerDataSource(
    private val pageSize: Int,
    private val currentDate: Date,
    private val endDate: Date
) {
    fun loadDataSource(pageNumber: Int): List<Date> {
        val dates = mutableListOf<Date>()
        val startingDate = startingDate(pageNumber)
        if (startingDate.after(endDate)){
            return dates
        }
        val tempCalendar = Calendar.getInstance()
        tempCalendar.time = startingDate

        Log.e("loadData startingDate", "" + startingDate)

        var index = 0;
        while (index++ < pageSize) {
            dates.add(tempCalendar.time)
            tempCalendar.add(Calendar.DATE, 1)
        }
        return dates
    }

    private fun startingDate(pageNumber: Int): Date {
        Calendar.getInstance().let {
            it.time = currentDate
            it.add(Calendar.DATE, (pageNumber * pageSize) + pageSize)
            return it.time
        }
    }
}