Google is committed to advancing racial equity for Black communities. See how.

PagedListAdapter

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

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.

Summary

Protected constructors
<init>(diffCallback: DiffUtil.ItemCallback<T>)

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

Public methods
open Unit

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

open Int

open Unit
onCurrentListChanged(currentList: PagedList<T>?)

Called when the current PagedList is updated.

open Unit
onCurrentListChanged(previousList: PagedList<T>?, currentList: PagedList<T>?)

Called when the current PagedList is updated.

open Unit

Remove a previously registered LoadState listener.

open Unit
submitList(pagedList: PagedList<T>?)

Set the new list to be displayed.

open Unit
submitList(pagedList: PagedList<T>?, commitCallback: Runnable?)

Set the new list to be displayed.

ConcatAdapter

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

ConcatAdapter

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

ConcatAdapter

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

Protected methods
open T?
getItem(position: Int)

Inherited functions