PagedListAdapter

public abstract class PagedListAdapter<T extends Object, VH extends RecyclerView.ViewHolder>


RecyclerView.Adapter base class for presenting paged data from androidx.paging.PagedLists in a RecyclerView.

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

While using a LiveData<PagedList> is an easy way to provide data to the adapter, it isn't required - you can use submitList when new lists are available.

PagedListAdapter listens to PagedList loading callbacks as pages are loaded, and uses DiffUtil on a background thread to compute fine grained updates as new PagedLists are received.

Handles both the internal paging of the list as more data is loaded, and updates in the form of new PagedLists.

A complete usage pattern with Room would look like this:

@Dao
interface UserDao {
@Query("SELECT * FROM user ORDER BY lastName ASC")
public abstract DataSource.Factory<Integer, User> usersByLastName();
}

class MyViewModel extends ViewModel {
public final LiveData<PagedList<User>> usersList;
public MyViewModel(UserDao userDao) {
usersList = new LivePagedListBuilder&lt;>(
userDao.usersByLastName(), /* page size */20).build();
}
}

class MyActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
MyViewModel viewModel = new ViewModelProvider(this).get(MyViewModel.class);
RecyclerView recyclerView = findViewById(R.id.user_list);
UserAdapter&lt;User> adapter = new UserAdapter();
viewModel.usersList.observe(this, pagedList -> adapter.submitList(pagedList));
recyclerView.setAdapter(adapter);
}
}

class UserAdapter extends PagedListAdapter<User, UserViewHolder> {
public UserAdapter() {
super(DIFF_CALLBACK);
}
@Override
public void onBindViewHolder(UserViewHolder holder, int position) {
User user = getItem(position);
if (user != null) {
holder.bindTo(user);
} else {
// Null defines a placeholder item - PagedListAdapter will automatically invalidate
// this row when the actual object is loaded from the database
holder.clear();
}
}
public static final DiffUtil.ItemCallback&lt;User> DIFF_CALLBACK =
new DiffUtil.ItemCallback<User>() {
@Override
public boolean areItemsTheSame(@NonNull User oldUser, @NonNull User newUser) {
// User properties may have changed if reloaded from the DB, but ID is fixed
return oldUser.getId() == newUser.getId();
}
@Override
public boolean areContentsTheSame(@NonNull User oldUser, @NonNull User newUser) {
// NOTE: if you use equals, your object must properly override Object#equals()
// Incorrectly returning false here will result in too many animations.
return oldUser.equals(newUser);
}
}
}

Advanced users that wish for more control over adapter behavior, or to provide a specific base class should refer to AsyncPagedListDiffer, which provides the mapping from paging events to adapter-friendly callbacks.

Parameters
<T extends Object>

Type of the PagedLists this Adapter will receive.

<VH extends RecyclerView.ViewHolder>

A class that extends ViewHolder that will be used by the adapter.

Summary

Public fields

@Nullable PagedList<@NonNull T>

Returns the PagedList currently being displayed by the PagedListAdapter.

Public methods

@NonNull void
addLoadStateListener(
    @NonNull Function2<@NonNull LoadType, @NonNull LoadState, @NonNull Unit> listener
)

Add a LoadState listener to observe the loading state of the current PagedList.

@NonNull int
@NonNull void

This method is deprecated. Use the two argument variant instead.

@NonNull void
onCurrentListChanged(
    @Nullable PagedList<@NonNull T> previousList,
    @Nullable PagedList<@NonNull T> currentList
)

Called when the current PagedList is updated.

@NonNull void
removeLoadStateListener(
    @NonNull Function2<@NonNull LoadType, @NonNull LoadState, @NonNull Unit> listener
)

Remove a previously registered LoadState listener.

@NonNull void

Set the new list to be displayed.

@NonNull void
submitList(
    @Nullable PagedList<@NonNull T> pagedList,
    @Nullable Runnable commitCallback
)

Set the new list to be displayed.

final @NonNull ConcatAdapter

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

final @NonNull ConcatAdapter

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

final @NonNull ConcatAdapter

Create a ConcatAdapter with the provided LoadStateAdapters displaying the LoadType.PREPEND and LoadType.APPENDs as list items at the start and end respectively.

Inherited methods

From class RecyclerView.Adapter
final @NonNull void
bindViewHolder(@NonNull VH p0, @NonNull int p1)
@NonNull boolean
final @NonNull VH
@NonNull int
@NonNull long
getItemId(@NonNull int p0)
@NonNull int
final @NonNull RecyclerView.Adapter.StateRestorationPolicy
final @NonNull boolean
final @NonNull boolean
final @NonNull void
final @NonNull void
final @NonNull void
final @NonNull void
final @NonNull void
notifyItemMoved(@NonNull int p0, @NonNull int p1)
final @NonNull void
notifyItemRangeChanged(@NonNull int p0, @NonNull int p1)
final @NonNull void
notifyItemRangeChanged(@NonNull int p0, @NonNull int p1, @Nullable Object p2)
final @NonNull void
notifyItemRangeInserted(@NonNull int p0, @NonNull int p1)
final @NonNull void
notifyItemRangeRemoved(@NonNull int p0, @NonNull int p1)
final @NonNull void
@NonNull void
abstract @NonNull void
onBindViewHolder(@NonNull VH p0, @NonNull int p1)
@NonNull void
onBindViewHolder(
    @NonNull VH p0,
    @NonNull int p1,
    @NonNull List<@NonNull Object> p2
)
abstract @NonNull VH
@NonNull void
@NonNull boolean
@NonNull void
@NonNull void
@NonNull void
@NonNull void
@NonNull void
setHasStableIds(@NonNull boolean p0)
@NonNull void
@NonNull void

