kotlin 分页源始终从pageSize而不是1开始

g9icjywg  于 2022-12-27  发布在  Kotlin
关注(0)|答案(1)|浏览(133)

我需要在第一次请求时将起始距离设置为1,然后设置为101,再然后设置为201,以此类推,我的限制应始终为100,但我已尝试了PagingConfig中的所有方法,但无法获得此行为,下面是我获得的两个图像和以下代码

分页源

val page = params.key ?: 1
            val size = params.loadSize
            val from = page * size
            val data = homeRepository.getLatest(start = from, limit = size)
            val coinList = data.getOrNull()

视图模型

val coinList = Pager(
                pagingSourceFactory = { HomePagingSource(repo) },
                config = PagingConfig(pageSize = 100, prefetchDistance = 0, initialLoadSize = 100)
            ).flow.cachedIn(viewModelScope)
ctehm74n

ctehm74n1#

解决它的方法是

val coinList = Pager(
                pagingSourceFactory = { HomePagingSource(repo) },
                config = PagingConfig(
                    pageSize = NETWORK_PAGE_SIZE,
                    initialLoadSize = NETWORK_PAGE_SIZE,
                    prefetchDistance = 1
                )
            ).flow.cachedIn(viewModelScope)

以及

const val NETWORK_PAGE_SIZE = 100
private const val INITIAL_LOAD_SIZE = 1

                val position = params.key ?: INITIAL_LOAD_SIZE
                val offset = if (params.key != null) ((position - 1) * NETWORK_PAGE_SIZE) + 1 else INITIAL_LOAD_SIZE
                val data = homeRepository.getLatest(start = offset, limit = params.loadSize)
                val coinList = data.getOrNull()

相关问题