PagedListAdapter

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

java.lang.Object
   ↳ android.support.v7.widget.RecyclerView.Adapter<VH extends android.support.v7.widget.RecyclerView.ViewHolder>
     ↳ android.arch.paging.PagedListAdapter<T, VH extends android.support.v7.widget.RecyclerView.ViewHolder>


RecyclerView.Adapter base class for presenting paged data from 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(PagedList) 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<>(
                 userDao.usersByLastName(), /* page size */ 20).build();
     }
 }

 class MyActivity extends AppCompatActivity {
     @Override
     public void onCreate(Bundle savedState) {
         super.onCreate(savedState);
         MyViewModel viewModel = ViewModelProviders.of(this).get(MyViewModel.class);
         RecyclerView recyclerView = findViewById(R.id.user_list);
         UserAdapter<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<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

PagedListAdapter(ItemCallback<T> diffCallback)

Creates a PagedListAdapter with default threading and ListUpdateCallback.

PagedListAdapter(AsyncDifferConfig<T> config)

Public methods

PagedList<T> getCurrentList()

Returns the PagedList currently being displayed by the Adapter.

int getItemCount()
void onCurrentListChanged(PagedList<T> currentList)

Called when the current PagedList is updated.

void submitList(PagedList<T> pagedList)

Set the new list to be displayed.

Protected methods

T getItem(int position)

Inherited methods

Protected constructors

PagedListAdapter

PagedListAdapter (ItemCallback<T> diffCallback)

Creates a PagedListAdapter with default threading and ListUpdateCallback. Convenience for PagedListAdapter(AsyncDifferConfig), which uses default threading behavior.

Parameters
diffCallback ItemCallback: The DiffUtil.ItemCallback instance to compare items in the list.

PagedListAdapter

PagedListAdapter (AsyncDifferConfig<T> config)

Parameters
config AsyncDifferConfig

Public methods

getCurrentList

PagedList<T> getCurrentList ()

Returns the PagedList currently being displayed by the Adapter.

This is not necessarily the most recent list passed to submitList(PagedList), 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<T> The list currently being displayed.

getItemCount

int getItemCount ()

Returns
int

onCurrentListChanged

void onCurrentListChanged (PagedList<T> currentList)

Called when the current PagedList is updated.

This may be dispatched as part of submitList(PagedList) 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
currentList PagedList: new PagedList being displayed, may be null.

submitList

void submitList (PagedList<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
pagedList PagedList: The new list to be displayed.

Protected methods

getItem

T getItem (int position)

Parameters
position int

Returns
T