Google 致力于为黑人社区推动种族平等。查看具体举措

PagingDataAdapter

class PagingDataAdapter<T : Any> : ObjectAdapter
kotlin.Any
   ↳ androidx.leanback.widget.ObjectAdapter
   ↳ androidx.leanback.paging.PagingDataAdapter

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

Summary

Public constructors
<init>(diffCallback: DiffUtil.ItemCallback<T>, mainDispatcher: CoroutineDispatcher = Dispatchers.Main, workerDispatcher: CoroutineDispatcher = Dispatchers.Default)

Constructs an adapter

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

Constructs an adapter

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

Constructs an adapter

Public methods
Unit
addDataRefreshListener(listener: (isEmpty: Boolean) -> Unit)

Add a listener to observe new PagingData generations.

Unit

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

T?
get(position: Int)

Returns the item for the given position.

Unit

Refresh the data presented by this PagingDataAdapter.

Unit
removeDataRefreshListener(listener: (isEmpty: Boolean) -> Unit)

Remove a previously registered listener for new PagingData generations.

Unit

Remove a previously registered CombinedLoadStates listener.

Unit

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

Int

Returns the number of items in the adapter.

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.

Inherited functions
Properties
Flow<Boolean>

A Flow of Boolean that is emitted when new PagingData generations are submitted and displayed.

Flow<CombinedLoadStates>

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

Public constructors

<init>

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

<init>

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

<init>

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 methods

addDataRefreshListener

fun addDataRefreshListener(listener: (isEmpty: Boolean) -> Unit): Unit

Add a listener to observe new PagingData generations.

Parameters
listener: (isEmpty: Boolean) -> Unit called whenever a new PagingData is submitted and displayed. true is passed to the listener if the new PagingData is empty, false otherwise.

addLoadStateListener

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

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.

refresh

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.

removeDataRefreshListener

fun removeDataRefreshListener(listener: (isEmpty: Boolean) -> Unit): Unit

Remove a previously registered listener for new PagingData generations.

Parameters
listener: (isEmpty: Boolean) -> Unit Previously registered listener.

removeLoadStateListener

fun removeLoadStateListener(listener: (CombinedLoadStates) -> Unit): Unit

Remove a previously registered CombinedLoadStates listener.

Parameters
listener: (CombinedLoadStates) -> Unit Previously registered listener.

retry

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

fun size(): Int

Returns the number of items in the adapter.

submitData

suspend fun submitData(pagingData: PagingData<T>): Unit

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

submitData should be called on the same CoroutineDispatcher where updates will be dispatched to UI, typically Dispatchers.Main (this is done for you if you use lifecycleScope.launch {}).

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

submitData

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

Properties

dataRefreshFlow

val dataRefreshFlow: Flow<Boolean>

A Flow of Boolean that is emitted when new PagingData generations are submitted and displayed. The Boolean that is emitted is true if the new PagingData is empty, false otherwise.

loadStateFlow

val loadStateFlow: Flow<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.