PagingDataAdapter


class PagingDataAdapter<T : Any> : ObjectAdapter


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

Parameters
<T : Any>

Type of the item in the list.

Summary

Public constructors

<T : Any> PagingDataAdapter(
    diffCallback: DiffUtil.ItemCallback<T>,
    mainDispatcher: CoroutineDispatcher,
    workerDispatcher: CoroutineDispatcher
)

Constructs an adapter

<T : Any> PagingDataAdapter(
    presenter: Presenter,
    diffCallback: DiffUtil.ItemCallback<T>,
    mainDispatcher: CoroutineDispatcher,
    workerDispatcher: CoroutineDispatcher
)

Constructs an adapter

<T : Any> PagingDataAdapter(
    presenterSelector: PresenterSelector,
    diffCallback: DiffUtil.ItemCallback<T>,
    mainDispatcher: CoroutineDispatcher,
    workerDispatcher: CoroutineDispatcher
)

Constructs an adapter

Public functions

Unit

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

open operator T?
get(position: Int)

Returns the item for the given position.

T?
peek(index: @IntRange(from = 0) Int)

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

Unit

Refresh the data presented by this PagingDataAdapter.

Unit

Remove a previously registered CombinedLoadStates listener.

Unit

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

open Int
ItemSnapshotList<T>

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

suspend Unit
submitData(pagingData: PagingData<T>)

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

Unit
submitData(lifecycle: Lifecycle, pagingData: PagingData<T>)

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

Public properties

Flow<CombinedLoadStates>

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

Inherited functions

From androidx.leanback.widget.ObjectAdapter
open Long
getId(position: Int)

Returns the id for the given position.

Presenter?

Returns the Presenter for the given item from the adapter.

PresenterSelector

Returns the presenter selector for this ObjectAdapter.

Boolean

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

open Boolean

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

Unit

Notifies UI that the underlying data has changed.

Unit
notifyItemMoved(fromPosition: Int, toPosition: Int)

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

Unit
notifyItemRangeChanged(positionStart: Int, itemCount: Int)

Notifies UI that some items has changed.

Unit
notifyItemRangeChanged(positionStart: Int, itemCount: Int, payload: Any?)

Notifies UI that some items has changed.

Unit
notifyItemRangeInserted(positionStart: Int, itemCount: Int)

Notifies UI that new items has been inserted.

Unit
notifyItemRangeRemoved(positionStart: Int, itemCount: Int)

Notifies UI that some items that has been removed.

open Unit

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

open Unit

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

Unit

Registers a DataObserver for data change notifications.

Unit
setHasStableIds(hasStableIds: Boolean)

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

Unit

Sets the presenter selector.

Unit

Unregisters all DataObservers for this ObjectAdapter.

Unit

Unregisters a DataObserver for data change notifications.

Public constructors

PagingDataAdapter

<T : Any> PagingDataAdapter(
    diffCallback: DiffUtil.ItemCallback<T>,
    mainDispatcher: CoroutineDispatcher = Dispatchers.Main,
    workerDispatcher: CoroutineDispatcher = Dispatchers.Default
)

Constructs an adapter

Parameters
diffCallback: DiffUtil.ItemCallback<T>

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

mainDispatcher: CoroutineDispatcher = Dispatchers.Main

The CoroutineDispatcher to be used for foreground operations

workerDispatcher: CoroutineDispatcher = Dispatchers.Default

The CoroutineDispatcher to be used for computing diff

PagingDataAdapter

<T : Any> PagingDataAdapter(
    presenter: Presenter,
    diffCallback: DiffUtil.ItemCallback<T>,
    mainDispatcher: CoroutineDispatcher = Dispatchers.Main,
    workerDispatcher: CoroutineDispatcher = Dispatchers.Default
)

Constructs an adapter

Parameters
presenter: Presenter

Presenter

diffCallback: DiffUtil.ItemCallback<T>

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

mainDispatcher: CoroutineDispatcher = Dispatchers.Main

The CoroutineDispatcher to be used for foreground operations

workerDispatcher: CoroutineDispatcher = Dispatchers.Default

The CoroutineDispatcher to be used for computing diff

PagingDataAdapter

<T : Any> PagingDataAdapter(
    presenterSelector: PresenterSelector,
    diffCallback: DiffUtil.ItemCallback<T>,
    mainDispatcher: CoroutineDispatcher = Dispatchers.Main,
    workerDispatcher: CoroutineDispatcher = Dispatchers.Default
)

Constructs an adapter

Parameters
presenterSelector: PresenterSelector

PresenterSelector

diffCallback: DiffUtil.ItemCallback<T>

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

mainDispatcher: CoroutineDispatcher = Dispatchers.Main

The CoroutineDispatcher to be used for foreground operations

workerDispatcher: CoroutineDispatcher = Dispatchers.Default

The CoroutineDispatcher to be used for computing diff

Public functions

addLoadStateListener

Added in 1.1.0-alpha11
fun addLoadStateListener(listener: (CombinedLoadStates) -> Unit): Unit

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
listener: (CombinedLoadStates) -> Unit

CombinedLoadStates listener to receive updates.

get

Added in 1.1.0-alpha11
open operator fun get(position: Int): T?

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

peek

Added in 1.1.0-alpha11
fun peek(index: @IntRange(from = 0) Int): T?

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

Parameters
index: @IntRange(from = 0) Int

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
fun refresh(): Unit

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
fun removeLoadStateListener(listener: (CombinedLoadStates) -> Unit): Unit

Remove a previously registered CombinedLoadStates listener.

Parameters
listener: (CombinedLoadStates) -> Unit

Previously registered listener.

retry

Added in 1.1.0-alpha11
fun retry(): Unit

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
open fun size(): Int
Returns
Int

Total number of presented items, including placeholders.

snapshot

Added in 1.1.0-alpha11
fun snapshot(): ItemSnapshotList<T>

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

submitData

Added in 1.1.0-alpha11
suspend fun submitData(pagingData: PagingData<T>): Unit

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
fun submitData(lifecycle: Lifecycle, pagingData: PagingData<T>): Unit

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

Public properties

loadStateFlow

Added in 1.1.0-alpha11
val loadStateFlowFlow<CombinedLoadStates>

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.