Expand all children in expandable list view

AndroidExpandablelistview

Android Problem Overview


I would like to expand all children while the expandable list view is populated. Currently my code looks like this:

ExpandableListView listView = (ExpandableListView) findViewById(R.id.view);
int count = viewAdapted.getGroupCount();
for (int position = 1; position <= count; position++)
	listView.expandGroup(position - 1);

which is pretty ugly. Is there a nicer way to do this?

Android Solutions


Solution 1 - Android

You can expand it in getGroupView in your custom adapter:

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    View v = super.getGroupView(groupPosition, isExpanded, convertView, parent);
    ExpandableListView mExpandableListView = (ExpandableListView) parent;
	mExpandableListView.expandGroup(groupPosition);
    return v;
}

Gluck!

Solution 2 - Android

putting this code on your adapter will make the expandlist to stay open, and disables its closing function.

ExpandableListView mExpandableListView = (ExpandableListView) parent;
mExpandableListView.expandGroup(groupPosition);

this may be a little work around but works how it should be. It opens all group ( at start ) and can be close any time

 for ( int i = 0; i < groupList.getCount(); i++ ) {
	groupList.expandGroup(i);
 } 

NOTE : put this code after setting adapter on your expandable view, or else it may cause error. Hope it helps.

Solution 3 - Android

first fill the adapter than put this code in your oncreate method

   int count = adapter.getGroupCount();
                for ( int i = 0; i < count; i++ ) 
                    listView.expandGroup(i);

Solution 4 - Android

Expanding all groups

for(int i=0; i < myAdapter.getGroupCount(); i++)
    myExpandableListView.expandGroup(i);

if you wish to make them unCollapseable.

myExpandableListView.setOnGroupClickListener(new OnGroupClickListener() {
  @Override
  public boolean onGroupClick(ExpandableListView parent, View v,int  groupPosition, long id) { 
    return true; 
  }
});

Solution 5 - Android

I have tried the responses listed but i found that i can use the getGroupCount() method to get the number of groups.

Using this method i can iterate and expand every group of my ExpandableListView

for (int i = 0; i < myExpandableListView.getExpandableListAdapter().getGroupCount(); i++) {
      //Expand group
      myExpandableListView.expandGroup(i);
 }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDrejcView Question on Stackoverflow
Solution 1 - AndroidJustinView Answer on Stackoverflow
Solution 2 - AndroidralphgabbView Answer on Stackoverflow
Solution 3 - AndroidJatin DevaniView Answer on Stackoverflow
Solution 4 - AndroidVJ Vishal JogiyaView Answer on Stackoverflow
Solution 5 - AndroidJorgesysView Answer on Stackoverflow