I want to show empty view when paging3 is loaded with an empty list.
It seems to work with following code. Is this the proper way to do with the paging 3 library?:
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
}
}
}
Display the paged data in your UI Create an instance of your PagingDataAdapter class. Pass the PagingDataAdapter instance to the RecyclerView list that you want to display your paged data. Observe the PagingData stream, and pass each generated value to your adapter's submitData() method.
The Paging Library lets you load data directly from your backend using keys that the network provides. Your data can be uncountably large. Using the Paging Library, you can load data into pages until there isn't any data remaining. You can observe your data more easily.
Paging library overview Part of Android Jetpack. The Paging library helps you load and display pages of data from a larger dataset from local storage or over network. This approach allows your app to use both network bandwidth and system resources more efficiently.
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
}
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With