PagingDataAdapter


public final class PagingDataAdapter<T extends Object> extends ObjectAdapter


An ObjectAdapter implemented with an AsyncPagingDataDiffer. It is an analogue of androidx.paging.PagingDataAdapter for leanback widgets.

Parameters
<T extends Object>

Type of the item in the list.

Summary

Public constructors

<T extends Object> PagingDataAdapter(
    @NonNull DiffUtil.ItemCallback<@NonNull T> diffCallback,
    @NonNull CoroutineDispatcher mainDispatcher,
    @NonNull CoroutineDispatcher workerDispatcher
)

Constructs an adapter

<T extends Object> PagingDataAdapter(
    @NonNull Presenter presenter,
    @NonNull DiffUtil.ItemCallback<@NonNull T> diffCallback,
    @NonNull CoroutineDispatcher mainDispatcher,
    @NonNull CoroutineDispatcher workerDispatcher
)

Constructs an adapter

<T extends Object> PagingDataAdapter(
    @NonNull PresenterSelector presenterSelector,
    @NonNull DiffUtil.ItemCallback<@NonNull T> diffCallback,
    @NonNull CoroutineDispatcher mainDispatcher,
    @NonNull CoroutineDispatcher workerDispatcher
)

Constructs an adapter

Public methods

final void
addLoadStateListener(
    @NonNull Function1<@NonNull CombinedLoadStatesUnit> listener
)

Add a CombinedLoadStates listener to observe the loading state of the current PagingData.

T
get(int position)

Returns the item for the given position.

final @NonNull Flow<@NonNull CombinedLoadStates>

A hot Flow of CombinedLoadStates that emits a snapshot whenever the loading state of the current PagingData changes.

final T
peek(@IntRange(from = 0) int index)

Returns the presented item at the specified position, without notifying Paging of the item access that would normally trigger page loads.

final void

Refresh the data presented by this PagingDataAdapter.

final void

Remove a previously registered CombinedLoadStates listener.

final void

Retry any failed load requests that would result in a LoadState.Error update to this

int
final @NonNull ItemSnapshotList<@NonNull T>

Returns a new ItemSnapshotList representing the currently presented items, including any placeholders if they are enabled.

final void

Present a PagingData until it is invalidated by a call to refresh or PagingSource.invalidate.

final void
submitData(
    @NonNull Lifecycle lifecycle,
    @NonNull PagingData<@NonNull T> pagingData
)

Present a PagingData until it is either invalidated or another call to submitData is made.

Inherited methods

From androidx.leanback.widget.ObjectAdapter
long
getId(int position)

Returns the id for the given position.

final @Nullable Presenter

Returns the Presenter for the given item from the adapter.

final @NonNull PresenterSelector

Returns the presenter selector for this ObjectAdapter.

final boolean

Returns true if the item ids are stable across changes to the underlying data.

boolean

Returns true if the adapter pairs each underlying data change with a call to notify and false otherwise.

final void

Notifies UI that the underlying data has changed.

final void
notifyItemMoved(int fromPosition, int toPosition)

Notifies UI that item at fromPosition has been moved to toPosition.

final void
notifyItemRangeChanged(int positionStart, int itemCount)

Notifies UI that some items has changed.

final void
notifyItemRangeChanged(
    int positionStart,
    int itemCount,
    @Nullable Object payload
)

Notifies UI that some items has changed.

final void
notifyItemRangeInserted(int positionStart, int itemCount)

Notifies UI that new items has been inserted.

final void
notifyItemRangeRemoved(int positionStart, int itemCount)

Notifies UI that some items that has been removed.

void

Called when setHasStableIds is called and the status of stable ids has changed.

void

Called when setPresenterSelector is called and the PresenterSelector differs from the previous one.

final void

Registers a DataObserver for data change notifications.

final void
setHasStableIds(boolean hasStableIds)

Sets whether the item ids are stable across changes to the underlying data.

final void

Sets the presenter selector.

final void

Unregisters all DataObservers for this ObjectAdapter.

final void

Unregisters a DataObserver for data change notifications.

Public constructors

PagingDataAdapter

public <T extends Object> PagingDataAdapter(
    @NonNull DiffUtil.ItemCallback<@NonNull T> diffCallback,
    @NonNull CoroutineDispatcher mainDispatcher,
    @NonNull CoroutineDispatcher workerDispatcher
)

Constructs an adapter

Parameters
@NonNull DiffUtil.ItemCallback<@NonNull T> diffCallback

The DiffUtil.ItemCallback instance to compare items in the list.

@NonNull CoroutineDispatcher mainDispatcher

The CoroutineDispatcher to be used for foreground operations

@NonNull CoroutineDispatcher workerDispatcher

The CoroutineDispatcher to be used for computing diff

PagingDataAdapter

public <T extends Object> PagingDataAdapter(
    @NonNull Presenter presenter,
    @NonNull DiffUtil.ItemCallback<@NonNull T> diffCallback,
    @NonNull CoroutineDispatcher mainDispatcher,
    @NonNull CoroutineDispatcher workerDispatcher
)

Constructs an adapter

Parameters
@NonNull Presenter presenter

Presenter

