public abstract class PatchedExpandableListAdapter extends BaseExpandableListAdapter
Meant to replace the BaseExpandableListAdapter
as the starting point
for building a specialized adapter for the ExpandableListView
. It provides
convenient capabilities lacking in the ExpandableListView; as well as patching it's broken choice
mode functionality.
Choice Mode: This adapter provides a variety of ChoiceModes
which
may be enabled through setChoiceMode()
. When enabled, you must
also provide a callback via setMultiChoiceModeListener()
. Additionally, you'll need to store the adapter's saved state which
can be retrieved using onSaveInstanceState()
. Then subsequently restore it during
Activity recreation with onRestoreInstanceState(Parcelable)
.
Ownership: This adapter will take ownership of an ExpandableListView's ExpandableListView.OnGroupClickListener
, ExpandableListView.OnChildClickListener
,
and AbsListView.MultiChoiceModeListener
. Attempting to set those listeners
directly through the ExpandableListView is an error and will fail to function correctly.
Additionally all choice mode interactions must be conducted through this adapter.
Modifier and Type | Class and Description |
---|---|
static class |
PatchedExpandableListAdapter.ChoiceMode
Defines the various behaviors supported for use with
ExpandableListView . |
static interface |
PatchedExpandableListAdapter.ChoiceModeListener
An interface definition for callbacks that receive events for
ChoiceMode
modal variants. |
Constructor and Description |
---|
PatchedExpandableListAdapter(Context activity)
Constructor
|
Modifier and Type | Method and Description |
---|---|
void |
clearChoices()
Clear any choices previously set.
|
void |
collapseAll()
Collapse all groups in the adapter.
|
void |
expandAll()
Expand all groups in the adapter with no animation.
|
void |
expandAll(boolean animate)
Expand all groups in the adapter.
|
int |
getCheckedChildCount()
Returns the number of child items currently checked.
|
Long[] |
getCheckedChildIds()
Returns the set of checked children item ids.
|
Long[] |
getCheckedChildPositions()
Returns the set of child items in the list which are checked.
|
int |
getCheckedGroupCount()
Returns the number of group items currently checked.
|
Long[] |
getCheckedGroupIds()
Returns the set of checked group item ids.
|
Integer[] |
getCheckedGroupPositions()
Returns the set of group items in the list which are checked.
|
View |
getChildView(int groupPosition,
int childPosition,
boolean isLastChild,
View convertView,
ViewGroup parent) |
abstract View |
getChildView(LayoutInflater inflater,
int groupPosition,
int childPosition,
boolean isLastChild,
View convertView,
ViewGroup parent)
Gets a View that displays the data for the given child within the given group.
|
PatchedExpandableListAdapter.ChoiceMode |
getChoiceMode() |
Context |
getContext() |
View |
getGroupView(int groupPosition,
boolean isExpanded,
View convertView,
ViewGroup parent) |
abstract View |
getGroupView(LayoutInflater inflater,
int groupPosition,
boolean isExpanded,
View convertView,
ViewGroup parent)
Gets a View that displays the given group.
|
boolean |
hasAutoExpandingGroups() |
boolean |
hasStableIds() |
boolean |
isChildChecked(int groupPosition,
int childPosition)
Returns the checked state of the specified child item position.
|
boolean |
isChildSelectable(int groupPosition,
int childPosition) |
boolean |
isGroupChecked(int groupPosition)
Returns the checked state of the specified group item position.
|
boolean |
isGroupSelectable(int groupPosition)
Whether the group at the specified position is selectable.
|
void |
onRestoreInstanceState(Parcelable state)
Re-apply a representation of its internal state that had previously been generated by
onSaveInstanceState() . |
Parcelable |
onSaveInstanceState()
Saves the internal state of the adapter for use when re-instantiating a new adapter.
|
void |
setChildChecked(int groupPosition,
int childPosition,
boolean isChecked)
Sets the checked state of the specified child.
|
void |
setChoiceMode(PatchedExpandableListAdapter.ChoiceMode choiceMode)
Defines the choice behavior for the attached
ExpandableListView . |
void |
setGroupChecked(int groupPosition,
boolean isChecked)
Sets the checked state of the specified group.
|
void |
setMultiChoiceModeListener(PatchedExpandableListAdapter.ChoiceModeListener listener)
Set a
ChoiceModeListener that will
manage the lifecycle of the selection ActionMode . |
void |
setOnChildClickListener(ExpandableListView.OnChildClickListener onChildClickListener)
Register a callback to be invoked when a child item has been clicked.
|
void |
setOnGroupClickListener(ExpandableListView.OnGroupClickListener onGroupClickListener)
Register a callback to be invoked when a group item has been clicked.
|
void |
startActionMode()
Start an
ActionMode . |
areAllItemsEnabled, getChildType, getChildTypeCount, getCombinedChildId, getCombinedGroupId, getGroupType, getGroupTypeCount, isEmpty, notifyDataSetChanged, notifyDataSetInvalidated, onGroupCollapsed, onGroupExpanded, registerDataSetObserver, unregisterDataSetObserver
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getChild, getChildId, getChildrenCount, getGroup, getGroupCount, getGroupId
public PatchedExpandableListAdapter(Context activity)
activity
- Context used for inflating viewspublic void clearChoices()
NONE
(default).public void collapseAll()
public void expandAll()
expandAll(boolean)
with false passed in.public void expandAll(boolean animate)
animate
- True if the expanding groups should be animated inpublic int getCheckedChildCount()
Returns the number of child items currently checked. This will only be valid if the choice
mode is not NONE
(default).
To determine the specific items that are currently checked, use one of the getChecked*
methods.
getCheckedChildIds()
,
getCheckedChildIds()
,
getCheckedGroupIds()
,
getCheckedGroupPositions()
,
getCheckedChildPositions()
public Long[] getCheckedChildIds()
NONE
and the adapter enabled hasStableIds()
.public Long[] getCheckedChildPositions()
NONE
.public int getCheckedGroupCount()
Returns the number of group items currently checked. This will only be valid if the choice
mode is not NONE
(default).
To determine the specific items that are currently checked, use one of the getChecked*
methods.
getCheckedChildIds()
,
getCheckedGroupIds()
,
getCheckedGroupPositions()
,
getCheckedChildPositions()
public Long[] getCheckedGroupIds()
NONE
and the adapter enabled hasStableIds()
public Integer[] getCheckedGroupPositions()
NONE
.public abstract View getChildView(LayoutInflater inflater, int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
inflater
- The LayoutInflater object that can be used to inflate each view.groupPosition
- The position of the group that contains the childchildPosition
- The position of the child (for which the View is returned) within the
groupisLastChild
- Whether the child is the last child within the groupconvertView
- The old view to reuse, if possible. You should check that this view is
non-null and of an appropriate type before using. If it is not possible
to convert this view to display the correct data, this method can create
a new view. It is not guaranteed that the convertView will have been
previously created by this method.parent
- The parent that this view will eventually be attached topublic final View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
public PatchedExpandableListAdapter.ChoiceMode getChoiceMode()
ExpandableListView
public void setChoiceMode(PatchedExpandableListAdapter.ChoiceMode choiceMode)
Defines the choice behavior for the attached ExpandableListView
. By
default, this adapter does not have any choice behavior (NONE
) set.
By setting the choiceMode to SINGLE
, the ExpandableListView allows
up to one item to be in an activation state. By setting the choiceMode to MULTIPLE
, the ExpandableListView allows any number of items to be
chosen. Any of the MODAL variants will show a custom CAB when an item is long pressed.
Use this method instead of AbsListView.setChoiceMode(int)
.
This adapter will take over and emulate the behavior instead.
choiceMode
- One of the NONE
optionspublic Context getContext()
public final View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
public abstract View getGroupView(LayoutInflater inflater, int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
getChildView()
.inflater
- The LayoutInflater object that can be used to inflate each view.groupPosition
- The position of the group for which the View is returnedisExpanded
- Whether the group is expanded or collapsedconvertView
- The old view to reuse, if possible. You should check that this view is
non-null and of an appropriate type before using. If it is not possible
to convert this view to display the correct data, this method can create
a new view. It is not guaranteed that the convertView will have been
previously created by this method.parent
- The parent that this view will eventually be attached topublic boolean hasAutoExpandingGroups()
public boolean hasStableIds()
public boolean isChildChecked(int groupPosition, int childPosition)
NONE
groupPosition
- The position of the group that contains the child.childPosition
- The position of the child.setChoiceMode()
public boolean isChildSelectable(int groupPosition, int childPosition)
public boolean isGroupChecked(int groupPosition)
NONE
groupPosition
- The position of the group itemfalse
if choice mode is disabled.setChoiceMode
public boolean isGroupSelectable(int groupPosition)
groupPosition
- The position of the group that contains the childpublic void onRestoreInstanceState(Parcelable state)
onSaveInstanceState()
. Specifically this restores the ChoiceMode, ActionMode and checked
children/group items of the adapter. This method will not restore the internal data
originally stored with the adapter. That must still be done manually as you would with any
other adapter. If not using choice mode, there is no need to invoke this.state
- The frozen state that had previously been returned by onSaveInstanceState()
.onSaveInstanceState()
public Parcelable onSaveInstanceState()
onRestoreInstanceState(android.os.Parcelable)
public void setChildChecked(int groupPosition, int childPosition, boolean isChecked)
PatchedExpandableListAdapter.ChoiceMode.NONE
.groupPosition
- The position of the group that contains the child.childPosition
- The position of the child.isChecked
- The new checked state for the child item.public void setGroupChecked(int groupPosition, boolean isChecked)
NONE
. If choice mode is set to a multiple selector, Eg MULTIPLE
or MULTIPLE_MODAL
then all
child items will additionally have their check state updated.groupPosition
- The position of the group.isChecked
- The new checked state for the child item.public void setMultiChoiceModeListener(PatchedExpandableListAdapter.ChoiceModeListener listener)
ChoiceModeListener
that will
manage the lifecycle of the selection ActionMode
. Only used when the
choice mode is set to modal variant of ChoiceMode
. Eg MULITPLE_MODAL
or SINGLE_MODAL
.listener
- Callback that will manage the selection modepublic void setOnChildClickListener(ExpandableListView.OnChildClickListener onChildClickListener)
Register a callback to be invoked when a child item has been clicked. Whether a listener
is registered or not, this adapter takes ownership of the ExpandableListView
's equivalent listeners. Attempting to set the callback
directly through the ExpandableListView will not work.
When a modal ChoiceMode
CAB is activated, all children click events
will be ignored by this callback. Instead use the setMultiChoiceModeListener()
to handle that case.
onChildClickListener
- The callback that will be invoked.public void setOnGroupClickListener(ExpandableListView.OnGroupClickListener onGroupClickListener)
Register a callback to be invoked when a group item has been clicked. Whether a listener
is registered or not, this adapter takes ownership of the ExpandableListView
's equivalent listeners. Attempting to set the callback
directly through the ExpandableListView will not work.
When a modal ChoiceMode
CAB is activated, all group click events will
be ignored by this callback. Instead use the setMultiChoiceModeListener()
to handle that case.
onGroupClickListener
- The callback that will be invoked.public void startActionMode()
ActionMode
. Use this method instead of ExpandableListView.startActionMode()
.