如何在 Android 中使用 Paging 3 库显示空视图
How to show empty view with Paging 3 library in Android
我想在 paging3 加载空列表时显示空视图。
它似乎适用于以下代码。这是处理分页 3 库的正确方法吗?:
adapter?.addLoadStateListener { loadState ->
adapter?.apply {
if (itemCount <= 0 && !loadState.source.refresh.endOfPaginationReached) {
Timber.d("==> to show empty view")
tvEmptyView.isGone = false
} else {
Timber.d("==> to hide empty view")
tvEmptyView.isGone = true
}
}
}
您可以直接插入适配器loadStateFlow,例如
lifecycleScope.launchWhenCreated {
@OptIn(ExperimentalCoroutinesApi::class)
adapter.loadStateFlow.collectLatest { loadStates ->
val refresher = loadStates.refresh
val displayEmptyMessage = (refresher is LoadState.NotLoading && refresher.endOfPaginationReached && adapter.itemCount == 0)
layoutBinding.emptyStateMessage.isVisible = displayEmptyMessage
layoutBinding.emptyStateImage.isVisible = displayEmptyMessage
layoutBinding.swipeToRefresh.isRefreshing = refresher is LoadState.Loading
}
}
这对我有用:
if (loadState.source.refresh is LoadState.NotLoading &&
loadState.append.endOfPaginationReached &&
adapter.itemCount < 1
) {
recyclerView.isVisible = false
textViewEmpty.isVisible = true
} else {
textViewEmpty.isVisible = false
}
在我的例子中,如果它们小于 11,我不得不使用 concatAdapter 来显示低于 正常结果的空视图。不幸的是,分页 3 库会给我很多误报关于 LoadState.NotLoading
和 loadState.append.endOfPaginationReached
状态的结果,所以我不得不通过引入计数器来仔细检查它。 Florian 或 Paul 的回答都可以。
offersAdapter.addLoadStateListener { updateUiOnNewLoadSate(it) }
private fun updateUiOnNewLoadSate(loadState: CombinedLoadStates) {
Timber.i("STATE $loadState")
// Show empty view if results are less or equal 10
val displayEmptySearch =
loadState.source.refresh is LoadState.NotLoading && loadState.append.endOfPaginationReached && offersAdapter.itemCount <= 10 && loadStatesCounter > 1
if (displayEmptySearch) {
//here I display the empty view as a part of the
//infinite scrolling recyclerview by using concatAdapter
concatAdapter.addAdapter(emptyViewAdapter)
} else {
concatAdapter.removeAdapter(emptyViewAdapter)
}
loadStatesCounter++ //only apply above block when loadStatesCounter > 1
}
我想在 paging3 加载空列表时显示空视图。
它似乎适用于以下代码。这是处理分页 3 库的正确方法吗?:
adapter?.addLoadStateListener { loadState ->
adapter?.apply {
if (itemCount <= 0 && !loadState.source.refresh.endOfPaginationReached) {
Timber.d("==> to show empty view")
tvEmptyView.isGone = false
} else {
Timber.d("==> to hide empty view")
tvEmptyView.isGone = true
}
}
}
您可以直接插入适配器loadStateFlow,例如
lifecycleScope.launchWhenCreated {
@OptIn(ExperimentalCoroutinesApi::class)
adapter.loadStateFlow.collectLatest { loadStates ->
val refresher = loadStates.refresh
val displayEmptyMessage = (refresher is LoadState.NotLoading && refresher.endOfPaginationReached && adapter.itemCount == 0)
layoutBinding.emptyStateMessage.isVisible = displayEmptyMessage
layoutBinding.emptyStateImage.isVisible = displayEmptyMessage
layoutBinding.swipeToRefresh.isRefreshing = refresher is LoadState.Loading
}
}
这对我有用:
if (loadState.source.refresh is LoadState.NotLoading &&
loadState.append.endOfPaginationReached &&
adapter.itemCount < 1
) {
recyclerView.isVisible = false
textViewEmpty.isVisible = true
} else {
textViewEmpty.isVisible = false
}
在我的例子中,如果它们小于 11,我不得不使用 concatAdapter 来显示低于 正常结果的空视图。不幸的是,分页 3 库会给我很多误报关于 LoadState.NotLoading
和 loadState.append.endOfPaginationReached
状态的结果,所以我不得不通过引入计数器来仔细检查它。 Florian 或 Paul 的回答都可以。
offersAdapter.addLoadStateListener { updateUiOnNewLoadSate(it) }
private fun updateUiOnNewLoadSate(loadState: CombinedLoadStates) {
Timber.i("STATE $loadState")
// Show empty view if results are less or equal 10
val displayEmptySearch =
loadState.source.refresh is LoadState.NotLoading && loadState.append.endOfPaginationReached && offersAdapter.itemCount <= 10 && loadStatesCounter > 1
if (displayEmptySearch) {
//here I display the empty view as a part of the
//infinite scrolling recyclerview by using concatAdapter
concatAdapter.addAdapter(emptyViewAdapter)
} else {
concatAdapter.removeAdapter(emptyViewAdapter)
}
loadStatesCounter++ //only apply above block when loadStatesCounter > 1
}