我正在关注 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
。
我究竟做错了什么?有什么我错过的吗?或者也许是某个地方的错误?