77

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?

6
  • Maybe you can look this: stackoverflow.com/questions/6849013/expandable-listview
    – david55
    Jul 29, 2011 at 13:09
  • Apparently this is the only solution right now. Hoped there is some attribute or similar.
    – Drejc
    Jul 29, 2011 at 13:37
  • Of course (position <= count) ... copy past typo
    – Drejc
    Mar 21, 2013 at 10:26
  • @Drejc: could you pick the correct answer?
    – Justin
    Dec 14, 2015 at 2:29
  • There was ... but it was deleted
    – Drejc
    Dec 15, 2015 at 9:49

5 Answers 5

79

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!

2
  • 14
    this makes them unclickable Nov 13, 2013 at 5:35
  • 2
    @DavidLawson: yep, my sollution is fixed-expand them. If you wana expand them and they are clickable, you have to handle that where ExpandableListView is created, like Drejc's sollution.
    – Justin
    Jan 17, 2014 at 9:44
39

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.

1
  • It doesnt matter where you put this code, before or after setAdapter. But you should put that after your set your data for adapter. This makes sens if you sets adapter and then loads your data over a network Aug 1, 2021 at 19:36
8

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);
7

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; 
  }
});
0
1

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);
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.