Android Paging 3 不显示 Loadstate Adapter

ron*_*eno 5 android android-paging android-paging-library android-paging-3

我已经按照教程将 Loadstate Adapter 添加到 Android Paging 3 上的 Recyclerview Adapter 但目前,它没有显示。这就是我更新适配器的方式。

 lifecycleScope.launch {
            viewModel.searchProducts(searchParam, channelId, categoryId)
                .collectLatest {
                    binding.allProductRecyclerView.isVisible = true
                    adapter.submitData(it)
                }
        
Run Code Online (Sandbox Code Playgroud)

这就是我添加 LoadState 适配器的方式

  binding.allProductRecyclerView.adapter = adapter.withLoadStateFooter(
        footer = ProductLoadingStateAdapter()
    )
Run Code Online (Sandbox Code Playgroud)

这是 LoadStateAdapter 的要点也是Activity Layout加载状态项

适配器工作正常,我还可以通过添加 LoadStateListener 来获取负载状态。只有负载状态适配器不工作。我已经跟踪并克隆了,它运行良好。我的可能是什么问题?

Zij*_*ang 0

尝试在 ProductLoadingStateAdapter 中重写以下方法,如下所示:

class ProductLoadingStateAdapter: LoadStateAdapter<XXX>() {
   override fun onBindViewholder...

   override fun onCreateViewHolder...
  
   override fun displayLoadStateAsItem(loadState: LoadState): Boolean {
      return loadState is LoadState.Loading || loadState is LoadState.Error || LoadState.NotLoading
   }
}
Run Code Online (Sandbox Code Playgroud)

如果你查看 LoadStateAdapter 的源代码,默认情况下它只向适配器发出 Loading 和 Error 加载状态,这意味着适配器不知道加载过程是否已完成。

因此,在您的情况下,以下代码的结果将始终progress.visibilityVISIBLE. 因为你只能LoadState.Loading在你的onBindViewHolder.

//loadState is always LoadState.Loading
if (loadState is LoadState.Loading) progress.visibility =
            View.VISIBLE; txtErrorMessage.visibility = View.GONE
Run Code Online (Sandbox Code Playgroud)