@MainThread public abstract class Adapter<E,VH extends RecyclerView.ViewHolder> extends <any> implements DatasourceSwappable<E>
RecyclerView.Adapter that holds a reference to the Datasource object.
Notice that you should always detach adapter from RecyclerView when view is destroyed to avoid possible memory leaks. Detaching the adapter from RecyclerView guarantees that all ViewHolders will be recycled. Please see a code snippet below:
| Constructor and Description |
|---|
Adapter() |
Adapter(Datasource<? extends E> datasource) |
Adapter(Datasource<? extends E> datasource,
DiffCallbackFactory<E> diffCallbackFactory) |
| Modifier and Type | Method and Description |
|---|---|
Datasource<? extends E> |
getDatasource() |
int |
getItemCount() |
void |
onAttachedToRecyclerView(RecyclerView recyclerView) |
abstract void |
onBindViewHolder(VH holder,
E item,
int position)
Called by RecyclerView to display the data at the specified position.
|
void |
onBindViewHolder(VH holder,
E item,
int position,
List<Object> payloads)
Called by RecyclerView to display the data at the specified position.
|
void |
onBindViewHolder(VH holder,
int position) |
void |
onBindViewHolder(VH holder,
int position,
List<Object> payloads) |
abstract VH |
onCreateViewHolder(LayoutInflater inflater,
ViewGroup parent,
int viewType)
Called when RecyclerView needs a new
RecyclerView.ViewHolder of the given type to represent
an item. |
VH |
onCreateViewHolder(ViewGroup parent,
int viewType) |
void |
onDetachedFromRecyclerView(RecyclerView recyclerView) |
Datasource<? extends E> |
swap(Datasource<? extends E> datasource)
Performs swapping the internal components and returns the previous component.
|
public Adapter()
public Adapter(@NonNull Datasource<? extends E> datasource)
public Adapter(@NonNull Datasource<? extends E> datasource, @Nullable DiffCallbackFactory<E> diffCallbackFactory)
@CallSuper public void onAttachedToRecyclerView(RecyclerView recyclerView)
@CallSuper public void onDetachedFromRecyclerView(RecyclerView recyclerView)
@Nullable public final Datasource<? extends E> swap(@NonNull Datasource<? extends E> datasource)
swap in interface Swappable<Datasource<? extends E>>datasource - The new component that should replace the old one.@NonNull public Datasource<? extends E> getDatasource()
public int getItemCount()
@NonNull public abstract VH onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent, int viewType)
RecyclerView.ViewHolder of the given type to represent
an item.
This new ViewHolder should be constructed with a new View that can represent the items of the given type. You can either create a new View manually or inflate it from an XML layout file.
The new ViewHolder will be used to display items of the adapter using
onBindViewHolder(RecyclerView.ViewHolder, int, List). Since it will be re-used to display
different items in the data set, it is a good idea to cache references to sub views of
the View to avoid unnecessary View.findViewById(int) calls.
inflater - LayoutInflater to inflate views from XML.parent - The ViewGroup into which the new View will be added after it is bound to
an adapter position.viewType - The view type of the new View.#getItemViewType(int),
onBindViewHolder(RecyclerView.ViewHolder, int)public final void onBindViewHolder(VH holder, int position)
public abstract void onBindViewHolder(@NonNull VH holder, @NonNull E item, int position)
RecyclerView.ViewHolder#itemView to reflect the item at the given
position.
Note that unlike ListView, RecyclerView will not call this method
again if the position of the item changes in the data set unless the item itself is
invalidated or the new position cannot be determined. For this reason, you should only
use the position parameter while acquiring the related data item inside
this method and should not keep a copy of it. If you need the position of an item later
on (e.g. in a click listener), use RecyclerView.ViewHolder#getAdapterPosition() which will
have the updated adapter position.
holder - The ViewHolder which should be updated to represent the contents of the
item at the given position in the data set.item - The item at the given position in the data set.position - The position of the item within the adapter's data set.public void onBindViewHolder(@NonNull VH holder, @NonNull E item, int position, List<Object> payloads)
RecyclerView.ViewHolder#itemView to reflect
the item at the given position.
Note that unlike ListView, RecyclerView will not call this method
again if the position of the item changes in the data set unless the item itself is
invalidated or the new position cannot be determined. For this reason, you should only
use the position parameter while acquiring the related data item inside
this method and should not keep a copy of it. If you need the position of an item later
on (e.g. in a click listener), use RecyclerView.ViewHolder#getAdapterPosition()
which will have the updated adapter position.
Partial bind vs full bind:
The payloads parameter is a merge list from #notifyItemChanged(int, Object) or
#notifyItemRangeChanged(int, int, Object). If the payloads list is not empty,
the ViewHolder is currently bound to old data and Adapter may run an efficient partial
update using the payload info. If the payload is empty, Adapter must run a full bind.
Adapter should not assume that the payload passed in notify methods will be received by
onAttachViewHolder(). For example when the view is not attached to the screen, the
payload in notifyItemChange() will be simply dropped.
holder - The ViewHolder which should be updated to represent the contents of the
item at the given position in the data set.position - The position of the item within the adapter's data set.payloads - A non-null list of merged payloads. Can be empty list if requires full
update.