【问题标题】:Footer not showing with Paging 3页脚不显示与第 3 页
【发布时间】:2021-04-22 18:52:24
【问题描述】:

我正在关注 Paging 3 的 Codelab。

分页工作正常,但尝试添加页脚似乎不起作用。

LoadStateAdapter 使用时,我的代码与 Codelab 的代码完全相同

class ListLoadStateAdapter(
    private val retry: () -> Unit,
) : LoadStateAdapter<ListLoadStateAdapter.ListLoadStateViewHolder>() {

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

    override fun onCreateViewHolder(
        parent: ViewGroup,
        loadState: LoadState,
    ) = ListLoadStateViewHolder.create(parent, retry)

    class ListLoadStateViewHolder(
        private val binding: ComponentPagedListFooterBinding,
        retry: () -> Unit,
    ) : RecyclerView.ViewHolder(binding.root) {

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

        fun bind(loadState: LoadState) {
            if (loadState is LoadState.Error) {
                binding.textViewPlaceholderError.text = loadState.error.localizedMessage
            }

            binding.progressBar.isVisible = loadState is LoadState.Loading
            binding.buttonRetry.isVisible = loadState is LoadState.Error
            binding.textViewPlaceholderError.isVisible = loadState is LoadState.Error

//            binding.root.isVisible = loadState is LoadState.Loading || loadState is LoadState.Error
        }

        companion object {
            fun create(parent: ViewGroup, retry: () -> Unit): ListLoadStateViewHolder {
                val binding = ComponentPagedListFooterBinding.inflate(
                    LayoutInflater.from(parent.context),
                    parent,
                    false,
                )

                return ListLoadStateViewHolder(binding, retry)
            }
        }
    }
}

这就是我添加页脚的方式

adapter = this@InvoiceListFragment.adapter.apply {
                withLoadStateFooter(ListLoadStateAdapter { retry() })

                addLoadStateListener {
                    viewModel.handlePagingState(it, this)
                }
            }

handlePagingState只是跟随着状态,绑定到Page状态(Loading、Error、Empty等)。无论如何,删除它并没有改变。

ListLoadStateAdapter.onCreateViewHolder() 甚至没有被调用,ListLoadStateViewHolder 的构造函数也没有。

我做错了什么?有什么我错过的吗?或者可能是某个地方的错误?

【问题讨论】:

    标签: android android-paging-3


    【解决方案1】:

    我的问题是我没有设置withLoadStateFooter返回的ConcatAdapter

    【讨论】:

    • 你必须在哪里设置它?
    • @mantc_sdr 在这些yourAdapter.addLoadStateListener{} val contcatAdapter = yourAdapter.withLoadStateFooter(footer = YourLoadStateFooter{rourAdapter.retry()}) 之后,你只需要做rourRecyclerView.adapter = concatAdapter
    【解决方案2】:

    我遇到了保存问题。我的问题是我最初尝试在没有互联网的情况下从缓存中加载项目,在这种情况下,页脚没有出现。当我从 api 加载项目而不是从互联网加载项目时 - 页脚工作正常。

    【讨论】:

      【解决方案3】:

      如果有人仍然面临同样的问题。您必须将新适配器应用到回收站视图。

      val adapterWithLoading = adapter.withLoadStateFooter(PagingLoadStateAdapter(adapter::retry))
      
      binding.recycler.apply {
          layoutManager = LinearLayoutManager(context)
          adapter = adapterWithLoading
          addItemDecoration(ItemHorizontalDecorator())
      }
      

      【讨论】:

        【解决方案4】:

        从答案改进:https://stackoverflow.com/a/67228677/7735068

        我已经为它创建了一个扩展函数,

        fun <T : Any, VH : RecyclerView.ViewHolder> PagingDataAdapter<T, VH>.loadFooter(): ConcatAdapter {
        
            Timber.e("Appending footer")
            return this.withLoadStateFooter(
                footer = FooterAdapter {
                    this.retry()
                }
            )
        }
        

        这样我们就可以在recycler view adapter初始化的时候直接调用,

        rv.adapter = customPagingAdapter.loadFooter()
        

        FooterAdapter 实现的其余部分。和codelabs一样。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-01-23
          • 1970-01-01
          • 1970-01-01
          • 2013-05-22
          • 1970-01-01
          • 2018-12-28
          • 1970-01-01
          • 2015-03-04
          相关资源
          最近更新 更多