The Paging Library helps you load and display small chunks of data at a time.
Loading partial data on demand reduces usage of network bandwidth and system
resources.

This guide provides several conceptual examples of the library, along with an
overview of how it works. To view complete examples of how this library
functions, try out the codelab and samples from the additional
resources section.

Library architecture

This section describes and shows the main components of the paging library.

PagedList

The Paging Library's key component is the
PagedList class, which loads
chunks of your app's data, or pages. As more data is needed, it's
paged into the existing PagedList object. If any loaded data changes, a new
instance of PagedList is emitted to the observable data holder from a
LiveData or RxJava2-based object. As
PagedList objects are generated,
your app's UI presents their contents, all while respecting your UI controllers'
lifecycles.

The following code snippet shows how you can configure your app's view model to
load and present data using a LiveData holder of PagedList objects:

To learn more about how you can load data into PagedList objects, see the
guide on how to Load paged data.

UI

The PagedList class works with a
PagedListAdapter to load items into a
RecyclerView. These
classes work together to fetch and display content as it's loaded, prefetching
out-of-view content and animating content changes.

Support different data architectures

A combination of the other sources, using the on-device database as a cache.

Figure 1 shows how data flows in each of these architecture scenarios. In
the case of a network-only or database-only solution, the data flows directly to
your app's UI model. If you're using a combined approach, data flows from your
backend server, into an on-device database, and then to your app's UI model.
Every once in a while, the endpoint of each data flow runs out of data to load,
at which point it requests more data from the component that provided the data.
For example, when an on-device database runs out of data, it requests more data
from the server.

Figure 1. How data flows through each of the architectures that the
Paging Library supports

The remainder of this section provides recommendations for configuring each
data flow use case.

Network only

Note: The Paging Library's
DataSource objects don't provide
any error handling because different apps handle and present error UIs in
different ways. If an error occurs, defer to your result callback, and retry the
request later. See the PagingWithNetwork
sample for an example of
this behavior.

Database only

Set up your RecyclerView
to observe local storage, preferably using the Room persistence
library. That way, whenever data is
inserted or modified in your app's database, these changes are automatically
reflected in the RecyclerView that's displaying this data.

Network and database

After you've started observing the database, you can listen for when the
database is out of data by using
PagedList.BoundaryCallback.
You can then fetch more items from your network and insert them into the
database. If your UI is observing the database, that's all you need to do.

Handle network errors

When using a network to fetch or page the data that you're displaying using the
Paging Library, it's important to not treat the network as being either
"available" or "unavailable" all the time, as many connections are intermittent
or flaky:

A particular server might fail to respond to a network request.

The device might be connected to a network that's slow or weak.

Instead, your app should check each request for failures and recover as
gracefully as possible in cases where the network isn't available. For example,
you can provide a "retry" button for users to select if the data refresh step
doesn't work. If an error occurs during the data paging step, it's best to retry
the paging requests automatically.

Update your existing app

If your app already consumes data from a database or a backend source, it's
possible to upgrade directly to functionality that the Paging Library provides.
This section shows how to upgrade an app that has a common existing design.

Custom paging solutions

If you use custom functionality to load small subsets of data from your app's
data source, you can replace this logic with that from the
PagedList class. Instances of
PagedList offer built-in connections to common data sources. These instances
also provide adapters for
RecyclerView objects that
you might include in your app's UI.

Data loaded using lists instead of pages

If you use an in-memory list as the backing data structure for your UI's
adapter, consider observing data updates using a
PagedList class if the number
of items in the list can become large. Instances of PagedList can use either
LiveData<PagedList> or
Observable<List> to pass data updates to your app's UI, minimizing load times
and memory usage. Better still, replacing a List
object with a PagedList object in your app doesn't require any changes to your
app's UI structure or data updating logic.

Associate a data cursor with a list view using CursorAdapter

Your app might use a CursorAdapter
to associate data from a Cursor with a
ListView. In that case, you usually need
to migrate from a ListView to a
RecyclerView as your
app's list UI container, then replace the Cursor
component with either Room or
PositionalDataSource, depending on whether instances of Cursor access a
SQLite database.

In some situations, such as when working with instances of
Spinner, you provide only the adapter
itself. A library then takes the data that's loaded into that adapter and
displays the data for you. In these situations, change the type of your
adapter's data to
LiveData<PagedList>, then wrap
this list in an ArrayAdapter object
before attempting to have a library class inflate these items in a UI.

Load content asynchronously using AsyncListUtil

If you're using
AsyncListUtil objects to
load and display groups of information asynchronously, the Paging Library lets
you load data more easily:

Your data doesn't need to be positional. 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. The Paging library can present your
data that your app's ViewModel holds in an observable data structure.

Database examples

The following code snippets show several possible ways of having all the pieces
work together.

Observing paged data using LiveData

The following code snippet shows all the pieces working together. As concert
events are added, removed, or changed in the database, the content in the
RecyclerView is
automatically and efficiently updated: