gpt4 book ai didi

android - "restore scroll position in android Paging adapter when user go to another fragment or activity from adapter"问题

转载 作者:行者123 更新时间:2023-12-05 05:57:28 29 4
gpt4 key购买 nike

当我移动到另一个 fragment 时,我如何恢复分页适配器项状态?我试过下面的文章,但没有用。 https://medium.com/@florina.muntenescu

private fun loadData() {
viewModel.apply {
lifecycleScope.launch{
viewModel.getMorePopularMovies().collectLatest { pagingData ->
dataAdapter.submitData(viewLifecycleOwner.lifecycle, pagingData)

}
}
}

}

fun getMorePopularMovies() = Pager(
PagingConfig(pageSize = 3)
) {
VerticalMoviePagingSource(api, "Popular")
}.flow

最佳答案

尝试在您的 viewModel 代码中添加 .cachedIn(viewModelScope)。对于您的案例,您将获得:

  fun getMorePopularMovies() = Pager(
PagingConfig(pageSize = 3)
) {
VerticalMoviePagingSource(api, "Popular")
}.flow.cachedIn(viewModelScope)

并尝试在您的 fragment 中添加:

adapter.stateRestorationPolicy =
RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY

如果它不起作用,那么我猜你的代码中还有另一个问题。

关于android - "restore scroll position in android Paging adapter when user go to another fragment or activity from adapter"问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68794350/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com