PagedList

public abstract class PagedList
extends AbstractList<T>

java.lang.Object
   ↳ java.util.AbstractCollection<T>
     ↳ java.util.AbstractList<T>
       ↳ androidx.paging.PagedList<T>


Lazy loading list that pages in immutable content from a DataSource.

A PagedList is a List which loads its data in chunks (pages) from a DataSource. Items can be accessed with get(int), and further loading can be triggered with loadAround(int). To display a PagedList, see PagedListAdapter, which enables the binding of a PagedList to a RecyclerView.

Loading Data

All data in a PagedList is loaded from its DataSource. Creating a PagedList loads the first chunk of data from the DataSource immediately, and should for this reason be done on a background thread. The constructed PagedList may then be passed to and used on the UI thread. This is done to prevent passing a list with no loaded content to the UI thread, which should generally not be presented to the user.

A PagedList initially presents this first partial load as its content, and expands over time as content is loaded in. When loadAround(int) is called, items will be loaded in near the passed list index. If placeholder nulls are present in the list, they will be replaced as content is loaded. If not, newly loaded items will be inserted at the beginning or end of the list.

PagedList can present data for an unbounded, infinite scrolling list, or a very large but countable list. Use PagedList.Config to control how many items a PagedList loads, and when.

If you use LivePagedListBuilder to get a LiveData<PagedList>, it will initialize PagedLists on a background thread for you.

Placeholders

There are two ways that PagedList can represent its not-yet-loaded data - with or without null placeholders.

With placeholders, the PagedList is always the full size of the data set. get(N) returns the Nth item in the data set, or null if its not yet loaded.

Without null placeholders, the PagedList is the sublist of data that has already been loaded. The size of the PagedList is the number of currently loaded items, and get(N) returns the Nth loaded item. This is not necessarily the Nth item in the data set.

Placeholders have several benefits:

  • They express the full sized list to the presentation layer (often a PagedListAdapter), and so can support scrollbars (without jumping as pages are loaded or dropped) and fast-scrolling to any position, loaded or not.
  • They avoid the need for a loading spinner at the end of the loaded list, since the list is always full sized.

They also have drawbacks:

  • Your Adapter needs to account for null items. This often means providing default values in data you bind to a RecyclerView.ViewHolder.
  • They don't work well if your item views are of different sizes, as this will prevent loading items from cross-fading nicely.
  • They require you to count your data set, which can be expensive or impossible, depending on your DataSource.

Placeholders are enabled by default, but can be disabled in two ways. They are disabled if the DataSource does not count its data set in its initial load, or if false is passed to PagedList.Config.Builder.setEnablePlaceholders(boolean) when building a PagedList.Config.

Mutability and Snapshots

A PagedList is mutable while loading, or ready to load from its DataSource. As loads succeed, a mutable PagedList will be updated via Runnables on the main thread. You can listen to these updates with a PagedList.Callback. (Note that PagedListAdapter will listen to these to signal RecyclerView about the updates/changes).

If a PagedList attempts to load from an invalid DataSource, it will detach() from the DataSource, meaning that it will no longer attempt to load data. It will return true from isImmutable(), and a new DataSource / PagedList pair must be created to load further data. See DataSource and LivePagedListBuilder for how new PagedLists are created to represent changed data.

A PagedList snapshot is simply an immutable shallow copy of the current state of the PagedList as a List. It will reference the same inner items, and contain the same null placeholders, if present.

Summary

Nested classes

class PagedList.BoundaryCallback<T>

Signals when a PagedList has reached the end of available data. 

class PagedList.Builder<Key, Value>

Builder class for PagedList. 

class PagedList.Callback

Callback signaling when content is loaded into the list. 

class PagedList.Config

Configures how a PagedList loads content from its DataSource. 

Inherited fields

Public methods

void addWeakCallback(List<T> previousSnapshot, PagedList.Callback callback)

Adds a callback, and issues updates since the previousSnapshot was created.

void detach()

Detach the PagedList from its DataSource, and attempt to load no more data.

T get(int index)

Get the item in the list of loaded items at the provided index.

PagedList.Config getConfig()

Return the Config used to construct this PagedList.

abstract DataSource<?, T> getDataSource()

Return the DataSource that provides data to this PagedList.

abstract Object getLastKey()

Return the key for the position passed most recently to loadAround(int).

int getLoadedCount()

Returns the number of items loaded in the PagedList.

int getPositionOffset()

