2

当我移动到另一个片段时,如何恢复分页适配器项目状态?我尝试了下面的文章,但没有奏效。 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
4

1 回答 1

1

尝试添加.cachedIn(viewModelScope)您的 viewModel 代码。对于您的情况,您将获得:

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

并尝试添加您的片段:

adapter.stateRestorationPolicy =
        RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY

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

于 2021-10-28T12:09:07.063 回答