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


abstract class PagingDataAdapter<T : Any, VH : RecyclerView.ViewHolder> : RecyclerView.Adapter<VH>
   ↳ androidx.recyclerview.widget.RecyclerView.Adapter<VH>
   ↳ androidx.paging.PagingDataAdapter

RecyclerView.Adapter base class for presenting paged data from PagingDatas in a RecyclerView.

This class is a convenience wrapper around AsyncPagingDataDiffer that implements common default behavior for item counting, and listening to update events.

To present a Pager, use collectLatest to observe Pager.flow and call submitData whenever a new PagingData is emitted.

If using RxJava and LiveData extensions on Pager, use the non-suspending overload of submitData, which accepts a Lifecycle.

PagingDataAdapter listens to internal PagingData loading events as pages are loaded, and uses DiffUtil on a background thread to compute fine grained updates as updated content in the form of new PagingData objects are received.

State Restoration: To be able to restore RecyclerView state (e.g. scroll position) after a configuration change / application recreate, PagingDataAdapter calls RecyclerView.Adapter.setStateRestorationPolicy with RecyclerView.Adapter.StateRestorationPolicy.PREVENT upon initialization and waits for the first page to load before allowing state restoration. Any other call to RecyclerView.Adapter.setStateRestorationPolicy by the application will disable this logic and will rely on the user set value.

val USER_COMPARATOR = object : DiffUtil.ItemCallback<User>() {
    override fun areItemsTheSame(oldItem: User, newItem: User): Boolean =
        // User ID serves as unique ID
        oldItem.userId == newItem.userId

    override fun areContentsTheSame(oldItem: User, newItem: User): Boolean =
        // Compare full contents (note: Java users should call .equals())
        oldItem == newItem

class UserAdapter : PagingDataAdapter<User, UserViewHolder>(USER_COMPARATOR) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder {
        return UserViewHolder.create(parent)

    override fun onBindViewHolder(holder: UserViewHolder, position: Int) {
        val repoItem = getItem(position)
        // Note that item may be null, ViewHolder must support binding null item as placeholder


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

RecyclerView.Adapter base class for presenting paged data from PagingDatas in a RecyclerView.

Public methods

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

open Int

getItemId(position: Int)

Note: getItemId is final, because stable IDs are unnecessary and therefore unsupported.

peek(@IntRange(0) index: Int)

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


Refresh the data presented by this PagingDataAdapter.


Remove a previously registered CombinedLoadStates listener.


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

setHasStableIds(hasStableIds: Boolean)

Stable ids are unsupported by PagingDataAdapter.

open Unit


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.

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

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


Create a ConcatAdapter with the provided LoadStateAdapters displaying the LoadType.APPEND as a list item at the start of the presented list.


Create a ConcatAdapter with the provided LoadStateAdapters displaying the LoadType.PREPEND as a list item at the end of the pre