当分页3加载空列表时,我想显示空 View 。
似乎可以使用以下代码。这是使用分页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
}
}