PagedListAdapter

Added in 2.0.0
Deprecated in 3.0.0

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


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 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

Protected constructors

<T extends Object, VH extends RecyclerView.ViewHolder> PagedListAdapter(
    @NonNull AsyncDifferConfig<@NonNull T> config
)

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

<T extends Object, VH extends RecyclerView.ViewHolder> PagedListAdapter(
    @NonNull DiffUtil.ItemCallback<@NonNull T> diffCallback
)

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

Public methods

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

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

PagedList<@NonNull T>

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

int

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

void

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

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

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

void

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

void
submitList(PagedList<@NonNull T> pagedList)

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

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

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

final @NonNull ConcatAdapter

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

final @NonNull ConcatAdapter

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

final @NonNull ConcatAdapter

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

Protected methods

T
getItem(int position)

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

Inherited methods

From androidx.recyclerview.widget.RecyclerView.Adapter
final void
bindViewHolder(@NonNull VH holder, int position)

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

final @NonNull VH
createViewHolder(@NonNull ViewGroup parent, int viewType)

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

int

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

long
getItemId(int position)

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

int
getItemViewType(int position)

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

final @NonNull RecyclerView.Adapter.StateRestorationPolicy

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

final boolean

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

final boolean

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

final void

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

final void
notifyItemChanged(int position)

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

final void
notifyItemChanged(int position, @Nullable Object payload)

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

final void
notifyItemInserted(int position)

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

final void
notifyItemMoved(int fromPosition, int toPosition)

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

final void
notifyItemRangeChanged(int positionStart, int itemCount)

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

final void
notifyItemRangeChanged(
    int positionStart,
    int itemCount,
    @Nullable Object payload
)

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

final void
notifyItemRangeInserted(int positionStart, int itemCount)

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

final void
notifyItemRangeRemoved(int positionStart, int itemCount)

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

final void
notifyItemRemoved(int position)

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

void

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

abstract void
onBindViewHolder(@NonNull VH holder, int position)

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

void
onBindViewHolder(
    @NonNull VH holder,
    int position,
    @NonNull List<@NonNull Object> payloads
)

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

abstract @NonNull VH
onCreateViewHolder(@NonNull ViewGroup parent, int viewType)

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

void

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

boolean

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

void

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

void

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

void
onViewRecycled(@NonNull VH holder)

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

void

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

void
setHasStableIds(boolean hasStableIds)

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

void

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

void

This method is deprecated. PagedListAdapter is deprecated and has been replaced by PagingDataAdapter

Protected constructors

PagedListAdapter

protected <T extends Object, VH extends RecyclerView.ViewHolder> PagedListAdapter(
    @NonNull AsyncDifferConfig<@NonNull T> config
)

PagedListAdapter

protected <T extends Object, VH extends RecyclerView.ViewHolder> PagedListAdapter(
    @NonNull DiffUtil.ItemCallback<@NonNull T> diffCallback
)

Creates a PagedListAdapter with default threading and androidx.recyclerview.widget.ListUpdateCallback.

Convenience for PagedListAdapter, which uses default threading behavior.

Parameters
@NonNull DiffUtil.ItemCallback<@NonNull T> diffCallback

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

Public methods

addLoadStateListener

Added in 3.0.0
Deprecated in 3.0.0
public void addLoadStateListener(
    @NonNull Function2<@NonNull LoadType, @NonNull LoadStateUnit> 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 LoadStateUnit> listener

Listener to receive LoadState updates.

getCurrentList

Added in 2.0.0
Deprecated in 3.0.0
public PagedList<@NonNull T> getCurrentList()

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
PagedList<@