Skip to content

Most visited

Recently visited

navigation

PagedListAdapterHelper

public class PagedListAdapterHelper
extends Object

java.lang.Object
   ↳ android.arch.paging.PagedListAdapterHelper<T>


Helper object for mapping a PagedList into a RecyclerView.Adapter.

For simplicity, the PagedListAdapter wrapper class can often be used instead of the helper directly. This helper class is exposed for complex cases, and where overriding an adapter base class to support paging isn't convenient.

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

The PagedListAdapterHelper can be bound to a LiveData of PagedList and present the data simply for an adapter. It listens to PagedList loading callbacks, and uses DiffUtil on a background thread to compute updates as new PagedLists are received.

It provides a simple list-like API with getItem(int) and getItemCount() for an adapter to acquire and present data objects.

A complete usage pattern with Room would look like this:

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

 class MyViewModel extends ViewModel {
     public final LiveData<PagedList<User>> usersList;
     public MyViewModel(UserDao userDao) {
         usersList = userDao.usersByLastName().create(
                 /* initial load position */ 0,
                 new PagedList.Config.Builder()
                         .setPageSize(50)
                         .setPrefetchDistance(50)
                         .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);
         final UserAdapter<User> adapter = new UserAdapter();
         viewModel.usersList.observe(this, pagedList -> adapter.setList(pagedList));
         recyclerView.setAdapter(adapter);
     }
 }

 class UserAdapter extends RecyclerView.Adapter<UserViewHolder> {
     private final PagedListAdapterHelper<User> mHelper;
     public UserAdapter(PagedListAdapterHelper.Builder<User> builder) {
         mHelper = new PagedListAdapterHelper(this, DIFF_CALLBACK);
     }
     @Override
     public int getItemCount() {
         return mHelper.getItemCount();
     }
     public void setList(PagedList<User> pagedList) {
         mHelper.setList(pagedList);
     }
     @Override
     public void onBindViewHolder(UserViewHolder holder, int position) {
         User user = mHelper.getItem(position);
         if (user != null) {
             holder.bindTo(user);
         } else {
             // Null defines a placeholder item - PagedListAdapterHelper will automatically
             // invalidate this row when the actual object is loaded from the database
             holder.clear();
         }
     }
     public static final DiffCallback<User> DIFF_CALLBACK = new DiffCallback<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);
          }
      }
 }

Summary

Public constructors

PagedListAdapterHelper(Adapter adapter, DiffCallback<T> diffCallback)

Convenience for {@code PagedListAdapterHelper(new ListAdapterHelper.AdapterCallback(adapter), new ListAdapterConfig.Builder().setDiffCallback(diffCallback).build());

PagedListAdapterHelper(ListUpdateCallback listUpdateCallback, ListAdapterConfig<T> config)

Public methods

PagedList<T> getCurrentList()

Returns the list currently being displayed by the AdapterHelper.

T getItem(int index)

Get the item from the current PagedList at the specified index.

int getItemCount()

Get the number of items currently presented by this AdapterHelper.

void setList(PagedList<T> pagedList)

Pass a new PagedList to the AdapterHelper.

Inherited methods

From class java.lang.Object

Public constructors

PagedListAdapterHelper

PagedListAdapterHelper (Adapter adapter, 
                DiffCallback<T> diffCallback)

Convenience for {@code PagedListAdapterHelper(new ListAdapterHelper.AdapterCallback(adapter), new ListAdapterConfig.Builder().setDiffCallback(diffCallback).build());

Parameters
adapter Adapter: Adapter that will receive update signals.

diffCallback DiffCallback: The DiffCallback instance to compare items in the list.

PagedListAdapterHelper

PagedListAdapterHelper (ListUpdateCallback listUpdateCallback, 
                ListAdapterConfig<T> config)

Parameters
listUpdateCallback ListUpdateCallback

config ListAdapterConfig

Public methods

getCurrentList

PagedList<T> getCurrentList ()

Returns the list currently being displayed by the AdapterHelper.

This is not necessarily the most recent list passed to setList(PagedList), because a diff is computed asynchronously between the new list and the current list before updating the currentList value.

Returns
PagedList<T> The list currently being displayed.

getItem

T getItem (int index)

Get the item from the current PagedList at the specified index.

Note that this operates on both loaded items and null padding within the PagedList.

Parameters
index int: Index of item to get, must be >= 0, and < getItemCount().

Returns
T The item, or null, if a null placeholder is at the specified position.

getItemCount

int getItemCount ()

Get the number of items currently presented by this AdapterHelper. This value can be directly returned to getItemCount().

Returns
int Number of items being presented.

setList

void setList (PagedList<T> pagedList)

Pass a new PagedList to the AdapterHelper.

If a PagedList is already present, a diff will be computed asynchronously on a background thread. When the diff is computed, it will be applied (dispatched to the ListUpdateCallback), and the new PagedList will be swapped in.

Parameters
pagedList PagedList: The new PagedList.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)