Item - Type of the item presented within a data set of a subclass of this BaseRecyclerAdapter.VH - Type of the view holder used within a subclass of this BaseRecyclerAdapter.public abstract class BaseRecyclerAdapter<Item,VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> implements DataSetAdapter<Item>
RecyclerView.Adapter that provides API of
DataSetAdapter.| Modifier and Type | Class and Description |
|---|---|
static class |
BaseRecyclerAdapter.SimpleViewHolder
Simple
RecyclerView.ViewHolder implementation used as default
holder for purpose of onCreateViewHolder(ViewGroup, int) method. |
| Modifier and Type | Field and Description |
|---|---|
protected Context |
mContext
Context in which will be this adapter used.
|
protected LayoutInflater |
mLayoutInflater
Layout inflater used to inflate new views for this adapter.
|
protected Resources |
mResources
Application resources that may be used to obtain strings, texts, drawables, ...
|
NO_ID, NO_POSITION| Constructor and Description |
|---|
BaseRecyclerAdapter(Context context)
Creates a new instance of BaseAdapter within the given context.
|
| Modifier and Type | Method and Description |
|---|---|
long |
getItemId(int position)
Return the stable ID for the item at
position. |
protected View |
inflate(int resource,
ViewGroup parent)
Inflates a new view hierarchy from the given xml resource.
|
boolean |
isEmpty()
Returns a boolean flag indicating whether this data set is empty or not.
|
boolean |
isEnabled(int position)
Returns a boolean flag indicating whether an item at the specified position is
enabled or not.
|
protected boolean |
notifyDataSetActionSelected(int action,
int position,
Object payload)
Notifies that the given action has been performed for the specified position.
|
abstract void |
onBindViewHolder(VH viewHolder,
int position)
Called by RecyclerView to display the data at the specified position.
|
abstract VH |
onCreateViewHolder(ViewGroup parent,
int viewType)
Called when RecyclerView needs a new
RecyclerView.ViewHolder of the given type to represent
an item. |
protected boolean |
onDataSetActionSelected(int action,
int position,
Object payload)
Invoked immediately after
notifyDataSetActionSelected(int, int, Object) was called. |
void |
registerOnDataChangeListener(OnDataChangeListener listener)
Registers a callback to be invoked when a data change occurs in this data set.
|
void |
registerOnDataSetActionListener(OnDataSetActionListener listener)
Registers a callback to be invoked when a specific data set action is selected within this
data set.
|
void |
registerOnDataSetListener(OnDataSetListener listener)
Registers a callback to be invoked when a data set event occurs.
|
void |
restoreInstanceState(Parcelable savedState)
Restores the previous state, saved via
DataSetAdapter.saveInstanceState(), of this adapter. |
Parcelable |
saveInstanceState()
Saves the current state of this adapter.
|
void |
unregisterOnDataChangeListener(OnDataChangeListener listener)
Unregisters the given callback from the data change listeners, so it will not receive any
callbacks further.
|
void |
unregisterOnDataSetActionListener(OnDataSetActionListener listener)
Unregisters the given callback from the data set action listeners, so it will not receive any
callbacks further.
|
void |
unregisterOnDataSetListener(OnDataSetListener listener)
Unregisters the given callback from the data set listeners, so it will not receive any
callbacks further.
|
bindViewHolder, createViewHolder, getItemCount, getItemViewType, hasObservers, hasStableIds, notifyDataSetChanged, notifyItemChanged, notifyItemChanged, notifyItemInserted, notifyItemMoved, notifyItemRangeChanged, notifyItemRangeChanged, notifyItemRangeInserted, notifyItemRangeRemoved, notifyItemRemoved, onAttachedToRecyclerView, onBindViewHolder, onDetachedFromRecyclerView, onFailedToRecycleView, onViewAttachedToWindow, onViewDetachedFromWindow, onViewRecycled, registerAdapterDataObserver, setHasStableIds, unregisterAdapterDataObserverclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waithasStableIdsgetItem, getItemCount, hasItemAtprotected final Context mContext
protected final LayoutInflater mLayoutInflater
protected final Resources mResources
public void registerOnDataChangeListener(@NonNull OnDataChangeListener listener)
DataSetregisterOnDataChangeListener in interface DataSet<Item>listener - The desired listener callback to register.DataSet.unregisterOnDataChangeListener(OnDataChangeListener)public void unregisterOnDataChangeListener(@NonNull OnDataChangeListener listener)
DataSetunregisterOnDataChangeListener in interface DataSet<Item>listener - The desired listener callback to unregister.DataSet.registerOnDataChangeListener(OnDataChangeListener)public void registerOnDataSetListener(@NonNull OnDataSetListener listener)
DataSetregisterOnDataSetListener in interface DataSet<Item>listener - The desired listener callback to register.DataSet.unregisterOnDataSetListener(OnDataSetListener)public void unregisterOnDataSetListener(@NonNull OnDataSetListener listener)
DataSetunregisterOnDataSetListener in interface DataSet<Item>listener - The desired listener callback to unregister.DataSet.registerOnDataSetListener(OnDataSetListener)public void registerOnDataSetActionListener(@NonNull OnDataSetActionListener listener)
DataSetregisterOnDataSetActionListener in interface DataSet<Item>listener - The desired listener callback to register.DataSet.unregisterOnDataSetActionListener(OnDataSetActionListener)protected boolean notifyDataSetActionSelected(int action,
int position,
@Nullable
Object payload)
If onDataSetActionSelected(int, int, Object) will not process this call, the registered
OnDataSetActionListeners will be notified.
Note, that invoking this method with 'invalid' position, out of bounds of the current data set, will be ignored.
action - The action that was selected.position - The position for which was the specified action selected.payload - Additional payload data for the selected action. May be null if no
payload has been specified.True if the action has been handled internally by this adapter or by one of
the registers listeners, false otherwise.protected boolean onDataSetActionSelected(int action,
int position,
@Nullable
Object payload)
notifyDataSetActionSelected(int, int, Object) was called.True to indicate that this event was processed here, false to dispatch
this event to the registered OnDataSetActionListeners.public void unregisterOnDataSetActionListener(@NonNull OnDataSetActionListener listener)
DataSetunregisterOnDataSetActionListener in interface DataSet<Item>listener - The desired listener callback to unregister.DataSet.registerOnDataSetActionListener(OnDataSetActionListener)public boolean isEmpty()
DataSetisEmpty in interface DataSet<Item>True if cursor attached to this data set has no rows or it is null,
false otherwise.DataSet.getItemCount()public long getItemId(int position)
android.support.v7.widget.RecyclerView.Adapterposition. If RecyclerView.Adapter.hasStableIds()
would return false this method should return RecyclerView.NO_ID. The default implementation
of this method returns RecyclerView.NO_ID.getItemId in interface DataSet<Item>getItemId in class RecyclerView.Adapter<VH extends RecyclerView.ViewHolder>position - Adapter position to queryDataSet.hasItemAt(int),
DataSet.getItem(int)public boolean isEnabled(int position)
DataSetAdapterisEnabled in interface DataSetAdapter<Item>position - The position of item to check.True if the item is enabled, false otherwise.public abstract VH onCreateViewHolder(ViewGroup parent, int viewType)
android.support.v7.widget.RecyclerView.AdapterRecyclerView.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(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.
onCreateViewHolder in class RecyclerView.Adapter<VH extends RecyclerView.ViewHolder>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.RecyclerView.Adapter.getItemViewType(int),
#onBindViewHolder(ViewHolder, int)@NonNull protected View inflate(@LayoutRes int resource, @NonNull ViewGroup parent)
resource - Resource id of a view to inflate.parent - A parent view, to resolve correct layout params for the newly creating view.LayoutInflater.inflate(int, ViewGroup)public abstract void onBindViewHolder(@NonNull VH viewHolder, int position)
android.support.v7.widget.RecyclerView.AdapterRecyclerView.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.
Override #onBindViewHolder(ViewHolder, int, List) instead if Adapter can
handle efficient partial bind.
onBindViewHolder in class RecyclerView.Adapter<VH extends RecyclerView.ViewHolder>viewHolder - 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.@NonNull @CallSuper public Parcelable saveInstanceState()
DataSetAdapter
If you decide to override this method, do not forget to call super.saveInstanceState()
and pass the obtained super state to the corresponding constructor of your saved state
implementation to ensure the state of all classes along the chain is properly saved.
saveInstanceState in interface DataSetAdapter<Item>@CallSuper public void restoreInstanceState(@NonNull Parcelable savedState)
DataSetAdapterDataSetAdapter.saveInstanceState(), of this adapter.
If you decide to override this method, do not forget to call super.restoreInstanceState(Parcelable)
and pass there the parent state obtained from your saved state implementation to ensure the
state of all classes along the chain is properly restored.
restoreInstanceState in interface DataSetAdapter<Item>savedState - Should be the same state as obtained via DataSetAdapter.saveInstanceState() before.