【问题标题】:Android Paging 3 with LoadStateAdapter - Issue of Screen flickering and jumping when new pages are loaded on scrolling带有 LoadStateAdapter 的 Android Paging 3 - 滚动加载新页面时屏幕闪烁和跳跃的问题
【发布时间】:2022-03-04 15:06:46
【问题描述】:

我已经使用 RemoteMediator 实现了 Paging 3。 并且还有一个 LoadStateAdapter。

最初,我在滚动页面时遇到闪烁、故障和跳跃。 这个答案可以解决这个问题 - https://stackoverflow.com/a/66713643/15392387

我可以在一个屏幕上看到我的 RecyclerView 中的 3 个项目,因此按照建议设置 PageSize = 8 可以解决所有闪烁问题。

但由于我也使用了 PagingDataAdapter.withLoadStateHeaderAndFooter,所以安装应用时的初始加载,会自动向下滚动到第 8 个 ListItem。

它不是从页面顶部开始的。

有人可以帮我解决这个问题吗?

我找到了一个可能正在谈论相同问题的答案,但解决方案仍不清楚 - https://stackoverflow.com/a/66763460/15392387

HomeFragment.kt

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        _binding = FragmentHomeBinding.bind(view) //View Binding

        val parentAdapter = PlaylistParentPagingAdapter(this, this)

        binding.apply {
            playlistParentRecyclerView.setHasFixedSize(true)
            playlistParentRecyclerView.adapter = parentAdapter.withLoadStateHeaderAndFooter(
             header = PlaylistLoadStateAdapter { parentAdapter.retry() },
             footer = PlaylistLoadStateAdapter { parentAdapter.retry() },
           )

        }

        viewModel.playlists.observe(viewLifecycleOwner) {
            parentAdapter.submitData(viewLifecycleOwner.lifecycle, it)
        }
    }

PlaylistLoadStateAdapter.kt

class PlaylistLoadStateAdapter(private val retry: () -> Unit) :
    LoadStateAdapter<PlaylistLoadStateAdapter.LoadStateViewHolder>() {

    private val TAG = "PlaylistLoadStateAdapte"

    override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState): LoadStateViewHolder {
        val binding = PlaylistLoadStateFooterBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return LoadStateViewHolder(binding)
    }

    override fun onBindViewHolder(holder: LoadStateViewHolder, loadState: LoadState) {
        holder.bind(loadState)
    }

    inner class LoadStateViewHolder(private val binding: PlaylistLoadStateFooterBinding) :
        RecyclerView.ViewHolder(binding.root) {

        init {
            binding.retryButtonFooter.setOnClickListener {
                retry.invoke()
            }
        }

        fun bind(loadState: LoadState) {
            binding.apply {
                Log.d(TAG, "bind: loadstate = $loadState")
                progressBarFooter.isVisible = loadState is LoadState.Loading
                retryButtonFooter.isVisible = loadState !is LoadState.Loading
                errorTextViewFooter.isVisible = loadState !is LoadState.Loading
            }
    }
    }

【问题讨论】:

    标签: android android-paging-3


    【解决方案1】:

    RecyclerView 有一个开放的功能请求,在考虑恢复滚动位置时,我们需要一种方法来忽略页眉/页脚。本质上,您有一个仅包含页脚的列表,然后是一个包含一些项目 + 页脚的列表,并且 RV 正在尝试将滚动位置恢复到页脚,因为这是唯一的重叠。

    您可以在此处关注问题:https://issuetracker.google.com/issues/184874613

    一种解决方法是使用占位符,但如果这不合适,您也可以尝试自定义 LoadStateAdapter,它仅在 PagingSource 本地加载完成后可见。这将导致您的页脚在本地刷新期间被删除并重新添加,但它很可能对用户不可见。

    【讨论】:

      猜你喜欢
      • 2021-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-03
      相关资源
      最近更新 更多