A-A*_*UCG 10 android android-paging-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)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我添加页脚的方式
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)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
handlePagingState只是跟随状态并将其绑定到页面状态(正在加载、错误、空等)。无论如何,删除它并没有改变任何事情。
甚至ListLoadStateAdapter.onCreateViewHolder()都没有被调用, 的构造函数也没有被调用ListLoadStateViewHolder。
我究竟做错了什么?有什么我错过的吗?或者也许某个地方有错误?
A-A*_*UCG 18
我的问题是我没有设置ConcatAdapter返回的withLoadStateFooter
recyclerView.adapter = adapter.withLoadStateFooter(
footer = ListLoadStateAdapter { retry() }
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3613 次 |
| 最近记录: |