我正在使用下面的分页源实现从网络中获取数据,并通过 collectAsLazyPagingItems()
在可组合项中观察它。但是,当在此 LazyPagingItems
上调用 refresh()
方法时,它不会从 page 0
获取数据,而是从最后获取的页码获取数据,这导致没有数据显示。这里出了什么问题?会不会是因为 val page = params.key ?: 0
?
class CommentDataSource(
private val postId: Long,
private val commentApi: CommentApi
) : PagingSource<Int, Comment>() {
override fun getRefreshKey(state: PagingState<Int, Comment>): Int? {
return state.anchorPosition
}
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Comment> {
return try {
val page = params.key ?: 0
val responseData = commentApi.getPostComments(
postId = postId,
page = page
).data
LoadResult.Page(
data = responseData.comments,
prevKey = if (page == 0) null else page - 1,
nextKey = if (!responseData.hasNextPage) null else responseData.currentPage + 1
)
} catch (e: IOException) {
LoadResult.Error(e)
} catch (e: HttpException) {
LoadResult.Error(e)
}
}
}
最佳答案
我通过在 getRefreshKey()
方法中返回 null 来解决这个问题
https://stackoverflow.com/questions/70444830/
相关文章:
google-cloud-platform - 关于 Google Cloud Pub/Sub 消息
nuget - nuget 的 GitHub Packages 服务身份验证问题
reactjs - 为什么 npm uninstall -g create-react-app 失败
vue.js - 有没有办法在 Vue 3 Composition API 中的随机组件之间共享 r
javascript - 如何在项目的特定子目录上运行 prettier?
amazon-web-services - AWS S3 : Why Public object d