-
public abstract class ListAdapter<T, VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH>
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 when new lists are available.
A complete usage pattern with Room would look like this:
Advanced users that wish for more control over adapter behavior, or to provide a specific base class should refer to AsyncListDiffer, which provides custom mapping from diff events to adapter positions.{@literal @}
Dao interface UserDao {{@literal @}
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 {{@literal @}
Override public void onCreate(Bundle savedState) { super.onCreate(savedState); MyViewModel viewModel = new ViewModelProvider(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); }{@literal @}
Override public void onBindViewHolder(UserViewHolder holder, int position) { holder.bindTo(getItem(position)); } public static final DiffUtil.ItemCallback<User> DIFF_CALLBACK = new DiffUtil.ItemCallback<User>() {{@literal @}
Override public boolean areItemsTheSame({@literal @}
NonNull User oldUser,{@literal @}
NonNull User newUser) { // User properties may have changed if reloaded from the DB, but ID is fixed return oldUser.getId() == newUser.getId(); }{@literal @}
Override public boolean areContentsTheSame({@literal @}
NonNull User oldUser,{@literal @}
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); } } }
-
-
Method Summary
Modifier and Type Method Description void
submitList(@Nullable() List<T> list)
Submits a new list to be diffed, and displayed. void
submitList(@Nullable() List<T> list, @Nullable() Runnable commitCallback)
Set the new list to be displayed. int
getItemCount()
Returns the total number of items in the data set held by the adapter. List<T>
getCurrentList()
Get the current List - any diffing to present this list has already been computed anddispatched via the ListUpdateCallback. void
onCurrentListChanged(@NonNull() List<T> previousList, @NonNull() List<T> currentList)
Called when the current List is updated. -
Methods inherited from class tds.androidx.recyclerview.widget.RecyclerView.Adapter
bindViewHolder, createViewHolder, findRelativeAdapterPositionIn, getItemId, getItemViewType, getStateRestorationPolicy, hasObservers, hasStableIds, notifyDataSetChanged, notifyItemChanged, notifyItemChanged, notifyItemInserted, notifyItemMoved, notifyItemRangeChanged, notifyItemRangeChanged, notifyItemRangeInserted, notifyItemRangeRemoved, notifyItemRemoved, onAttachedToRecyclerView, onBindViewHolder, onBindViewHolder, onCreateViewHolder, onDetachedFromRecyclerView, onFailedToRecycleView, onViewAttachedToWindow, onViewDetachedFromWindow, onViewRecycled, registerAdapterDataObserver, setHasStableIds, setStateRestorationPolicy, unregisterAdapterDataObserver
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
Method Detail
-
submitList
void submitList(@Nullable() 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, whichwill dispatch Adapter.notifyItem events on the main thread.
- Parameters:
list
- The new list to be displayed.
-
submitList
void submitList(@Nullable() List<T> list, @Nullable() Runnable commitCallback)
Set the new list to be displayed.
If a List is already being displayed, a diff will be computed on a background thread, whichwill dispatch Adapter.notifyItem events on the main thread.
The commit callback can be used to know when the List is committed, but note that itmay not be executed. If List B is submitted immediately after List A, and iscommitted directly, the callback associated with List A will not be run.
- Parameters:
list
- The new list to be displayed.commitCallback
- Optional runnable that is executed when the List is committed, ifit is committed.
-
getItemCount
int getItemCount()
Returns the total number of items in the data set held by the adapter.
-
getCurrentList
@NonNull() List<T> getCurrentList()
Get the current List - any diffing to present this list has already been computed anddispatched via the ListUpdateCallback.
If a
null
List, or no List has been submitted, an empty list will be returned.The returned list may not be mutated - mutations to content must be done through submitList.
-
onCurrentListChanged
void onCurrentListChanged(@NonNull() List<T> previousList, @NonNull() List<T> currentList)
Called when the current List is updated.
If a
null
List is passed to submitList, or no List has beensubmitted, the current List is represented as an empty List.- Parameters:
previousList
- List that was displayed previously.currentList
- new List being displayed, will be empty if{@code null}
was passed tosubmitList.
-
-
-
-