How to show empty view with Paging 3 library in Android
You can directly plug into the adapter loadStateFlow, e.g
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
}
}
This worked for me:
if (loadState.source.refresh is LoadState.NotLoading &&
loadState.append.endOfPaginationReached &&
adapter.itemCount < 1
) {
recyclerView.isVisible = false
textViewEmpty.isVisible = true
} else {
textViewEmpty.isVisible = false
}