在 jetpack 撰写导航中返回导航时分页 3 列表自动刷新
我正在使用 Jetpack Compose 以及 Paging 3 库和 Paging 3 库。喷气背包导航。我面临的问题是我有一个 LazyList,它使用分页库从远程源获取数据。
ViewModel
fun getImages(): Flow<PagingData<ObjectImage>> = Pager(
PagingConfig(PAGE_SIZE, enablePlaceholders = false)
) { DataHome(RANDOM) }.flow.cachedIn(viewModelScope)
HomeView
val images = viewModelHome.getImages().collectAsLazyPagingItems()
LazyColumn {
...
}
现在发生的事情是,当我使用 navHostController.navigate()
导航到另一个视图,然后按返回键转到 HomeView 时... LazyColumn 会自行重置 &再次开始从网络加载项目。
所以我被这个问题困扰了。我尝试在 viewModel 变量中手动缓存...虽然它可以工作,但它搞砸了 SwipeRefresh (停止显示刷新状态)
data.apply {
when {
// refresh
loadState.refresh is LoadState.Loading -> {
ItemLoading()
}
// reload
loadState.append is LoadState.Loading -> {...}
// refresh error
loadState.refresh is LoadState.Error -> {...}
// reload error
loadState.append is LoadState.Error -> {...}
}
}
implementation("androidx.paging:paging-runtime-ktx:3.1.0")
implementation("androidx.paging:paging-compose:1.0.0-alpha14")
这是仍处于 alpha 状态的 PagingLibrary 的问题吗?
更新1(我不确定这是否是一个好的解决方案,但我正在解决 滑动刷新问题如下)
// get images
var images: Flow<PagingData<ObjectImage>> = Pager(PagingConfig(PAGE_SIZE)) {
DataHome(RANDOM)
}.flow.cachedIn(viewModelScope)
// reload items
fun reload(){
images = Pager(PagingConfig(PAGE_SIZE)) {
DataHome(RANDOM)
}.flow.cachedIn(viewModelScope)
}
// and rather than calling .refresh() method on lazy items... I am calling viewModel.reload()
I am using Jetpack Compose, along with Paging 3 library & Jetpack Navigation. The issue I am facing is I have a LazyList which is fetching data from remote source using paging library.
ViewModel
fun getImages(): Flow<PagingData<ObjectImage>> = Pager(
PagingConfig(PAGE_SIZE, enablePlaceholders = false)
) { DataHome(RANDOM) }.flow.cachedIn(viewModelScope)
HomeView
val images = viewModelHome.getImages().collectAsLazyPagingItems()
LazyColumn {
...
}
Now whats happening is when I navigate to another View using navHostController.navigate()
and then press back to get to HomeView... the LazyColumn resets itself & start loading items again from network.
So I am stuck with this issue. I tried manually caching in viewModel variable... though it works but it screws up SwipeRefresh (which stops showing refresh state)
data.apply {
when {
// refresh
loadState.refresh is LoadState.Loading -> {
ItemLoading()
}
// reload
loadState.append is LoadState.Loading -> {...}
// refresh error
loadState.refresh is LoadState.Error -> {...}
// reload error
loadState.append is LoadState.Error -> {...}
}
}
implementation("androidx.paging:paging-runtime-ktx:3.1.0")
implementation("androidx.paging:paging-compose:1.0.0-alpha14")
Is this an issue with PagingLibrary which is still in alpha??
Update 1 (I am not sure if this is a good solution, but I am solving
the swipe refresh issue as follows)
// get images
var images: Flow<PagingData<ObjectImage>> = Pager(PagingConfig(PAGE_SIZE)) {
DataHome(RANDOM)
}.flow.cachedIn(viewModelScope)
// reload items
fun reload(){
images = Pager(PagingConfig(PAGE_SIZE)) {
DataHome(RANDOM)
}.flow.cachedIn(viewModelScope)
}
// and rather than calling .refresh() method on lazy items... I am calling viewModel.reload()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是,每次调用
getImages()
时,您都会创建新的Pager
,这就是每次可组合项重新组合时,这不是它应该完成的方式。您应该将其设置为
val items = Pager(...
以便缓存正常工作。对于搞砸的
SwipeRefresh
,您如何实现它?有一个 refresh() 方法LazyPagingItems
,您应该使用它:好的,根据您的问题的评论和编辑:
在您的视图模型中,按照我之前的建议进行操作:
您的可组合项可以如下所示:
The problem is that you are creating new
Pager
every time you callgetImages()
, which is every time your composable recomposes, that's not how it's supposed to be done.You should make it a
val items = Pager(...
for the caching to work.For the screwed up
SwipeRefresh
, how do you implement it? There is a refresh() method onLazyPagingItems
, you should use that.EDIT: Ok, so based on the coments and edits to your question:
In your viewmodel, do as I suggested before:
Your composable can then look like this: