public abstract class ExpandableRecyclerAdapter<P extends Parent<C>,C,PVH extends ParentViewHolder,CVH extends ChildViewHolder>
extends <any>
Changes should be notified through:
notifyParentInserted(int)
notifyParentRemoved(int)
notifyParentChanged(int)
notifyParentRangeInserted(int, int)
notifyChildInserted(int, int)
notifyChildRemoved(int, int)
notifyChildChanged(int, int)
methods and not the notify methods of RecyclerView.Adapter.
| Modifier and Type | Class and Description |
|---|---|
static interface |
ExpandableRecyclerAdapter.ExpandCollapseListener
Allows objects to register themselves as expand/collapse listeners to be
notified of change events.
|
| Modifier and Type | Field and Description |
|---|---|
protected java.util.List<ExpandableWrapper<P,C>> |
mFlatItemList
A
List of all currently expanded parents and their children, in order. |
static int |
TYPE_CHILD
Default ViewType for children rows
|
static int |
TYPE_FIRST_USER
Start of user-defined view types
|
static int |
TYPE_PARENT
Default ViewType for parent rows
|
| Constructor and Description |
|---|
ExpandableRecyclerAdapter(java.util.List<P> parentList)
Primary constructor.
|
| Modifier and Type | Method and Description |
|---|---|
void |
collapseAllParents()
Collapses all parents in the list.
|
void |
collapseParent(int parentPosition)
Collapses the parent with the specified index in the list of parents.
|
void |
collapseParent(P parent)
Collapses the parent associated with a specified
P in the list of parents. |
void |
collapseParentRange(int startParentPosition,
int parentCount)
Collapses all parents in a range of indices in the list of parents.
|
void |
expandAllParents()
Expands all parents in the list.
|
void |
expandParent(int parentPosition)
Expands the parent with the specified index in the list of parents.
|
void |
expandParent(P parent)
Expands the parent associated with a specified
P in the list of parents. |
void |
expandParentRange(int startParentPosition,
int parentCount)
Expands all parents in a range of indices in the list of parents.
|
int |
getChildViewType(int parentPosition,
int childPosition)
Return the view type of the child
parentPosition contained within the parent
at parentPosition for the purposes of view recycling. |
int |
getItemCount()
Gets the number of parents and children currently expanded.
|
int |
getItemViewType(int flatPosition)
For multiple view type support look at overriding
getParentViewType(int) and
getChildViewType(int, int). |
java.util.List<P> |
getParentList()
Gets the list of parents that is backing this adapter.
|
int |
getParentViewType(int parentPosition)
Return the view type of the parent at
parentPosition for the purposes of view recycling. |
boolean |
isParentViewType(int viewType)
Used to determine whether a viewType is that of a parent or not, for ViewHolder creation purposes.
|
void |
notifyChildChanged(int parentPosition,
int childPosition)
Notify any registered observers that the parent at
parentPosition has
a child located at childPosition that has changed. |
void |
notifyChildInserted(int parentPosition,
int childPosition)
Notify any registered observers that the parent reflected at
parentPosition
has a child list item that has been newly inserted at childPosition. |
void |
notifyChildMoved(int parentPosition,
int fromChildPosition,
int toChildPosition)
Notify any registered observers that the child list item contained within the parent
at
parentPosition has moved from fromChildPosition to toChildPosition. |
void |
notifyChildRangeChanged(int parentPosition,
int childPositionStart,
int itemCount)
Notify any registered observers that the parent at
parentPosition has
itemCount children starting at childPositionStart that have changed. |
void |
notifyChildRangeInserted(int parentPosition,
int childPositionStart,
int itemCount)
Notify any registered observers that the parent reflected at
parentPosition
has itemCount child list items that have been newly inserted at childPositionStart. |
void |
notifyChildRangeRemoved(int parentPosition,
int childPositionStart,
int itemCount)
Notify any registered observers that the parent located at
parentPosition
has itemCount children that have been removed from the data set, previously
located at childPositionStart onwards. |
void |
notifyChildRemoved(int parentPosition,
int childPosition)
Notify any registered observers that the parent located at
parentPosition
has a child that has been removed from the data set, previously located at childPosition. |
void |
notifyParentChanged(int parentPosition)
Notify any registered observers that the parent at
parentPosition has changed. |
void |
notifyParentDataSetChanged(boolean preserveExpansionState)
Notify any registered observers that the data set has changed.
|
void |
notifyParentInserted(int parentPosition)
Notify any registered observers that the parent reflected at
parentPosition
has been newly inserted. |
void |
notifyParentMoved(int fromParentPosition,
int toParentPosition)
Notify any registered observers that the parent and its children reflected at
fromParentPosition has been moved to toParentPosition. |
void |
notifyParentRangeChanged(int parentPositionStart,
int itemCount)
Notify any registered observers that the
itemCount parents starting
at parentPositionStart have changed. |
void |
notifyParentRangeInserted(int parentPositionStart,
int itemCount)
Notify any registered observers that the currently reflected
itemCount
parents starting at parentPositionStart have been newly inserted. |
void |
notifyParentRangeRemoved(int parentPositionStart,
int itemCount)
Notify any registered observers that the
itemCount parents previously
located at parentPositionStart have been removed from the data set. |
void |
notifyParentRemoved(int parentPosition)
Notify any registered observers that the parents previously located at
parentPosition
has been removed from the data set. |
void |
onAttachedToRecyclerView(RecyclerView recyclerView)
Implementation of Adapter#onAttachedToRecyclerView(RecyclerView).
|
abstract void |
onBindChildViewHolder(CVH childViewHolder,
int parentPosition,
int childPosition,
C child)
Callback called from onBindViewHolder(RecyclerView.ViewHolder, int)
when the list item bound to is a child.
|
abstract void |
onBindParentViewHolder(PVH parentViewHolder,
int parentPosition,
P parent)
Callback called from onBindViewHolder(RecyclerView.ViewHolder, int)
when the list item bound to is a parent.
|
void |
onBindViewHolder(RecyclerView.ViewHolder holder,
int flatPosition)
Implementation of Adapter.onBindViewHolder(RecyclerView.ViewHolder, int)
that determines if the list item is a parent or a child and calls through
to the appropriate implementation of either
onBindParentViewHolder(ParentViewHolder, int, Parent) or
onBindChildViewHolder(ChildViewHolder, int, int, Object). |
abstract CVH |
onCreateChildViewHolder(android.view.ViewGroup childViewGroup,
int viewType)
Callback called from
onCreateViewHolder(ViewGroup, int) when
the list item created is a child. |
abstract PVH |
onCreateParentViewHolder(android.view.ViewGroup parentViewGroup,
int viewType)
Callback called from
onCreateViewHolder(ViewGroup, int) when
the list item created is a parent. |
RecyclerView.ViewHolder |
onCreateViewHolder(android.view.ViewGroup viewGroup,
int viewType)
Implementation of Adapter.onCreateViewHolder(ViewGroup, int)
that determines if the list item is a parent or a child and calls through
to the appropriate implementation of either
onCreateParentViewHolder(ViewGroup, int)
or onCreateChildViewHolder(ViewGroup, int). |
void |
onDetachedFromRecyclerView(RecyclerView recyclerView)
Implementation of Adapter.onDetachedFromRecyclerView(RecyclerView)
|
void |
onRestoreInstanceState(android.os.Bundle savedInstanceState)
Fetches the expandable state map from the saved instance state
Bundle
and restores the expanded states of all of the parents. |
void |
onSaveInstanceState(android.os.Bundle savedInstanceState)
Stores the expanded state map across state loss.
|
protected void |
parentCollapsedFromViewHolder(int flatParentPosition)
Called when a ParentViewHolder has triggered a collapse for it's parent
|
protected void |
parentExpandedFromViewHolder(int flatParentPosition)
Called when a ParentViewHolder has triggered an expansion for it's parent
|
void |
setExpandCollapseListener(ExpandableRecyclerAdapter.ExpandCollapseListener expandCollapseListener) |
void |
setParentList(java.util.List<P> parentList,
boolean preserveExpansionState)
Set a new list of parents and notify any registered observers that the data set has changed.
|
public static final int TYPE_PARENT
public static final int TYPE_CHILD
public static final int TYPE_FIRST_USER
@NonNull protected java.util.List<ExpandableWrapper<P extends Parent<C>,C>> mFlatItemList
List of all currently expanded parents and their children, in order.
Changes to this list should be made through the add/remove methods
available in ExpandableRecyclerAdapter.public ExpandableRecyclerAdapter(@NonNull
java.util.List<P> parentList)
mParentList and mFlatItemList.
Any changes to mParentList should be made on the original instance, and notified via
notifyParentInserted(int)
notifyParentRemoved(int)
notifyParentChanged(int)
notifyParentRangeInserted(int, int)
notifyChildInserted(int, int)
notifyChildRemoved(int, int)
notifyChildChanged(int, int)
methods and not the notify methods of RecyclerView.Adapter.
parentList - List of all parents to be displayed in the RecyclerView that this
adapter is linked to@NonNull
@UiThread
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull
android.view.ViewGroup viewGroup,
int viewType)
onCreateParentViewHolder(ViewGroup, int)
or onCreateChildViewHolder(ViewGroup, int).viewGroup - 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 android.view.View.android.view.View of the given view type.@UiThread
public void onBindViewHolder(@NonNull
RecyclerView.ViewHolder holder,
int flatPosition)
onBindParentViewHolder(ParentViewHolder, int, Parent) or
onBindChildViewHolder(ChildViewHolder, int, int, Object).holder - The RecyclerView.ViewHolder to bind data toflatPosition - The index in the merged list of children and parents at which to bind@NonNull @UiThread public abstract PVH onCreateParentViewHolder(@NonNull android.view.ViewGroup parentViewGroup, int viewType)
onCreateViewHolder(ViewGroup, int) when
the list item created is a parent.parentViewGroup - The ViewGroup in the list for which a PVH is being
createdPVH corresponding to the parent with the ViewGroup parentViewGroup@NonNull @UiThread public abstract CVH onCreateChildViewHolder(@NonNull android.view.ViewGroup childViewGroup, int viewType)
onCreateViewHolder(ViewGroup, int) when
the list item created is a child.childViewGroup - The ViewGroup in the list for which a CVH
is being createdCVH corresponding to the child with the ViewGroup childViewGroup@UiThread
public abstract void onBindParentViewHolder(@NonNull
PVH parentViewHolder,
int parentPosition,
@NonNull
P parent)
Bind data to the PVH here.
parentViewHolder - The PVH to bind data toparentPosition - The position of the parent to bindparent - The parent which holds the data to be bound to the PVH@UiThread
public abstract void onBindChildViewHolder(@NonNull
CVH childViewHolder,
int parentPosition,
int childPosition,
@NonNull
C child)
Bind data to the CVH here.
childViewHolder - The CVH to bind data toparentPosition - The position of the parent that contains the child to bindchildPosition - The position of the child to bindchild - The child which holds that data to be bound to the CVH@UiThread public int getItemCount()
mFlatItemList@UiThread public int getItemViewType(int flatPosition)
getParentViewType(int) and
getChildViewType(int, int). Almost all cases should override those instead
of this method.flatPosition - The index in the merged list of children and parents to get the view type ofpublic int getParentViewType(int parentPosition)
parentPosition for the purposes of view recycling.
The default implementation of this method returns TYPE_PARENT, making the assumption of
a single view type for the parents in this adapter. Unlike ListView adapters, types need not
be contiguous. Consider using id resources to uniquely identify item view types.
If you are overriding this method make sure to override isParentViewType(int) as well.
Start your defined viewtypes at TYPE_FIRST_USER
parentPosition - The index of the parent to queryparentPosition. Type codes need not be contiguous.public int getChildViewType(int parentPosition,
int childPosition)
parentPosition contained within the parent
at parentPosition for the purposes of view recycling.
The default implementation of this method returns TYPE_CHILD, making the assumption of
a single view type for the children in this adapter. Unlike ListView adapters, types need not
be contiguous. Consider using id resources to uniquely identify item view types.
Start your defined viewtypes at TYPE_FIRST_USER
parentPosition - The index of the parent continaing the child to querychildPosition - The index of the child within the parent to queryparentPosition. Type codes need not be contiguous.public boolean isParentViewType(int viewType)
Only override if getParentViewType(int) is being overriden
viewType - the viewType identifier in question@NonNull @UiThread public java.util.List<P> getParentList()
notifyParentInserted(int)
notifyParentRemoved(int)
notifyParentChanged(int)
notifyParentRangeInserted(int, int)
notifyChildInserted(int, int)
notifyChildRemoved(int, int)
notifyChildChanged(int, int)
methods.@UiThread
public void setParentList(@NonNull
java.util.List<P> parentList,
boolean preserveExpansionState)
This setter does not specify what about the data set has changed, forcing any observers to assume that all existing items and structure may no longer be valid. LayoutManagers will be forced to fully rebind and relayout all visible views.
It will always be more efficient to use the more specific change events if you can.
Rely on #setParentList(List, boolean) as a last resort. There will be no animation
of changes, unlike the more specific change events listed below.
preserveExpansionState - If true, the adapter will attempt to preserve your parent's last expanded
state. This depends on object equality for comparisons of
old parents to parents in the new list.
If false, only Parent.isInitiallyExpanded()
will be used to determine expanded state.notifyParentInserted(int),
notifyParentRemoved(int),
notifyParentChanged(int),
notifyParentRangeInserted(int, int),
notifyChildInserted(int, int),
notifyChildRemoved(int, int),
notifyChildChanged(int, int)@UiThread
public void onAttachedToRecyclerView(@NonNull
RecyclerView recyclerView)
Called when this ExpandableRecyclerAdapter is attached to a RecyclerView.
recyclerView - The RecyclerView this ExpandableRecyclerAdapter
is being attached to@UiThread
public void onDetachedFromRecyclerView(@NonNull
RecyclerView recyclerView)
Called when this ExpandableRecyclerAdapter is detached from a RecyclerView.
recyclerView - The RecyclerView this ExpandableRecyclerAdapter
is being detached from@UiThread
public void setExpandCollapseListener(@Nullable
ExpandableRecyclerAdapter.ExpandCollapseListener expandCollapseListener)
@UiThread protected void parentExpandedFromViewHolder(int flatParentPosition)
flatParentPosition - the position of the parent that is calling to be expanded@UiThread protected void parentCollapsedFromViewHolder(int flatParentPosition)
flatParentPosition - the position of the parent that is calling to be collapsed@UiThread
public void expandParent(@NonNull
P parent)
P in the list of parents.parent - The P of the parent to expand@UiThread public void expandParent(int parentPosition)
parentPosition - The position of the parent to expand@UiThread
public void expandParentRange(int startParentPosition,
int parentCount)
startParentPosition - The index at which to to start expanding parentsparentCount - The number of parents to expand@UiThread public void expandAllParents()
@UiThread
public void collapseParent(@NonNull
P parent)
P in the list of parents.parent - The P of the parent to collapse@UiThread public void collapseParent(int parentPosition)
parentPosition - The index of the parent to collapse@UiThread
public void collapseParentRange(int startParentPosition,
int parentCount)
startParentPosition - The index at which to to start collapsing parentsparentCount - The number of parents to collapse@UiThread public void collapseAllParents()
@UiThread
public void onSaveInstanceState(@NonNull
android.os.Bundle savedInstanceState)
Should be called from Activity.onSaveInstanceState(Bundle) in
the Activity that hosts the RecyclerView that this
ExpandableRecyclerAdapter is attached to.
This will make sure to add the expanded state map as an extra to the
instance state bundle to be used in onRestoreInstanceState(Bundle).
savedInstanceState - The Bundle into which to store the
expanded state map@UiThread
public void onRestoreInstanceState(@Nullable
android.os.Bundle savedInstanceState)
Bundle
and restores the expanded states of all of the parents.
Should be called from Activity.onRestoreInstanceState(Bundle) in
the Activity that hosts the RecyclerView that this
ExpandableRecyclerAdapter is attached to.
Assumes that the list of parents is the same as when the saved instance state was stored.
savedInstanceState - The Bundle from which the expanded
state map is loaded@UiThread public void notifyParentDataSetChanged(boolean preserveExpansionState)
This event does not specify what about the data set has changed, forcing any observers to assume that all existing items and structure may no longer be valid. LayoutManagers will be forced to fully rebind and relayout all visible views.
It will always be more efficient to use the more specific change events if you can.
Rely on #notifyParentDataSetChanged(boolean) as a last resort. There will be no animation
of changes, unlike the more specific change events listed below.
preserveExpansionState - If true, the adapter will attempt to preserve your parent's last expanded
state. This depends on object equality for comparisons of
old parents to parents in the new list.
If false, only Parent.isInitiallyExpanded()
will be used to determine expanded state.notifyParentInserted(int),
notifyParentRemoved(int),
notifyParentChanged(int),
notifyParentRangeInserted(int, int),
notifyChildInserted(int, int),
notifyChildRemoved(int, int),
notifyChildChanged(int, int)@UiThread public void notifyParentInserted(int parentPosition)
parentPosition
has been newly inserted. The parent previously at parentPosition is now at
position parentPosition + 1.
This is a structural change event. Representations of other existing items in the data set are still considered up to date and will not be rebound, though their positions may be altered.
parentPosition - Position of the newly inserted parent in the data set, relative
to the list of parents only.notifyParentRangeInserted(int, int)@UiThread
public void notifyParentRangeInserted(int parentPositionStart,
int itemCount)
itemCount
parents starting at parentPositionStart have been newly inserted.
The parents previously located at parentPositionStart and beyond
can now be found starting at position parentPositionStart + itemCount.
This is a structural change event. Representations of other existing items in the data set are still considered up to date and will not be rebound, though their positions may be altered.
parentPositionStart - Position of the first parent that was inserted, relative
to the list of parents only.itemCount - Number of items insertednotifyParentInserted(int)@UiThread public void notifyParentRemoved(int parentPosition)
parentPosition
has been removed from the data set. The parents previously located at and after
parentPosition may now be found at oldPosition - 1.
This is a structural change event. Representations of other existing items in the data set are still considered up to date and will not be rebound, though their positions may be altered.
parentPosition - Position of the parent that has now been removed, relative
to the list of parents only.public void notifyParentRangeRemoved(int parentPositionStart,
int itemCount)
itemCount parents previously
located at parentPositionStart have been removed from the data set. The parents
previously located at and after parentPositionStart + itemCount may now be
found at oldPosition - itemCount.
This is a structural change event. Representations of other existing items in the data set are still considered up to date and will not be rebound, though their positions may be altered.
parentPositionStart - The previous position of the first parent that was
removed, relative to list of parents only.itemCount - Number of parents removed from the data set@UiThread public void notifyParentChanged(int parentPosition)
parentPosition has changed.
This will also trigger an item changed for children of the parent list specified.
This is an item change event, not a structural change event. It indicates that any
reflection of the data at parentPosition is out of date and should be updated.
The parent at parentPosition retains the same identity. This means
the number of children must stay the same.
parentPosition - Position of the item that has changed@UiThread
public void notifyParentRangeChanged(int parentPositionStart,
int itemCount)
itemCount parents starting
at parentPositionStart have changed. This will also trigger an item changed
for children of the parent list specified.
This is an item change event, not a structural change event. It indicates that any reflection of the data in the given position range is out of date and should be updated. The parents in the given range retain the same identity. This means that the number of children must stay the same.
parentPositionStart - Position of the item that has changeditemCount - Number of parents changed in the data set@UiThread
public void notifyParentMoved(int fromParentPosition,
int toParentPosition)
fromParentPosition has been moved to toParentPosition.
This is a structural change event. Representations of other existing items in the data set are still considered up to date and will not be rebound, though their positions may be altered.
fromParentPosition - Previous position of the parent, relative to the list of
parents only.toParentPosition - New position of the parent, relative to the list of parents only.@UiThread
public void notifyChildInserted(int parentPosition,
int childPosition)
parentPosition
has a child list item that has been newly inserted at childPosition.
The child list item previously at childPosition is now at
position childPosition + 1.
This is a structural change event. Representations of other existing items in the data set are still considered up to date and will not be rebound, though their positions may be altered.
parentPosition - Position of the parent which has been added a child, relative
to the list of parents only.childPosition - Position of the child that has been inserted, relative to children
of the parent specified by parentPosition only.@UiThread
public void notifyChildRangeInserted(int parentPosition,
int childPositionStart,
int itemCount)
parentPosition
has itemCount child list items that have been newly inserted at childPositionStart.
The child list item previously at childPositionStart and beyond are now at
position childPositionStart + itemCount.
This is a structural change event. Representations of other existing items in the data set are still considered up to date and will not be rebound, though their positions may be altered.
parentPosition - Position of the parent which has been added a child, relative
to the list of parents only.childPositionStart - Position of the first child that has been inserted,
relative to children of the parent specified by
parentPosition only.itemCount - number of children inserted@UiThread
public void notifyChildRemoved(int parentPosition,
int childPosition)
parentPosition
has a child that has been removed from the data set, previously located at childPosition.
The child list item previously located at and after childPosition may
now be found at childPosition - 1.
This is a structural change event. Representations of other existing items in the data set are still considered up to date and will not be rebound, though their positions may be altered.
parentPosition - Position of the parent which has a child removed from, relative
to the list of parents only.childPosition - Position of the child that has been removed, relative to children
of the parent specified by parentPosition only.@UiThread
public void notifyChildRangeRemoved(int parentPosition,
int childPositionStart,
int itemCount)
parentPosition
has itemCount children that have been removed from the data set, previously
located at childPositionStart onwards. The child previously located at and
after childPositionStart may now be found at childPositionStart - itemCount.
This is a structural change event. Representations of other existing items in the data set are still considered up to date and will not be rebound, though their positions may be altered.
parentPosition - Position of the parent which has a child removed from, relative
to the list of parents only.childPositionStart - Position of the first child that has been removed, relative
to children of the parent specified by parentPosition only.itemCount - number of children removed@UiThread
public void notifyChildChanged(int parentPosition,
int childPosition)
parentPosition has
a child located at childPosition that has changed.
This is an item change event, not a structural change event. It indicates that any
reflection of the data at childPosition is out of date and should be updated.
The parent at childPosition retains the same identity.
parentPosition - Position of the parent which has a child that has changedchildPosition - Position of the child that has changed@UiThread
public void notifyChildRangeChanged(int parentPosition,
int childPositionStart,
int itemCount)
parentPosition has
itemCount children starting at childPositionStart that have changed.
This is an item change event, not a structural change event. It indicates that any
The parent at childPositionStart retains the same identity.
reflection of the set of itemCount children starting at childPositionStart
are out of date and should be updated.
parentPosition - Position of the parent who has a child that has changedchildPositionStart - Position of the first child that has changeditemCount - number of children changed@UiThread
public void notifyChildMoved(int parentPosition,
int fromChildPosition,
int toChildPosition)
parentPosition has moved from fromChildPosition to toChildPosition.
This is a structural change event. Representations of other existing items in the data set are still considered up to date and will not be rebound, though their positions may be altered.
parentPosition - Position of the parent which has a child that has movedfromChildPosition - Previous position of the childtoChildPosition - New position of the child