Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Jetpack series: Pagination

The Jetpack series: Pagination

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.

Jorge Garrido

May 28, 2019
Tweet

More Decks by Jorge Garrido

Other Decks in Programming

Transcript

  1. #BADG About me ❖ Jorge Garrido (aka: firezenk) ❖ Senior

    Android developer at Jacoti (jacoti.com) ❖ Founder & organizer of Barcelona Android Group ❖ Open source lover, passionate motorbike rider and astronomy hobbyist
  2. #BADG Overview Following the official documentation: ❖ Jetpack’s pagination library

    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.
  3. #BADG My overview Jetpack’s pagination library will help us to

    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.
  4. #BADG Main components ❖ PagedList & PagedListAdapter ❖ LivePagedListBuilder (builds

    LiveData<PagedList>) ❖ DataSource ➢ PageKeyedDataSource (our example) ➢ ItemKeyedDataSource ➢ PositionalDataSource
  5. #BADG PagedList The PagedList component is in charge to load

    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.
  6. #BADG PagedListAdapter The new adapter provided by this library and

    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.
  7. #BADG LivePagedListBuilder No rocket science here just a very simple

    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.
  8. #BADG PageKeyedDataSource Incremental data loader for page-keyed content, where requests

    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.
  9. #BADG ItemKeyedDataSource This DataSource is our one of choice if

    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.
  10. #BADG PositionalDataSource We can choose this DataSource If we can

    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.
  11. #BADG Some related links ❖ Official docs: https://developer.android.com/topic/libraries/architecture/paging ❖ Google

    paging sample: https://github.com/googlesamples/android-architecture-components/tree/master/PagingSample ❖ Codelab: https://codelabs.developers.google.com/codelabs/android-paging/index.html?index=..%2F..%2Findex#0 ❖ Demo project: https://github.com/FireZenk/Jetpack-Pagination-library-demo