# AsyncPagedListDiffer

Summary:[Ctors](https://developer.android.com/reference/android/arch/paging/AsyncPagedListDiffer#pubctors)\|[Methods](https://developer.android.com/reference/android/arch/paging/AsyncPagedListDiffer#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/arch/paging/AsyncPagedListDiffer#inhmethods)  

# AsyncPagedListDiffer

| 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.AsyncPagedListDiffer](https://developer.android.com/reference/androidx/paging/AsyncPagedListDiffer)instead.

`
public


class
AsyncPagedListDiffer
`  
`

extends Object


``


`

|---|-----------------------------------------------|
| java.lang.Object                                 ||
| ↳ | android.arch.paging.AsyncPagedListDiffer\<T\> |

<br />

*** ** * ** ***

Helper object for mapping a[PagedList](https://developer.android.com/reference/android/arch/paging/PagedList)into a[RecyclerView.Adapter](https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter).

For simplicity, the[PagedListAdapter](https://developer.android.com/reference/android/arch/paging/PagedListAdapter)wrapper class can often be used instead of the differ directly. This diff class is exposed for complex cases, and where overriding an adapter base class to support paging isn't convenient.

When consuming a[LiveData](https://developer.android.com/reference/android/arch/lifecycle/LiveData)of PagedList, you can observe updates and dispatch them directly to[submitList(PagedList)](https://developer.android.com/reference/android/arch/paging/AsyncPagedListDiffer#submitList(android.arch.paging.PagedList<T>)). The AsyncPagedListDiffer then can present this updating data set 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)](https://developer.android.com/reference/android/arch/paging/AsyncPagedListDiffer#getItem(int))and[getItemCount()](https://developer.android.com/reference/android/arch/paging/AsyncPagedListDiffer#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 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);
         final UserAdapter adapter = new UserAdapter();
         viewModel.usersList.observe(this, pagedList -> adapter.submitList(pagedList));
         recyclerView.setAdapter(adapter);
     }
 }

 class UserAdapter extends RecyclerView.Adapter<UserViewHolder> {
     private final AsyncPagedListDiffer<User> mDiffer
             = new AsyncPagedListDiffer(this, DIFF_CALLBACK);
     @Override
     public int getItemCount() {
         return mDiffer.getItemCount();
     }
     public void submitList(PagedList<User> pagedList) {
         mDiffer.submitList(pagedList);
     }
     @Override
     public void onBindViewHolder(UserViewHolder holder, int position) {
         User user = mDiffer.getItem(position);
         if (user != null) {
             holder.bindTo(user);
         } else {
             // Null defines a placeholder item - AsyncPagedListDiffer 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);
          }
      }
 }
```

<br />

## Summary

|                                                                                                                                                                                                    ### Public constructors                                                                                                                                                                                                    ||
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[AsyncPagedListDiffer](https://developer.android.com/reference/android/arch/paging/AsyncPagedListDiffer#AsyncPagedListDiffer(android.support.v7.widget.RecyclerView.Adapter, android.support.v7.util.DiffUtil.ItemCallback<T>))`(Adapter adapter, ItemCallback<T> diffCallback) ` Convenience for`AsyncPagedListDiffer(new AdapterListUpdateCallback(adapter), new AsyncDifferConfig.Builder<T>(diffCallback).build();` |
| ` `[AsyncPagedListDiffer](https://developer.android.com/reference/android/arch/paging/AsyncPagedListDiffer#AsyncPagedListDiffer(android.support.v7.util.ListUpdateCallback, android.support.v7.recyclerview.extensions.AsyncDifferConfig<T>))`(ListUpdateCallback listUpdateCallback, 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/AsyncPagedListDiffer#getCurrentList())`() ` Returns the PagedList currently being displayed by the differ.                                                                                               |
| ` T`                                                                                       | ` `[getItem](https://developer.android.com/reference/android/arch/paging/AsyncPagedListDiffer#getItem(int))`(int index) ` Get the item from the current PagedList at the specified index.                                                                                                |
| ` int`                                                                                     | ` `[getItemCount](https://developer.android.com/reference/android/arch/paging/AsyncPagedListDiffer#getItemCount())`() ` Get the number of items currently presented by this Differ.                                                                                                      |
| ` void`                                                                                    | ` `[submitList](https://developer.android.com/reference/android/arch/paging/AsyncPagedListDiffer#submitList(android.arch.paging.PagedList<T>))`(`[PagedList](https://developer.android.com/reference/android/arch/paging/PagedList)`<T> pagedList) ` Pass a new PagedList to the differ. |

| ### Inherited methods |
|-----------------------|---|
| 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() `                    | ||

## Public constructors

### AsyncPagedListDiffer

```
AsyncPagedListDiffer (Adapter adapter, 
                ItemCallback<T> diffCallback)
```

Convenience for`AsyncPagedListDiffer(new AdapterListUpdateCallback(adapter),
new AsyncDifferConfig.Builder<T>(diffCallback).build();`

<br />

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

### AsyncPagedListDiffer

```
AsyncPagedListDiffer (ListUpdateCallback listUpdateCallback, 
                AsyncDifferConfig<T> config)
```

<br />

<br />

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

## Public methods

### getCurrentList

```
PagedList<T> getCurrentList ()
```

Returns the PagedList currently being displayed by the differ.

This is not necessarily the most recent list passed to[submitList(PagedList)](https://developer.android.com/reference/android/arch/paging/AsyncPagedListDiffer#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, may be null. <br /> |

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

<br />

|                                                                                      Parameters                                                                                      ||
|---------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `index` | `int`: Index of item to get, must be \>= 0, and \<[getItemCount()](https://developer.android.com/reference/android/arch/paging/AsyncPagedListDiffer#getItemCount()). <br /> |

|                                      Returns                                       ||
|-----|-------------------------------------------------------------------------------|
| `T` | The item, or null, if a null placeholder is at the specified position. <br /> |

### getItemCount

```
int getItemCount ()
```

Get the number of items currently presented by this Differ. This value can be directly returned to[getItemCount()](https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter#getItemCount()).

<br />

|                    Returns                     ||
|-------|-----------------------------------------|
| `int` | Number of items being presented. <br /> |

### submitList

```
void submitList (PagedList<T> pagedList)
```

Pass a new PagedList to the differ.

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](https://developer.android.com/reference/android/support/v7/util/ListUpdateCallback)), and the new PagedList will be swapped in as the[current list](https://developer.android.com/reference/android/arch/paging/AsyncPagedListDiffer#getCurrentList()).

<br />

|                     Parameters                      ||
|-------------|----------------------------------------|
| `pagedList` | `PagedList`: The new PagedList. <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)