3

我正在关注 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

我究竟做错了什么?有什么我错过的吗?或者也许是某个地方的错误?

4

4 回答 4

7

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

于 2021-04-23T10:53:05.923 回答
0

从答案改进: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()
        }
    )
}

这样我们就可以在回收器视图适配器初始化期间直接调用它,

rv.adapter = customPagingAdapter.loadFooter()

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

于 2022-02-02T08:42:25.100 回答
0

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

于 2021-05-07T10:51:29.370 回答
0

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

val adapterWithLoading = adapter.withLoadStateFooter(PagingLoadStateAdapter(adapter::retry))

binding.recycler.apply {
    layoutManager = LinearLayoutManager(context)
    adapter = adapterWithLoading
    addItemDecoration(ItemHorizontalDecorator())
}
于 2021-10-21T09:50:23.893 回答