Item - Type of the item presented within a data set of a subclass of this BaseAdapter.VH - Type of the view holder used within a subclass of this BaseAdapter.public abstract class BaseAdapter<Item,VH> extends BaseAdapter implements DataSetAdapter<Item>
BaseAdapter. This version of BaseAdapter implements
optimized algorithm for the getView(int, View, ViewGroup) method using the holder pattern
as described below:
public class BaseAdapter extends android.widget.BaseAdapter {
// ...
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Object viewHolder;
// Obtain current item view type.
this.mCurrentViewType = getItemViewType(position);
if (convertView == null) {
// Dispatch to create new view.
convertView = onCreateView(parent, position);
// Resolve holder for the newly created view.
final Object holder = onCreateViewHolder(convertView, position);
if (holder != null) {
convertView.setTag(viewHolder = holder);
} else {
viewHolder = convertView;
}
} else {
final Object holder = convertView.getTag();
viewHolder = holder != null ? holder : convertView;
}
ensureViewHolderPosition(viewHolder, position);
// Dispatch to bind view holder with data.
onBindViewHolder(viewHolder, position);
return convertView;
}
// ...
}
In case when an implementation of this adapter provides item views for data set upon which can be
performed a wide set of actions, like marking items as favorite or deleting them using action button
placed directly within these item views, this adapter provides simple interface which allows to
notify the registered OnDataSetActionListeners via
notifyDataSetActionSelected(int, int, Object) with identifier of the performed action.
This method also allows to pass some additional payload data for the action.
public class SampleAdapter extends BaseAdapter {
// ...
@NonNull
@Override
public Parcelable saveInstanceState() {
final SavedState state = new SavedState(super.saveInstanceState());
// ...
// Pass here all data of this adapter which need to be saved to the state.
// ...
return state;
}
@Override
public void restoreInstanceState(@NonNull Parcelable savedState) {
if (!(savedState instanceof SavedState)) {
// Passed savedState is not our state, let super to process it.
super.restoreInstanceState(savedState);
return;
}
final SavedState state = (SavedState) savedState;
// Pass superState to super to process it.
super.restoreInstanceState(savedState.getSuperState());
// ...
// Set here all data of this adapter which need to be restored from the state.
// ...
}
// ...
// Implementation of AdapterSavedState for this adapter.
static class SavedState extends AdapterSavedState {
// Each implementation of saved state need to have its own CREATOR provided.
public static final Creator<SavedState> CREATOR = new Creator<SavedState>() {
@Override
public SavedState createFromParcel(@NonNull Parcel source) {
return new SavedState(source);
}
@Override
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
// Constructor used to chain the state of inheritance hierarchies.
SavedState(@NonNull Parcelable superState) {
super(superState);
}
SavedState(@NonNull Parcel source) {
super(source);
// Restore here state's data.
}
@Override
public void writeToParcel(@NonNull Parcel dest, int flags) {
super.writeToParcel(dest, flags);
// Save here state's data.
}
}
}
| 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_POSITIONIGNORE_ITEM_VIEW_TYPE, NO_SELECTION| Constructor and Description |
|---|
BaseAdapter(Context context)
Creates a new instance of BaseAdapter within the given context.
|
| Modifier and Type | Method and Description |
|---|---|
protected int |
currentViewType()
Returns the type of an item's view for the currently iterated position.
|
int |
getCount()
Same as
DataSet.getItemCount(). |
long |
getItemId(int position)
Returns the ID of the item at the specified position.
|
View |
getView(int position,
View convertView,
ViewGroup parent) |
boolean |
hasStableIds()
Returns a boolean flag indicating whether the data set of this adapter has stable ids or not.
|
protected View |
inflate(int resource,
ViewGroup parent)
Inflates a new view hierarchy from the given xml resource.
|
protected boolean |
notifyDataSetActionSelected(int action,
int position,
Object payload)
Notifies that the given action has been performed for the specified position.
|
void |
notifyDataSetChanged() |
void |
notifyDataSetInvalidated() |
protected abstract void |
onBindViewHolder(VH viewHolder,
int position)
Invoked to configure and bind a view of an item from the current data set at the specified
position.
|
protected abstract View |
onCreateView(ViewGroup parent,
int position)
Invoked to create a view for an item from the current data set at the specified position.
|
protected VH |
onCreateViewHolder(View itemView,
int position)
Invoked to create a view holder for a view of an item from the current data set at the specified
position.
|
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.
|
areAllItemsEnabled, getDropDownView, getItemViewType, getViewTypeCount, isEmpty, isEnabled, registerDataSetObserver, unregisterDataSetObserverclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitisEnabledgetItem, getItemCount, hasItemAt, isEmptyprotected 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 notifyDataSetChanged()
notifyDataSetChanged in class BaseAdapterpublic void notifyDataSetInvalidated()
notifyDataSetInvalidated in class BaseAdapterpublic 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 final int getCount()
DataSet.getItemCount().public long getItemId(int position)
DataSetgetItemId in interface AdaptergetItemId in interface DataSet<Item>position - The position of item of which id to obtain.DataSet.NO_ID if there is no item at the specified position.DataSet.hasItemAt(int),
DataSet.getItem(int)public boolean hasStableIds()
DataSetAdapterhasStableIds in interface AdapterhasStableIds in interface DataSetAdapter<Item>hasStableIds in class BaseAdapterTrue if the data set has stable ids so each position has its unique id,
false otherwise.DataSet.getItemId(int)protected final int currentViewType()
BaseAdapter.getItemViewType(int) for the currently iterated position.@NonNull protected abstract View onCreateView(@NonNull ViewGroup parent, int position)
This is invoked only if convertView for the specified position in
getView(int, View, ViewGroup) was null.
parent - A parent view, to resolve correct layout params for the newly creating view.position - Position of the item from the current data set for which should be a new view
created.inflate(int, ViewGroup)@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)@Nullable protected VH onCreateViewHolder(@NonNull View itemView, int position)
This is invoked only if convertView for the specified position in
getView(int, View, ViewGroup) was null, so view along with its corresponding
holder need to be created.
itemView - The same view as obtained from onCreateView(ViewGroup, int) for the
specified position.position - Position of the item from the current data set for which should be a new view
holder created.null if holder for the view is
not required.protected abstract void onBindViewHolder(@NonNull VH viewHolder, int position)
getView(int, View, ViewGroup) is called.
Note, that if onCreateViewHolder(View, int) returns null for the
specified position here passed viewHolder will be the view created by
onCreateView(ViewGroup, int) for the specified position or just recycled view for
that position. This approach may be used when a view hierarchy of a specific item is represented
by single custom view, where such view represents a holder for all its child views.
viewHolder - The same holder as provided by onCreateViewHolder(View, int) for
the specified position or converted view as holder as described above.position - Position of the item from the current data set of which view to bind with data.@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.