【问题标题】:Android Paging 3 doesnt show Loadstate AdapterAndroid Paging 3 不显示 Loadstate Adapter
【发布时间】:2020-12-15 04:36:16
【问题描述】:

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

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

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

  binding.allProductRecyclerView.adapter = adapter.withLoadStateFooter(
        footer = ProductLoadingStateAdapter()
    )

This is the gist of LoadStateAdapter 还有Activity Layoutload state Item

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

【问题讨论】:

  • 你找到解决办法了吗?
  • 你找到解决方法了吗,我也有同样的问题

标签: android android-paging android-paging-library android-paging-3


【解决方案1】:

尝试像这样覆盖您的 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
   }
}

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

因此,在您的情况下,以下代码将始终导致 progress.visibilityVISIBLE。因为你只能在onBindViewHolder 中获得LoadState.Loading 状态。

//loadState is always LoadState.Loading
if (loadState is LoadState.Loading) progress.visibility =
            View.VISIBLE; txtErrorMessage.visibility = View.GONE

【讨论】:

  • loadState is LoadState.Loading 不正确。 loadState.mediator?.refresh is LoadState.Loading 是正确的。
猜你喜欢
  • 1970-01-01
  • 2023-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-29
  • 1970-01-01
  • 2020-12-12
  • 2021-10-05
相关资源
最近更新 更多