belongs to Maven artifact com.android.support:recyclerview-v7:28.0.0-alpha1
ListAdapter
public
abstract
class
ListAdapter
extends Adapter<VH extends RecyclerView.ViewHolder>
java.lang.Object | ||
↳ | android.support.v7.widget.RecyclerView.Adapter<VH extends android.support.v7.widget.RecyclerView.ViewHolder> | |
↳ | android.support.v7.recyclerview.extensions.ListAdapter<T, VH extends android.support.v7.widget.RecyclerView.ViewHolder> |
RecyclerView.Adapter
base class for presenting List data in a
RecyclerView
, including computing diffs between Lists on a background thread.
This class is a convenience wrapper around AsyncListDiffer
that implements Adapter common
default behavior for item access and counting.
While using a LiveData<List> is an easy way to provide data to the adapter, it isn't required
- you can use submitList(List)
when new lists are available.
A complete usage pattern with Room would look like this:
@Dao interface UserDao { @Query("SELECT * FROM user ORDER BY lastName ASC") public abstract LiveData<List<User>> usersByLastName(); } class MyViewModel extends ViewModel { public final LiveData<List<User>> usersList; public MyViewModel(UserDao userDao) { usersList = userDao.usersByLastName(); } } 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, list -> adapter.submitList(list)); recyclerView.setAdapter(adapter); } } class UserAdapter extends ListAdapter<User, UserViewHolder> { public UserAdapter() { super(User.DIFF_CALLBACK); } @Override public void onBindViewHolder(UserViewHolder holder, int position) { holder.bindTo(getItem(position)); } 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); } } }
AsyncListDiffer
, which provides custom mapping from diff events
to adapter positions.
Summary
Protected constructors | |
---|---|
ListAdapter(ItemCallback<T> diffCallback)
|
|
ListAdapter(AsyncDifferConfig<T> config)
|
Public methods | |
---|---|
int
|
getItemCount()
Returns the total number of items in the data set held by the adapter. |
void
|
submitList(List<T> list)
Submits a new list to be diffed, and displayed. |
Protected methods | |
---|---|
T
|
getItem(int position)
|
Inherited methods | |
---|---|
From
class
android.support.v7.widget.RecyclerView.Adapter
| |
From
class
java.lang.Object
|
Protected constructors
ListAdapter
ListAdapter (ItemCallback<T> diffCallback)
Parameters | |
---|---|
diffCallback |
ItemCallback |
ListAdapter
ListAdapter (AsyncDifferConfig<T> config)
Parameters | |
---|---|
config |
AsyncDifferConfig |
Public methods
getItemCount
int getItemCount ()
Returns the total number of items in the data set held by the adapter.
Returns | |
---|---|
int |
The total number of items in this adapter. |
submitList
void submitList (List<T> list)
Submits a new list to be diffed, and 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 | |
---|---|
list |
List : The new list to be displayed.
|