@NonNull DiffUtil.ItemCallback<@NonNull T> diffCallback

The DiffUtil.ItemCallback instance to compare items in the list.

@NonNull CoroutineDispatcher mainDispatcher

The CoroutineDispatcher to be used for foreground operations

@NonNull CoroutineDispatcher workerDispatcher

The CoroutineDispatcher to be used for computing diff

PagingDataAdapter

public <T extends Object> PagingDataAdapter(
    @NonNull PresenterSelector presenterSelector,
    @NonNull DiffUtil.ItemCallback<@NonNull T> diffCallback,
    @NonNull CoroutineDispatcher mainDispatcher,
    @NonNull CoroutineDispatcher workerDispatcher
)

Constructs an adapter

Parameters
@NonNull PresenterSelector presenterSelector

PresenterSelector

@NonNull DiffUtil.ItemCallback<@NonNull T> diffCallback

The DiffUtil.ItemCallback instance to compare items in the list.

@NonNull CoroutineDispatcher mainDispatcher

The CoroutineDispatcher to be used for foreground operations

@NonNull CoroutineDispatcher workerDispatcher

The CoroutineDispatcher to be used for computing diff

Public methods

addLoadStateListener

Added in 1.1.0-alpha11
public final void addLoadStateListener(
    @NonNull Function1<@NonNull CombinedLoadStatesUnit> listener
)

Add a CombinedLoadStates listener to observe the loading state of the current PagingData.

As new PagingData generations are submitted and displayed, the listener will be notified to reflect the current CombinedLoadStates.

Parameters
@NonNull Function1<@NonNull CombinedLoadStatesUnit> listener

CombinedLoadStates listener to receive updates.

get

Added in 1.1.0-alpha11
public T get(int position)

Returns the item for the given position. It will return null if placeholders are enabled and data is not yet loaded.

getLoadStateFlow

Added in 1.1.0-alpha11
public final @NonNull Flow<@NonNull CombinedLoadStatesgetLoadStateFlow()

A hot Flow of CombinedLoadStates that emits a snapshot whenever the loading state of the current PagingData changes.

This flow is conflated, so it buffers the last update to CombinedLoadStates and immediately delivers the current load states on collection.

peek

Added in 1.1.0-alpha11
public final T peek(@IntRange(from = 0) int index)

Returns the presented item at the specified position, without notifying Paging of the item access that would normally trigger page loads.

Parameters
@IntRange(from = 0) int index

Index of the presented item to return, including placeholders.

Returns
T

The presented item at position index, null if it is a placeholder.

refresh

Added in 1.1.0-alpha11
public final void refresh()

Refresh the data presented by this PagingDataAdapter.

refresh triggers the creation of a new PagingData with a new instance of PagingSource to represent an updated snapshot of the backing dataset. If a RemoteMediator is set, calling refresh will also trigger a call to RemoteMediator.load with LoadType REFRESH] to allow RemoteMediator to check for updates to the dataset backing PagingSource.

Note: This API is intended for UI-driven refresh signals, such as swipe-to-refresh. Invalidation due repository-layer signals, such as DB-updates, should instead use PagingSource.invalidate.

See also
invalidate

removeLoadStateListener

Added in 1.1.0-alpha11
public final void removeLoadStateListener(
    @NonNull Function1<@NonNull CombinedLoadStatesUnit> listener
)

Remove a previously registered CombinedLoadStates listener.

Parameters
@NonNull Function1<@NonNull CombinedLoadStatesUnit> listener

Previously registered listener.

retry

Added in 1.1.0-alpha11
public final void retry()

Retry any failed load requests that would result in a LoadState.Error update to this

PagingDataAdapter.

LoadState.Error can be generated from two types of load requests: PagingSource.load returning PagingSource.LoadResult.Error returning RemoteMediator.MediatorResult.Error

size

Added in 1.1.0-alpha11
public int size()
Returns
int

Total number of presented items, including placeholders.

snapshot

Added in 1.1.0-alpha11
public final @NonNull ItemSnapshotList<@NonNull T> snapshot()

Returns a new ItemSnapshotList representing the currently presented items, including any placeholders if they are enabled.

submitData

Added in 1.1.0-alpha11
public final void submitData(@NonNull PagingData<@NonNull T> pagingData)

Present a PagingData until it is invalidated by a call to refresh or PagingSource.invalidate.

This method is typically used when collecting from a Flow produced by Pager. For RxJava or LiveData support, use the non-suspending overload of submitData, which accepts a Lifecycle.

Note: This method suspends while it is actively presenting page loads from a PagingData, until the PagingData is invalidated. Although cancellation will propagate to this call automatically, collecting from a Pager.flow with the intention of presenting the most up-to-date representation of your backing dataset should typically be done using collectLatest.

See also
Pager

submitData

Added in 1.1.0-alpha11
public final void submitData(
    @NonNull Lifecycle lifecycle,
    @NonNull PagingData<@NonNull T> pagingData
)

Present a PagingData until it is either invalidated or another call to submitData is made.

This method is typically used when observing a RxJava or LiveData stream produced by Pager. For Flow support, use the suspending overload of submitData, which automates cancellation via CoroutineScope instead of relying of Lifecycle.

See also
submitData
Pager