H - Type of the header items presented within data set of a subclass of this HeadersModule.public abstract class HeadersModule<H extends HeadersModule.Header> extends AdapterModule
AdapterModule implementation that specifies API for modules that may provide additional
data set of headers for theirs associated adapter. To ensure that the associated adapter works
properly, it is necessary to call some of HeadersModule API methods from within such adapter as
it is shown below:
public class SampleAdapter extends BaseAdapter implements ModuleAdapter {
// Flag indicating item view type.
private static final int VIEW_TYPE_ITEM = 0x00;
// Flag indicating header view type.
private static final int VIEW_TYPE_HEADER = 0x01;
// Headers module holding the header items data set.
private HeadersModule mHeadersModule;
// ...
public SampleAdapter(@NonNull Context context) {
super(context);
this.mHeadersModule = new HeadersModule();
this.mHeadersModule.attachToAdapter(this);
}
@NonNull
public int getItemCount() {
// Expand current data set size with also size of headers data set.
return super.getItemCount() + mHeadersModule.size();
}
@NonNull
public Item getItem(int position) {
// This is necessary because this adapter does not hold the entire data set but only
// its items, so without this correction this call can throw IndexOutOfBoundsException
// for the positions above super.getItemCount() because of implementation of getItemCount().
return super.getItem(mHeadersModule.correctPosition(position));
}
@NonNull
protected View onCreateView(@NonNull ViewGroup parent, int position) {
switch (currentViewType()) {
case VIEW_TYPE_ITEM:
return new View(mContext); // Create here view for adapter's item.
case VIEW_TYPE_HEADER:
return mHeadersModule.createView(mLayoutInflater, parent, position);
}
return null;
}
@NonNull
protected void onBindViewHolder(@NonNull Object viewHolder, int position) {
switch (currentViewType()) {
case VIEW_TYPE_ITEM:
// Bind here adapter's item.
break;
case VIEW_TYPE_HEADER:
mHeadersModule.bindViewHolder(viewHolder, position);
break;
}
}
// ...
}
| Modifier and Type | Class and Description |
|---|---|
static interface |
HeadersModule.Header
Required interface for header items of
HeadersModule. |
static class |
HeadersModule.SimpleHeader
Simple implementation of
HeadersModule.Header item for HeadersModule. |
AdapterModule.ModuleAdapter| Constructor and Description |
|---|
HeadersModule() |
| Modifier and Type | Method and Description |
|---|---|
protected void |
addHeader(H header,
int position)
Adds the given header at the specified position into the current headers data set of this module.
|
void |
bindViewHolder(Object viewHolder,
int position)
Binds the given header viewHolder for the specified position.
|
void |
clearHeaders()
Clears the current headers data set of this module.
|
int |
correctPosition(int position)
Corrects the given position passed from the related adapter.
|
View |
createView(LayoutInflater inflater,
ViewGroup parent,
int position)
Creates the view for the header item at the specified position.
|
Object |
createViewHolder(View view,
int position)
Creates the view holder for the given header view at the specified position.
|
H |
getHeader(int position)
Returns the header associated with the specified position from the current headers data set
of this module.
|
SparseArray<H> |
getHeaders()
Returns the current headers data set of this module.
|
int |
getHeadersCountBeforePosition(int position)
Counts headers presented in the current headers data set before the requested position.
|
int |
getHeaderStyleAttr()
Returns the Xml style attribute specified via
setHeaderStyleAttr(int). |
boolean |
isEmpty()
Checks whether this module has some headers or not.
|
boolean |
isHeaderAt(int position)
Checks whether there is a header at the specified position or not.
|
protected void |
removeHeaderAt(int position)
Removes a header at the specified position from the current headers data set of this module.
|
void |
setHeaderStyleAttr(int styleAttr)
Sets an Xml attribute from the current theme, which contains a resource of style with attributes
for header view which may be created via
createView(LayoutInflater, ViewGroup, int). |
int |
size()
Returns the count of headers in the current headers data set of this module.
|
assertAttachedToAdapterOrThrow, attachToAdapter, isAdapterNotificationEnabled, notifyAdapter, onAttachedToAdapter, requiresStateSaving, setAdapterNotificationEnabledpublic boolean isEmpty()
True if this module does not have any headers, false otherwise.size()public int size()
isEmpty()protected void addHeader(@NonNull H header, int position)
header - The desired header to add.position - The position at which should be header added.getHeader(int),
removeHeaderAt(int)public boolean isHeaderAt(int position)
position - The position to check.True if there is a header item at the specified position, false otherwise.getHeader(int)@Nullable public H getHeader(int position)
position - Position of the desired header to obtain.null if there
is no header item at the requested position.getHeaders(),
isEmpty()@NonNull public SparseArray<H> getHeaders()
getHeader(int),
isEmpty()protected void removeHeaderAt(int position)
position - The position at which should be header removed.addHeader(Header, int),
clearHeaders()public void clearHeaders()
public int correctPosition(int position)
getHeadersCountBeforePosition(int).
This should be used within the associated adapter's Adapter#getItem(int).
position - The position to correct.public int getHeadersCountBeforePosition(int position)
position - The position, to which should be headers counted.public void setHeaderStyleAttr(@AttrRes int styleAttr)
createView(LayoutInflater, ViewGroup, int).
Default value: R.attr.textViewStyle
styleAttr - Xml style attribute.getHeaderStyleAttr()@AttrRes public int getHeaderStyleAttr()
setHeaderStyleAttr(int).setHeaderStyleAttr(int)@NonNull public View createView(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent, int position)
Note, that the position passed here need to be the same position as passed to
Adapter#getView(int, android.view.View, android.view.ViewGroup).
inflater - Layout inflater that may be used to create the requested view.parent - A parent view, to resolve correct layout params for the newly creating view.position - Position of the header from the current data set for which should be a new view
created.@Nullable public Object createViewHolder(@NonNull View view, int position)
This method returns null by default as createView(LayoutInflater, ViewGroup, int)
method creates by default instance of TextView.
view - The header view created via createView(LayoutInflater, ViewGroup, int)
for which to create its holder.position - The position for which has been the view created.null if the view itself is a holder.public void bindViewHolder(@NonNull Object viewHolder, int position)
Note, that the position passed here need to be the same position as passed to
Adapter#getView(int, android.view.View, android.view.ViewGroup).
viewHolder - Holder for the specified position of which view should be bound with header's
data.position - The position for which to bind header data.