在分页 3 android 中到达页面末尾时分页不起作用?

pagination not working when reaches to end of page in paging 3 android?

VideoStatusDataSource.kt

class VideoStatusDataSource(
        private val categoryKey: String,
        private val videosStatusApi: VideoStatusApiService
    ) : PagingSource<Int, VideoStatus>() {
    
    
        companion object {
            private const val VIDEO_STARTING_PAGE_INDEX = 0
        }
    
        override suspend fun load(params: LoadParams<Int>): LoadResult<Int, VideoStatus> {
            return try {
    
                val pageIndex = params.key ?: VIDEO_STARTING_PAGE_INDEX
                logger(params.key)
                logger(pageIndex)
                val response =
                    videosStatusApi.getVideoStatusByPageNumberAndCategoryName(pageIndex, categoryKey)
    
                val jsonCategoryResponse = response.getAsJsonArray(DATA_KEY)
                val videoStatusList: List<VideoStatus> = Gson().fromJson(jsonCategoryResponse)
    
                LoadResult.Page(
                    data = videoStatusList.orEmpty(),
                    prevKey = if (pageIndex == VIDEO_STARTING_PAGE_INDEX) null else pageIndex - 1,
                    nextKey = if (videoStatusList.isEmpty()) null else pageIndex.plus(1)
                )
    
            } catch (exception: IOException) {
                LoadResult.Error(exception)
            } catch (exception: HttpException) {
                LoadResult.Error(exception)
            } catch (exception: Exception) {
                LoadResult.Error(exception)
            }
        }
    
        override fun getRefreshKey(state: PagingState<Int, VideoStatus>): Int? {
            return state.anchorPosition?.let { anchorPosition ->
                val anchorPage = state.closestPageToPosition(anchorPosition)
                anchorPage?.prevKey?.plus(1) ?: anchorPage?.nextKey?.minus(1)
            }
        }

}

video-API 服务在每个页面上提供 10 个结果,但使用此数据源 class 它会立即加载所有数据 我只想最初加载前 10 个项目,然后使用滚动前 10 个项目来加载接下来的 10 个项目 这是我的分页数据存储库

MainRepopsitory.kt

fun getVideoStatusPagingData(categoryKey: String): Pager<Int, VideoStatus> =
    Pager(
        config = PagingConfig(
            pageSize = 10
        ),
        pagingSourceFactory = { VideoStatusDataSource(categoryKey, videosStatusApi) }
    )

视图模型

@HiltViewModel
class PagingViewModel @Inject constructor(
    private val mainRepository: MainRepository,
    @IoDispatcher private val ioDispatcher: CoroutineDispatcher
) : ViewModel() {


    fun getCurrentCategoryVideoStatus(categoryKey: String): Flow<PagingData<VideoStatus>> =
        mainRepository
            .getVideoStatusPagingData(categoryKey)
            .flow.cachedIn(viewModelScope)
            .flowOn(ioDispatcher)


}

这就是我在分页源中使用加载函数的方式 你可以从中获得帮助 我的应用程序中有五到六个分页源,并且都具有与此相同的实现

override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Message> {
return try {
            val nextPage = params.key ?: 0
            chatWithSellerRequest.offset = nextPage.times(PAGE_SIZE_LIMIT)
            val response = apiService.getSellerChatResponse(chatWithSellerRequest)
            _chatWithSellerResultResponse.value = response.chatWithSellerResult

            LoadResult.Page(
                data = response.chatWithSellerResult?.messages!!,
                prevKey = null,
                nextKey = if (response.chatWithSellerResult.messages.isEmpty()) null else nextPage + 1
            )
        } catch (e: Exception) {
            LoadResult.Error(e)
        }