This talk will try to go in-depth with the new Jetpack paging library at the same time that tries to demonstrate that, in practice, will fit in our projects with no headaches.
Android developer at Jacoti (jacoti.com) ❖ Founder & organizer of Barcelona Android Group ❖ Open source lover, passionate motorbike rider and astronomy hobbyist
is designed to help us to load and display pieces of data both a the same time. ❖ Loading partial data on demand reduces usage of network bandwidth and system resources.
easily get and present any list of items that come from any paged source. And that’s cool, but take into account that you will be tangled to some architecture components like LiveData. Good news here: is ready to use with RxJava2 and fairly easy to combine with Coroutines.
data from our DataSource. Loading is done on a background thread, but consumed on the main thread using an Executor, which means: no extra threading configuration is required. This component can be configured using the library's provided builder, as we can see in the following code.
will help us to show the data in the standard way. If we’re already using the ListAdapter class, we've to only replace this class by PagedListAdapter and we’re done. PagedListAdapter uses DiffUtil to avoid represent duplicated items.
helper class that is in charge to put together our PagedList and our DataSource. Here will also define the Executor that will manage the threading for us. The result of this builder is a LiveData<PagedList>, who we’ll observe from our view and send the updates to our previous defined PagedAdapter.
return keys for next/previous pages. As simple as this. We can use this class if we need to load data based on the number of pages in the DataSource. For instance, we pass page number as a query parameter in the request. The page number will increment sequentially until all the pages are fetched and displayed.
who determines the next page is a key instead of a page. For example we’re loading a list of products with a defined productId key and with no pagination defined. In a common case, we will not know the size, so this DataSource will fetch items for us by sets of 10 items.
load pages of a requested size at arbitrary positions, and provide a fixed item count. This is the most abstract (and flexible) DataSource by the library but in general, the other two will fit our use cases.