Expand all children in expandable list view

Drejc picture Drejc · Jul 29, 2011 · Viewed 61.4k times · Source

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?

Answer

Justin picture Justin · Nov 11, 2013

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!