MoviePagingSource.kt
- Notice the overridden
load()
function is asuspend
function so we can make api requests here to get data from a network or a room database easily. - The
LoadParams
object holds the information about the load operation such askey
and page size. - If the api request is successful, then we will return the response data wrapped in a
LoadResult.Page
object along with the previous and next keys. - If the api request is unsuccessful then we will return the occurred exception wrapped in a
LoadResult.Error
object.
The figure below shows exactly how the load()
function recieves keys from the previous load and provides the keys for the next load.
Photo on Official Docs of Paging 3
Get the PagingData in ViewModel
Now we will create an instance of Pager
in our viewmodel to get a stream of data from the MoviePagingSource
that we just created.
MovieFragmentViewModel.kt
- The
Pager
object calls theload()
method from theMoviePagingSource
object, providing it with theLoadParams
object and receiving theLoadResult
object in return. - We also have to provide configurations such as
pageSize
with thePagingConfig
object. - The
cachedIn(viewModelScope)
caches the data from theMoviePagingSource
to survive the screen orientation changes.
Display data in RecyclerView
First we have to create a RecyclerView
adapter class which extends from the PagingDataAdapter
. This is the same as a normal RecyclerView
adapter. The PagingDataAdapter
takes two parameters, the first one is the type of the data(which in our case is the Movie
object), and the second one is a RecyclerView.ViewHolder
.
MovieAdapter.kt
Finally, in OnActivityCreated()
of our fragment, we can collect the data from PagingData
and then submit it into our MovieAdapter
.
MovieFragment.kt
The RecyclerView
list now displays data from the data source and automatically loads more data when we reach at the end of the list.
Display LoadState and Error messages
The Paging 3.0 library has support for displaying loading states and handling errors. The Paging library exposes the loading state for use in the UI through the LoadState
object.
- If the
LoadState
is aLoadState.NotLoading
object, then there is no active load operation and no errors. - If the
LoadState
is aLoadState.Loading
object, then there is an active load operation. - If the
LoadState
is aLoadState.Error
object, then there is an error.
We can attach a addLoadStateListener()
in our MovieAdapter
to listen for LoadState
updates.
LoadStateListener.kt
In the next article, we learn how to create header or footers to show the loading state in the RecyclerView
itself, and then we will learn to create list separators.
Conclusion
The Paging 3.0 architectural components library is a major update over the previous versions of paging library, and it is completely rewritten from the previous versions of Paging library. It has complete support for the Kotlin coroutines
and other reactive streams such as RxJava
and LiveData
. It also has inbuilt error handling functionality and support for managing loading states which makes implementing paging in our app super easy.
Other parts
References