Position offset of the data in the list.

boolean isDetached()

True if the PagedList has detached the DataSource it was loading from, and will no longer load new data.

boolean isImmutable()

Returns whether the list is immutable.

void loadAround(int index)

Load adjacent items to passed index.

void removeWeakCallback(PagedList.Callback callback)

Removes a previously added callback.

int size()

Returns size of the list, including any not-yet-loaded null padding.

List<T> snapshot()

Returns an immutable snapshot of the PagedList in its current state.

Inherited methods

Public methods

addWeakCallback

public void addWeakCallback (List<T> previousSnapshot, 
                PagedList.Callback callback)

Adds a callback, and issues updates since the previousSnapshot was created.

If previousSnapshot is passed, the callback will also immediately be dispatched any differences between the previous snapshot, and the current state. For example, if the previousSnapshot was of 5 nulls, 10 items, 5 nulls, and the current state was 5 nulls, 12 items, 3 nulls, the callback would immediately receive a call of onChanged(14, 2).

This allows an observer that's currently presenting a snapshot to catch up to the most recent version, including any changes that may have been made.

The callback is internally held as weak reference, so PagedList doesn't hold a strong reference to its observer, such as a PagedListAdapter. If an adapter were held with a strong reference, it would be necessary to clear its PagedList observer before it could be GC'd.

Parameters
previousSnapshot List: Snapshot previously captured from this List, or null.

callback PagedList.Callback: Callback to dispatch to.

detach

public void detach ()

Detach the PagedList from its DataSource, and attempt to load no more data.

This is called automatically when a DataSource load returns null, which is a signal to stop loading. The PagedList will continue to present existing data, but will not initiate new loads.

get

public T get (int index)

Get the item in the list of loaded items at the provided index.

Parameters
index int: Index in the loaded item list. Must be >= 0, and < size()

Returns
T The item at the passed index, or null if a null placeholder is at the specified position.

See also:

getConfig

public PagedList.Config getConfig ()

Return the Config used to construct this PagedList.

Returns
PagedList.Config the Config of this PagedList

getDataSource

public abstract DataSource<?, T> getDataSource ()

Return the DataSource that provides data to this PagedList.

Returns
DataSource<?, T> the DataSource of this PagedList.

getLastKey

public abstract Object getLastKey ()

Return the key for the position passed most recently to loadAround(int).

When a PagedList is invalidated, you can pass the key returned by this function to initialize the next PagedList. This ensures (depending on load times) that the next PagedList that arrives will have data that overlaps. If you use LivePagedListBuilder, it will do this for you.

Returns
Object Key of position most recently passed to loadAround(int).

getLoadedCount

public int getLoadedCount ()

Returns the number of items loaded in the PagedList. Unlike size() this counts only loaded items, not placeholders.

If placeholders are disabled, this method is equivalent to size().

Returns
int Number of items currently loaded, not counting placeholders.

See also:

getPositionOffset

public int getPositionOffset ()

Position offset of the data in the list.

If data is supplied by a PositionalDataSource, the item returned from get(i) has a position of i + getPositionOffset().

If the DataSource is a ItemKeyedDataSource or PageKeyedDataSource, it doesn't use positions, returns 0.

Returns
int

isDetached

public boolean isDetached ()

True if the PagedList has detached the DataSource it was loading from, and will no longer load new data.

A detached list is immutable.

Returns
boolean True if the data source is detached.

isImmutable

public boolean isImmutable ()

Returns whether the list is immutable. Immutable lists may not become mutable again, and may safely be accessed from any thread.

In the future, this method may return true when a PagedList has completed loading from its DataSource. Currently, it is equivalent to isDetached().

Returns
boolean True if the PagedList is immutable.

loadAround

public void loadAround (int index)

Load adjacent items to passed index.

Parameters
index int: Index at which to load.

removeWeakCallback

public void removeWeakCallback (PagedList.Callback callback)

Removes a previously added callback.

Parameters
callback PagedList.Callback: Callback, previously added.

size

public int size ()

Returns size of the list, including any not-yet-loaded null padding. To get the number of loaded items, not counting placeholders, use getLoadedCount().

Returns
int Current total size of the list, including placeholders.

See also:

snapshot

public List<T> snapshot ()

Returns an immutable snapshot of the PagedList in its current state. If this PagedList is immutable due to its DataSource being invalid, it will be returned.

Returns
List<T> Immutable snapshot of PagedList data.