# PagedListAdapter

Summary:[Protected Ctors](https://developer.android.com/reference/android/arch/paging/PagedListAdapter#proctors)\|[Methods](https://developer.android.com/reference/android/arch/paging/PagedListAdapter#pubmethods)\|[Protected Methods](https://developer.android.com/reference/android/arch/paging/PagedListAdapter#promethods)\|[Inherited Methods](https://developer.android.com/reference/android/arch/paging/PagedListAdapter#inhmethods)  

# PagedListAdapter

| The`android.arch`Architecture Components packages are no longer maintained. They have been superseded by the corresponding[androidx.\*](https://developer.android.com/jetpack/androidx/migrate)packages. See[androidx.paging.PagedListAdapter](https://developer.android.com/reference/androidx/paging/PagedListAdapter)instead.

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

<br />

*** ** * ** ***

[RecyclerView.Adapter](https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter)base class for presenting paged data from[PagedList](https://developer.android.com/reference/android/arch/paging/PagedList)s in a[RecyclerView](https://developer.android.com/reference/android/support/v7/widget/RecyclerView).

This class is a convenience wrapper around[AsyncPagedListDiffer](https://developer.android.com/reference/android/arch/paging/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)](https://developer.android.com/reference/android/arch/paging/PagedListAdapter#submitList(android.arch.paging.PagedList<T>))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](https://developer.android.com/reference/android/arch/paging/AsyncPagedListDiffer), which provides the mapping from paging events to adapter-friendly callbacks.

<br />

## Summary

|                                                                                                                                                                        ### Protected constructors                                                                                                                                                                         ||
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[PagedListAdapter](https://developer.android.com/reference/android/arch/paging/PagedListAdapter#PagedListAdapter(android.support.v7.util.DiffUtil.ItemCallback<T>))`(ItemCallback<T> diffCallback) ` Creates a PagedListAdapter with default threading and[ListUpdateCallback](https://developer.android.com/reference/android/support/v7/util/ListUpdateCallback). |
| ` `[PagedListAdapter](https://developer.android.com/reference/android/arch/paging/PagedListAdapter#PagedListAdapter(android.support.v7.recyclerview.extensions.AsyncDifferConfig<T>))`(AsyncDifferConfig<T> config) `                                                                                                                                                  |

|                                                                                                                                                                                                ### Public methods                                                                                                                                                                                                ||
|--------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` `[PagedList](https://developer.android.com/reference/android/arch/paging/PagedList)`<T>` | ` `[getCurrentList](https://developer.android.com/reference/android/arch/paging/PagedListAdapter#getCurrentList())`() ` Returns the PagedList currently being displayed by the Adapter.                                                                                                                              |
| ` int`                                                                                     | ` `[getItemCount](https://developer.android.com/reference/android/arch/paging/PagedListAdapter#getItemCount())`() `                                                                                                                                                                                                  |
| ` void`                                                                                    | ` `[onCurrentListChanged](https://developer.android.com/reference/android/arch/paging/PagedListAdapter#onCurrentListChanged(android.arch.paging.PagedList<T>))`(`[PagedList](https://developer.android.com/reference/android/arch/paging/PagedList)`<T> currentList) ` Called when the current PagedList is updated. |
| ` void`                                                                                    | ` `[submitList](https://developer.android.com/reference/android/arch/paging/PagedListAdapter#submitList(android.arch.paging.PagedList<T>))`(`[PagedList](https://developer.android.com/reference/android/arch/paging/PagedList)`<T> pagedList) ` Set the new list to be displayed.                                   |

|                                                     ### Protected methods                                                      ||
|------|--------------------------------------------------------------------------------------------------------------------------|
| ` T` | ` `[getItem](https://developer.android.com/reference/android/arch/paging/PagedListAdapter#getItem(int))`(int position) ` |

| ### Inherited methods |
|-----------------------|---|
| From class` android.support.v7.widget.RecyclerView.Adapter ` |------------------|---------------------------------------------------------------------------------------------------------------------------| | ` final void`    | ` bindViewHolder(VH arg0, int arg1) `                                                                                     | | ` final VH`      | ` createViewHolder(`[ViewGroup](https://developer.android.com/reference/android/view/ViewGroup.html)` arg0, int arg1) `   | | ` abstract int`  | ` getItemCount() `                                                                                                        | | ` long`          | ` getItemId(int arg0) `                                                                                                   | | ` int`           | ` getItemViewType(int arg0) `                                                                                             | | ` final boolean` | ` hasObservers() `                                                                                                        | | ` final boolean` | ` hasStableIds() `                                                                                                        | | ` final void`    | ` notifyDataSetChanged() `                                                                                                | | ` final void`    | ` notifyItemChanged(int arg0, Object arg1) `                                                                              | | ` final void`    | ` notifyItemChanged(int arg0) `                                                                                           | | ` final void`    | ` notifyItemInserted(int arg0) `                                                                                          | | ` final void`    | ` notifyItemMoved(int arg0, int arg1) `                                                                                   | | ` final void`    | ` notifyItemRangeChanged(int arg0, int arg1, Object arg2) `                                                               | | ` final void`    | ` notifyItemRangeChanged(int arg0, int arg1) `                                                                            | | ` final void`    | ` notifyItemRangeInserted(int arg0, int arg1) `                                                                           | | ` final void`    | ` notifyItemRangeRemoved(int arg0, int arg1) `                                                                            | | ` final void`    | ` notifyItemRemoved(int arg0) `                                                                                           | | ` void`          | ` onAttachedToRecyclerView(RecyclerView arg0) `                                                                           | | ` abstract void` | ` onBindViewHolder(VH arg0, int arg1) `                                                                                   | | ` void`          | ` onBindViewHolder(VH arg0, int arg1, List<Object> arg2) `                                                                | | ` abstract VH`   | ` onCreateViewHolder(`[ViewGroup](https://developer.android.com/reference/android/view/ViewGroup.html)` arg0, int arg1) ` | | ` void`          | ` onDetachedFromRecyclerView(RecyclerView arg0) `                                                                         | | ` boolean`       | ` onFailedToRecycleView(VH arg0) `                                                                                        | | ` void`          | ` onViewAttachedToWindow(VH arg0) `                                                                                       | | ` void`          | ` onViewDetachedFromWindow(VH arg0) `                                                                                     | | ` void`          | ` onViewRecycled(VH arg0) `                                                                                               | | ` void`          | ` registerAdapterDataObserver(RecyclerView.AdapterDataObserver arg0) `                                                    | | ` void`          | ` setHasStableIds(boolean arg0) `                                                                                         | | ` void`          | ` unregisterAdapterDataObserver(RecyclerView.AdapterDataObserver arg0) `                                                  | ||
| From class` java.lang.Object ` |-------------------|-------------------------------| | ` Object`         | ` clone() `                   | | ` boolean`        | ` equals(Object arg0) `       | | ` void`           | ` finalize() `                | | ` final Class<?>` | ` getClass() `                | | ` int`            | ` hashCode() `                | | ` final void`     | ` notify() `                  | | ` final void`     | ` notifyAll() `               | | ` String`         | ` toString() `                | | ` final void`     | ` wait(long arg0, int arg1) ` | | ` final void`     | ` wait(long arg0) `           | | ` final void`     | ` wait() `                    | ||

## Protected constructors

### PagedListAdapter

```
PagedListAdapter (ItemCallback<T> diffCallback)
```

Creates a PagedListAdapter with default threading and[ListUpdateCallback](https://developer.android.com/reference/android/support/v7/util/ListUpdateCallback). Convenience for[PagedListAdapter(AsyncDifferConfig)](https://developer.android.com/reference/android/arch/paging/PagedListAdapter#PagedListAdapter(android.support.v7.recyclerview.extensions.AsyncDifferConfig<T>)), which uses default threading behavior.

<br />

|                                                                                           Parameters                                                                                           ||
|----------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `diffCallback` | `ItemCallback`: The[DiffUtil.ItemCallback](https://developer.android.com/reference/android/support/v7/util/DiffUtil.ItemCallback)instance to compare items in the list. <br /> |

### PagedListAdapter

```
PagedListAdapter (AsyncDifferConfig<T> config)
```

<br />

<br />

|              Parameters              ||
|----------|----------------------------|
| `config` | `AsyncDifferConfig` <br /> |

## 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)](https://developer.android.com/reference/android/arch/paging/PagedListAdapter#submitList(android.arch.paging.PagedList<T>)), 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.

<br />

|                                                               Returns                                                               ||
|-----------------------------------------------------------------------------------------|--------------------------------------------|
| [PagedList](https://developer.android.com/reference/android/arch/paging/PagedList)`<T>` | The list currently being displayed. <br /> |

### getItemCount

```
int getItemCount ()
```

<br />

<br />

|    Returns    ||
|-------|--------|
| `int` | <br /> |

### onCurrentListChanged

```
void onCurrentListChanged (PagedList<T> currentList)
```

Called when the current PagedList is updated.

This may be dispatched as part of[submitList(PagedList)](https://developer.android.com/reference/android/arch/paging/PagedListAdapter#submitList(android.arch.paging.PagedList<T>))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)](https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter#notifyItemRangeInserted(int, int)).

This method will*not*be 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.

<br />

|                                   Parameters                                   ||
|---------------|-----------------------------------------------------------------|
| `currentList` | `PagedList`: new PagedList being displayed, may be null. <br /> |

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

<br />

|                           Parameters                           ||
|-------------|---------------------------------------------------|
| `pagedList` | `PagedList`: The new list to be displayed. <br /> |

## Protected methods

### getItem

```
T getItem (int position)
```

<br />

<br />

|        Parameters        ||
|------------|--------------|
| `position` | `int` <br /> |

|   Returns   ||
|-----|--------|
| `T` | <br /> |

-

  ## Interfaces

  - [DataSource.InvalidatedCallback](https://developer.android.com/reference/android/arch/paging/DataSource.InvalidatedCallback)
-

  ## Classes

  - [AsyncPagedListDiffer](https://developer.android.com/reference/android/arch/paging/AsyncPagedListDiffer)
  - [DataSource](https://developer.android.com/reference/android/arch/paging/DataSource)
  - [DataSource.Factory](https://developer.android.com/reference/android/arch/paging/DataSource.Factory)
  - [ItemKeyedDataSource](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource)
  - [ItemKeyedDataSource.LoadCallback](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource.LoadCallback)
  - [ItemKeyedDataSource.LoadInitialCallback](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource.LoadInitialCallback)
  - [ItemKeyedDataSource.LoadInitialParams](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource.LoadInitialParams)
  - [ItemKeyedDataSource.LoadParams](https://developer.android.com/reference/android/arch/paging/ItemKeyedDataSource.LoadParams)
  - [LivePagedListBuilder](https://developer.android.com/reference/android/arch/paging/LivePagedListBuilder)
  - [PagedList](https://developer.android.com/reference/android/arch/paging/PagedList)
  - [PagedList.BoundaryCallback](https://developer.android.com/reference/android/arch/paging/PagedList.BoundaryCallback)
  - [PagedList.Builder](https://developer.android.com/reference/android/arch/paging/PagedList.Builder)
  - [PagedList.Callback](https://developer.android.com/reference/android/arch/paging/PagedList.Callback)
  - [PagedList.Config](https://developer.android.com/reference/android/arch/paging/PagedList.Config)
  - [PagedList.Config.Builder](https://developer.android.com/reference/android/arch/paging/PagedList.Config.Builder)
  - [PagedListAdapter](https://developer.android.com/reference/android/arch/paging/PagedListAdapter)
  - [PageKeyedDataSource](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource)
  - [PageKeyedDataSource.LoadCallback](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.LoadCallback)
  - [PageKeyedDataSource.LoadInitialCallback](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.LoadInitialCallback)
  - [PageKeyedDataSource.LoadInitialParams](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.LoadInitialParams)
  - [PageKeyedDataSource.LoadParams](https://developer.android.com/reference/android/arch/paging/PageKeyedDataSource.LoadParams)
  - [PositionalDataSource](https://developer.android.com/reference/android/arch/paging/PositionalDataSource)
  - [PositionalDataSource.LoadInitialCallback](https://developer.android.com/reference/android/arch/paging/PositionalDataSource.LoadInitialCallback)
  - [PositionalDataSource.LoadInitialParams](https://developer.android.com/reference/android/arch/paging/PositionalDataSource.LoadInitialParams)
  - [PositionalDataSource.LoadRangeCallback](https://developer.android.com/reference/android/arch/paging/PositionalDataSource.LoadRangeCallback)
  - [PositionalDataSource.LoadRangeParams](https://developer.android.com/reference/android/arch/paging/PositionalDataSource.LoadRangeParams)
  - [RxPagedListBuilder](https://developer.android.com/reference/android/arch/paging/RxPagedListBuilder)