使用 Google 的 Paging3 库时显示空状态
Display empty state when using Google's Paging3 library
我目前是第一次使用 Google 的 Paging3 库。我的代码基于他们在 https://codelabs.developers.google.com/codelabs/android-paging/#0.
的 Codelab
我的应用程序的流程遵循以下规则:
- 显示空状态
- 让用户搜索内容
- 显示结果
- 当用户按下搜索栏上的后退按钮时 -> 再次显示空状态
不幸的是,代码实验室不处理空状态:(我正在使用以下 addLoadStateListener 来处理搜索结果:
adapter.addLoadStateListener { loadState ->
// Only show the list if refresh succeeds.
binding.userRecyclerView.isVisible = loadState.source.refresh is LoadState.NotLoading
// Show loading spinner during initial load or refresh.
binding.progressBar.isVisible = loadState.source.refresh is LoadState.Loading
// Show the retry state if initial load or refresh fails.
binding.errorMessage.isVisible = loadState.source.refresh is LoadState.Error
if (binding.errorMessage.isVisible) {
binding.errorMessage.text =
(loadState.source.refresh as LoadState.Error).error.toString()
}
binding.retryButton.isVisible = binding.errorMessage.isVisible
// Toast on any error while loading more entries
val errorState = loadState.source.append as? LoadState.Error
?: loadState.source.prepend as? LoadState.Error
?: loadState.append as? LoadState.Error
?: loadState.prepend as? LoadState.Error
errorState?.let {
Toast.makeText(
context,
resources.getString(R.string.error_loading),
Toast.LENGTH_LONG
).show()
}
}
如何使用addLoadStateListener来显示空状态? LoadState 只有三个可能的值:NotLoading、Loading 和 Error。但是,我并不总是想显示 RecyclerView。它应该只有在有结果并且用户没有按下后退按钮时才可见。
有人可以指出正确的方向吗?我该怎么做?关于这个问题是否有某种最佳实践?
提前致谢!
以下是一些您可能会觉得有用的 API:
您可以使用 PagingData.empty()
创建一个空的 PagingData
您可以提交以“清除”之前的 PagingData
,直到您再次调用 adapter.submitData
。如果您想在搜索之间或搜索之前显示空状态,这很有用。如果要显示一些静态内容,也可以使用 PagingData.from(list)
。
要获取显示的项目数,您可以使用 adapter.itemCount
如果您需要具体检查刷新完成后加载了哪些项目,您可以使用 adapter.peek(index)
或 adapter.snapshot()
一次获取所有项目(但更昂贵)。
我目前是第一次使用 Google 的 Paging3 库。我的代码基于他们在 https://codelabs.developers.google.com/codelabs/android-paging/#0.
的 Codelab我的应用程序的流程遵循以下规则:
- 显示空状态
- 让用户搜索内容
- 显示结果
- 当用户按下搜索栏上的后退按钮时 -> 再次显示空状态
不幸的是,代码实验室不处理空状态:(我正在使用以下 addLoadStateListener 来处理搜索结果:
adapter.addLoadStateListener { loadState ->
// Only show the list if refresh succeeds.
binding.userRecyclerView.isVisible = loadState.source.refresh is LoadState.NotLoading
// Show loading spinner during initial load or refresh.
binding.progressBar.isVisible = loadState.source.refresh is LoadState.Loading
// Show the retry state if initial load or refresh fails.
binding.errorMessage.isVisible = loadState.source.refresh is LoadState.Error
if (binding.errorMessage.isVisible) {
binding.errorMessage.text =
(loadState.source.refresh as LoadState.Error).error.toString()
}
binding.retryButton.isVisible = binding.errorMessage.isVisible
// Toast on any error while loading more entries
val errorState = loadState.source.append as? LoadState.Error
?: loadState.source.prepend as? LoadState.Error
?: loadState.append as? LoadState.Error
?: loadState.prepend as? LoadState.Error
errorState?.let {
Toast.makeText(
context,
resources.getString(R.string.error_loading),
Toast.LENGTH_LONG
).show()
}
}
如何使用addLoadStateListener来显示空状态? LoadState 只有三个可能的值:NotLoading、Loading 和 Error。但是,我并不总是想显示 RecyclerView。它应该只有在有结果并且用户没有按下后退按钮时才可见。
有人可以指出正确的方向吗?我该怎么做?关于这个问题是否有某种最佳实践?
提前致谢!
以下是一些您可能会觉得有用的 API:
您可以使用 PagingData.empty()
创建一个空的 PagingData
您可以提交以“清除”之前的 PagingData
,直到您再次调用 adapter.submitData
。如果您想在搜索之间或搜索之前显示空状态,这很有用。如果要显示一些静态内容,也可以使用 PagingData.from(list)
。
要获取显示的项目数,您可以使用 adapter.itemCount
如果您需要具体检查刷新完成后加载了哪些项目,您可以使用 adapter.peek(index)
或 adapter.snapshot()
一次获取所有项目(但更昂贵)。