Public fields

currentList

@Nullable
public @Nullable PagedList<@NonNull T> currentList

Returns the PagedList currently being displayed by the PagedListAdapter.

This is not necessarily the most recent list passed to submitList, because a diff is computed asynchronously between the new list and the current list before updating the currentList value. May be null if no PagedList is being presented.

Returns
@Nullable PagedList<@NonNull T>

The list currently being displayed.

Public methods

addLoadStateListener

@NonNull
public @NonNull void addLoadStateListener(
    @NonNull Function2<@NonNull LoadType, @NonNull LoadState, @NonNull Unit> listener
)

Add a LoadState listener to observe the loading state of the current PagedList.

As new PagedLists are submitted and displayed, the listener will be notified to reflect current LoadType.REFRESH, LoadType.PREPEND, and LoadType.APPEND states.

Parameters
@NonNull Function2<@NonNull LoadType, @NonNull LoadState, @NonNull Unit> listener

Listener to receive LoadState updates.

getItemCount

@NonNull
public @NonNull int getItemCount()

onCurrentListChanged

@NonNull
public @NonNull void onCurrentListChanged(@Nullable PagedList<@NonNull T> currentList)

Called when the current PagedList is updated.

This may be dispatched as part of .submitList if a background diff isn't needed (such as when the first list is passed, or the list is cleared). In either case, PagedListAdapter will simply call .notifyItemRangeInserted.

This method will notbe called when the Adapter switches from presenting a PagedList to a snapshot version of the PagedList during a diff. This means you cannot observe each PagedList via this method.

Parameters
@Nullable PagedList<@NonNull T> currentList

new PagedList being displayed, may be null.

onCurrentListChanged

@NonNull
public @NonNull void onCurrentListChanged(
    @Nullable PagedList<@NonNull T> previousList,
    @Nullable PagedList<@NonNull T> currentList
)

Called when the current PagedList is updated.

This may be dispatched as part of .submitList if a background diff isn't needed (such as when the first list is passed, or the list is cleared). In either case, PagedListAdapter will simply call notifyItemRangeInserted/Removed(0, mPreviousSize).

This method will notbe called when the Adapter switches from presenting a PagedList to a snapshot version of the PagedList during a diff. This means you cannot observe each PagedList via this method.

Parameters
@Nullable PagedList<@NonNull T> previousList

PagedList that was previously displayed, may be null.

@Nullable PagedList<@NonNull T> currentList

new PagedList being displayed, may be null.

removeLoadStateListener

@NonNull
public @NonNull void removeLoadStateListener(
    @NonNull Function2<@NonNull LoadType, @NonNull LoadState, @NonNull Unit> listener
)

Remove a previously registered LoadState listener.

Parameters
@NonNull Function2<@NonNull LoadType, @NonNull LoadState, @NonNull Unit> listener

Previously registered listener.

submitList

@NonNull
public @NonNull void submitList(@Nullable PagedList<@NonNull T> pagedList)

Set the new list to be displayed.

If a list is already being displayed, a diff will be computed on a background thread, which will dispatch Adapter.notifyItem events on the main thread.

Parameters
@Nullable PagedList<@NonNull T> pagedList

The new list to be displayed.

submitList

@NonNull
public @NonNull void submitList(
    @Nullable PagedList<@NonNull T> pagedList,
    @Nullable Runnable commitCallback
)

Set the new list to be displayed.

If a list is already being displayed, a diff will be computed on a background thread, which will dispatch Adapter.notifyItem events on the main thread.

The commit callback can be used to know when the PagedList is committed, but note that it may not be executed. If PagedList B is submitted immediately after PagedList A, and is committed directly, the callback associated with PagedList A will not be run.

Parameters
@Nullable PagedList<@NonNull T> pagedList

The new list to be displayed.

@Nullable Runnable commitCallback

Optional runnable that is executed when the PagedList is committed, if it is committed.

withLoadStateFooter

@NonNull
public final @NonNull ConcatAdapter withLoadStateFooter(@NonNull LoadStateAdapter<@NonNull ?> footer)

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

withLoadStateHeader

@NonNull
public final @NonNull ConcatAdapter withLoadStateHeader(@NonNull LoadStateAdapter<@NonNull ?> header)

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

withLoadStateHeaderAndFooter

@NonNull
public final @NonNull ConcatAdapter withLoadStateHeaderAndFooter(
    @NonNull LoadStateAdapter<@NonNull ?> header,
    @NonNull LoadStateAdapter<@NonNull ?> footer
)

Create a ConcatAdapter with the provided LoadStateAdapters displaying the LoadType.PREPEND and LoadType.APPENDs as list items at the start and end respectively.