Reasonably expand or collapse all groups of ExapandableListView

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.