Register now for Android Dev Summit 2019!

PagedList

abstract class PagedList<T : Any!> : AbstractList<T>
kotlin.Any
   ↳ java.util.AbstractCollection<E>
   ↳ java.util.AbstractList<T>
   ↳ androidx.paging.PagedList

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 androidx.recyclerview.widget.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 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 Config to control how many items a PagedList loads, and when.

If you use LivePagedListBuilder to get a androidx.lifecycle.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 androidx.recyclerview.widget.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 Config.Builder#setEnablePlaceholders(boolean) when building a 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 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

abstract

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

Builder class for PagedList.

abstract

Callback signaling when content is loaded into the list.

open

Configures how a PagedList loads content from its DataSource.

Public methods

open Unit
addWeakCallback(@Nullable previousSnapshot: MutableList<T>?, @NonNull callback: PagedList.Callback)

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

open Unit

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

open T?
get(index: Int)

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

open PagedList.Config

Return the Config used to construct this PagedList.

abstract DataSource<*, T>

Return the DataSource that provides data to this PagedList.

abstract Any?

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

open Int

Returns the number of items loaded in the PagedList.

open Int

Position offset of the data in the list.

open Boolean

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

open Boolean

Returns whether the list is immutable.

open Unit
loadAround(index: Int)

Load adjacent items to passed index.

open Unit

Removes a previously added callback.

open MutableList<T>

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

Properties

open Int

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

Public methods

addWeakCallback

open fun addWeakCallback(@Nullable previousSnapshot: MutableList<T>?, @NonNull callback: PagedList.Callback): Unit

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 MutableList<T>?: Snapshot previously captured from this List, or null.
callback MutableList<T>?: Callback to dispatch to.

detach

open fun detach(): Unit

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

@Nullable open fun get(index: Int): T?

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()
Return
T?: The item at the passed index, or null if a null placeholder is at the specified position.

See Also

getConfig

@NonNull open fun getConfig(): PagedList.Config

Return the Config used to construct this PagedList.

Return
PagedList.Config: the Config of this PagedList

getDataSource

@NonNull abstract fun getDataSource(): DataSource<*, T>

Return the DataSource that provides data to this PagedList.

Return
DataSource<*, T>: the DataSource of this PagedList.

getLastKey

@Nullable abstract fun getLastKey(): Any?

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.

Return
Any?: Key of position most recently passed to loadAround(int).

getLoadedCount

open fun getLoadedCount(): Int

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().

Return
Int: Number of items currently loaded, not counting placeholders.

See Also

getPositionOffset

open fun getPositionOffset(): Int

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.

isDetached

open fun isDetached(): Boolean

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

A detached list is immutable.

Return
Boolean: True if the data source is detached.

isImmutable

open fun isImmutable(): Boolean

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().

Return
Boolean: True if the PagedList is immutable.

loadAround

open fun loadAround(index: Int): Unit

Load adjacent items to passed index.

Parameters
index Int: Index at which to load.

removeWeakCallback

open fun removeWeakCallback(@NonNull callback: PagedList.Callback): Unit

Removes a previously added callback.

Parameters
callback PagedList.Callback: Callback, previously added.

snapshot

@NonNull open fun snapshot(): MutableList<T>

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.

Return
MutableList<T>: Immutable snapshot of PagedList data.

Properties

size

open val size: Int

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

Return
Int: Current total size of the list, including placeholders.