AsyncPagingDataDiffer

public final class AsyncPagingDataDiffer<T extends Object>


Helper class for mapping a PagingData into a RecyclerView.Adapter.

For simplicity, PagingDataAdapter can often be used in place of this class. AsyncPagingDataDiffer is exposed for complex cases, and where overriding PagingDataAdapter to support paging isn't convenient.

Summary

Public fields

final int

Get the number of items currently presented by this Differ.

final @NonNull Flow<@NonNull CombinedLoadStates>

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

final @NonNull Flow<Unit>

A hot Flow that emits after the pages presented to the UI are updated, even if the actual items presented don't change.

Public constructors

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

Public methods

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

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

final void
addOnPagesUpdatedListener(@NonNull Function0<Unit> listener)

Add a listener which triggers after the pages presented to the UI are updated, even if the actual items presented don't change.

final @Nullable T
getItem(@IntRange(from = 0) int index)

Get the item from the current PagedList at the specified index.

final @Nullable 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 AsyncPagingDataDiffer.

final void

Remove a previously registered CombinedLoadStates listener.

final void

Remove a previously registered listener for new PagingData generations completing initial load and presenting to the UI.

final void

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

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.

Public fields

itemCount

@NonNull
public final int itemCount

Get the number of items currently presented by this Differ. This value can be directly returned to androidx.recyclerview.widget.RecyclerView.Adapter.getItemCount.

Returns
int

Number of items being presented, including placeholders.

loadStateFlow

@NonNull
public final @NonNull Flow<@NonNull CombinedLoadStatesloadStateFlow

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.

val adapter = UserPagingAdapter()
lifecycleScope.launch {
    adapter.loadStateFlow
        .map { it.refresh }
        .distinctUntilChanged()
        .collectLatest {
            // show a retry button outside the list when refresh hits an error
            retryButton.isVisible = it is LoadState.Error

            // swipeRefreshLayout displays whether refresh is occurring
            swipeRefreshLayout.isRefreshing = it is LoadState.Loading

            // show an empty state over the list when loading initially, before items are loaded
            emptyState.isVisible = it is LoadState.Loading && adapter.itemCount == 0
        }
}

onPagesUpdatedFlow

@NonNull
public final @NonNull Flow<