O Google tem o compromisso de promover a igualdade racial para as comunidades negras. Saiba como.

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 PagingSource.

A PagedList is a List which loads its data in chunks (pages) from a PagingSource. Items can be accessed with get, and further loading can be triggered with loadAround. To display a PagedList, see androidx.paging.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 PagingSource. Creating a PagedList loads the first chunk of data from the PagingSource 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 PagedList.Config to control how many items a PagedList loads, and when.

If you use androidx.paging.LivePagedListBuilder to get a androidx.lifecycle.LiveData, 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 androidx.paging.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 PagingSource.

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

Mutability and Snapshots

A PagedList is mutable while loading, or ready to load from its PagingSource. 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 androidx.paging.PagedListAdapter will listen to these to signal RecyclerView about the updates/changes).

If a PagedList attempts to load from an invalid PagingSource, it will detach from the PagingSource, meaning that it will no longer attempt to load data. It will return true from isImmutable, and a new PagingSource / PagedList pair must be created to load further data.

See PagingSource and androidx.paging.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.

Configures how a PagedList loads content from its PagingSource.

Public methods
Unit
addWeakCallback(previousSnapshot: List<T>?, callback: PagedList.Callback)

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

Unit

Adds a callback.

Unit

Add a listener to observe the loading state of the PagedList.

abstract Unit

Detach the PagedList from its PagingSource, 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.

Unit
loadAround(index: Int)

Load adjacent items to passed index.

Unit

Removes a previously added callback.

Unit

Remove a previously registered load state listener.

open Unit

Retry any errors associated with this PagedList.

List<T>

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

Inherited extension functions
From androidx.compose.ui.gesture.kotlin.collections.List
Boolean
List<PointerInputChange>.anyPointersInBounds(bounds: IntSize)

Utility method that determines if any pointers are currently in bounds.

From androidx.wear.complications.data.kotlin.collections.Collection
IntArray

Converts an array of ComplicationType to an array of integers with the corresponding wire types.

From androidx.compose.ui.util.kotlin.collections.List
Boolean
List<T>.fastAll(predicate: (T) -> Boolean)

Returns true if all elements match the given predicate.

Boolean
List<T>.fastAny(predicate: (T) -> Boolean)

Returns true if at least one element matches the given predicate.

T?
List<T>.fastFirstOrNull(predicate: (T) -> Boolean)

Returns the first value that predicate returns true for or null if nothing matches.

Unit
List<T>.fastForEach(action: (T) -> Unit)

Iterates through a List using the index and calls action for each item.

Unit
List<T>.fastForEachIndexed(action: (Int, T) -> Unit)

Iterates through a List using the index and calls action for each item.

List<R>
List<T>.fastMap(transform: (T) -> R)

Returns a list containing the results of applying the given transform function to each element in the original collection.

C
List<T>.fastMapTo(destination: C, transform: (T) -> R)

Applies the given transform function to each element of the original collection and appends the results to the given destination.

T?
List<T>.fastMaxBy(selector: (T) -> R)

Returns the first element yielding the largest value of the given function or null if there are no elements.

Int
List<T>.fastSumBy(selector: (T) -> Int)

Returns the sum of all values produced by selector function applied to each element in the list.

From androidx.compose.runtime.kotlin.collections.Collection
SnapshotStateList<T>

Create an instance of MutableList from a collection that is observerable and can be snapshot.

From androidx.compose.runtime.kotlin.collections.Iterable
SnapshotStateMap<K, V>

Create an instance of MutableMap<K, V> from a collection of pairs that is observable and can be snapshot.

Properties
PagedList.Config

Return the Config used to construct this PagedList.

DataSource<*, T>

abstract Boolean

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

open Boolean

Returns whether the list is immutable.

abstract Any?

Return the key for the position passed most recently to loadAround.

Int

Returns the number of items loaded in the PagedList.

Int

Position offset of the data in the list.

open Int

Size of the list, including any placeholders (not-yet-loaded null padding).

Public methods

addWeakCallback

fun addWeakCallback(
    previousSnapshot: List<T>?,
    callback: PagedList.Callback
): Unit

Deprecated.

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

addWeakCallback

fun addWeakCallback(callback: PagedList.Callback): Unit

Adds a callback.

The callback is internally held as weak reference, so PagedList doesn't hold a strong reference to its observer, such as a androidx.paging.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
callback: PagedList.Callback Callback to dispatch to.

addWeakLoadStateListener

fun addWeakLoadStateListener(listener: (LoadType, LoadState) -> Unit): Unit

Add a listener to observe the loading state of the PagedList.

Parameters
listener: (LoadType, LoadState) -> Unit Listener to receive updates.

detach

abstract fun detach(): Unit

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

This is called automatically when a PagingSource is observed to be invalid, which is a signal to stop loading. The PagedList will continue to present existing data, but will not initiate new loads.

get

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

See Also

loadAround

fun loadAround(index: Int): Unit

Load adjacent items to passed index.

Parameters
index: Int Index at which to load.
Exceptions
IndexOutOfBoundsException if index is not within bounds.

removeWeakCallback

fun removeWeakCallback(callback: PagedList.Callback): Unit

Removes a previously added callback.

Parameters
callback: PagedList.Callback Callback, previously added.

See Also

removeWeakLoadStateListener

fun removeWeakLoadStateListener(listener: (LoadType, LoadState) -> Unit): Unit

Remove a previously registered load state listener.

Parameters
listener: (LoadType, LoadState) -> Unit Previously registered listener.

retry

open fun retry(): Unit

Retry any errors associated with this PagedList.

If for example a network PagingSource append timed out, calling this method will retry the failed append load.

You can observe loading state via addWeakLoadStateListener, though generally this is done through the PagedListAdapter or AsyncPagedListDiffer.

snapshot

fun snapshot(): List<T>

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

If this PagedListisImmutable due to its PagingSource being invalid, it will be returned.

Return
Immutable snapshot of PagedList data.

Properties

config

val config: PagedList.Config

Return the Config used to construct this PagedList.

Return
the Config of this PagedList

dataSource

val dataSource: DataSource<*, T>

Deprecated.

Exceptions
IllegalStateException if this PagedList was instantiated without a wrapping a backing DataSource

isDetached

abstract val isDetached: Boolean

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

A detached list is immutable.

Return
true if the data source is detached.

isImmutable

open val 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 PagingSource. Currently, it is equivalent to isDetached.

Return