Problem: Expand and collapse all groups in the project, and traverse all groups as follows:
For (int I = 0, groupCount = listView. getCount (); I <groupCount; I ++)
{
If (expanded/**/) {listView. collapseGroup (I );}
Else {listView. expandGroup (I );}
}
But there is another problem. After expansion, you cannot see the group you clicked. My solution is to remember the groupPosition of the group before expanding and folding, and then use ExpandableListView. setSelectedGroup (groupPosition) to return to the group you clicked after all the group is expanded or collapsed.
In the MenuItem in public boolean onContextItemSelected (MenuItem item), The ContextMenuInfo obtained by getMenuInfo () is actually ExpandableListContextMenuInfo. You can use it to obtain the groupPosition of the group you clicked on.
The Code is as follows:
Public int getGroupPositionFromMenuInfo (ContextMenuInfo menuInfo)
{
If (menuInfo instanceof ExpandableListContextMenuInfo)
{
ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) menuInfo;
Return ExpandableListView. getPackedPositionGroup (info. packedPosition );
}
Return-1;
}
All code is as follows:
Public int getGroupPositionFromMenuInfo (ContextMenuInfo menuInfo)
{
If (menuInfo instanceof ExpandableListContextMenuInfo)
{
ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) menuInfo;
Return ExpandableListView. getPackedPositionGroup (info. packedPosition );
}
Return-1;
}
@ Override public boolean onContextItemSelected (MenuItem item)
{
//...
// Think about synchronized block
Int groupPosition = getGroupPositionFromMenuInfo (item. getMenuInfo ());
Int groupCount = listView. getCount ();
If (groupPosition <0 | groupPosition> = groupCount) {return true ;}
For (int I = 0; I <groupCount; I ++)
{
If (expanded/**/) {listView. collapseGroup (I );}
Else {listView. expandGroup (I );}
}
ListView. setSelectedGroup (groupPosition );
//...
}
Author: NeedJava