66

I'm developing android application using expandable list view. Actually what I need is, I'm listing group, which contains child.

If I select an unexpandable group, it should expand, after I ll select second group at that time the first group should be collapsed. I did Google, but I couldn't find what I want. Please help me out.

aynber
  • 22,380
  • 8
  • 50
  • 63
Sathish
  • 1,455
  • 1
  • 16
  • 22

8 Answers8

207

Have the current expanded group position stored in a variable. In onGroupExpanded do the following.

private int lastExpandedPosition = -1;
private ExpandableListView lv; //your expandable listview
...

lv.setOnGroupExpandListener(new OnGroupExpandListener() {

    @Override
    public void onGroupExpand(int groupPosition) {
            if (lastExpandedPosition != -1
                    && groupPosition != lastExpandedPosition) {
                lv.collapseGroup(lastExpandedPosition);
            }
            lastExpandedPosition = groupPosition;
    }
});
user936414
  • 7,574
  • 3
  • 30
  • 29
29

Use this code this will work

expandableList.setOnGroupExpandListener(new OnGroupExpandListener() {
    int previousItem = -1;

    @Override
    public void onGroupExpand(int groupPosition) {
        if(groupPosition != previousItem )
            expandableList.collapseGroup(previousItem );
        previousItem = groupPosition;
    }
});
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
  • but in this case, it's lose the focus on current group..Please help me for that – Denny Sharma Apr 16 '14 at 12:21
  • 1
    This line `int previousItem = -1;` needs to be placed outside the method. Or `previousItem` will be reset each time (vanishing this later instruction: `previousItem = groupPosition;`) – Phantômaxx Jul 31 '14 at 10:32
  • 2
    This method has a flaw. It doesn't scroll to the top of the expanded group as it would do without the collapseGroup call. It is the case when the expanded group is BELOW the collapsed group. The view scrolls to the place where the expanded group would be if the old group hasn't be collapsed. Any idea how to fix it? – Dark.Rider Mar 08 '17 at 22:23
  • @Dark.Rider I am having the same issue. Did you manage to fix it? – Sudheesh Mohan Apr 04 '18 at 09:41
8
@Override
    public void onGroupExpanded(int groupPosition){
        //collapse the old expanded group, if not the same
        //as new group to expand
        if(groupPosition != lastExpandedGroupPosition){
            listView.collapseGroup(lastExpandedGroupPosition);
        }

        super.onGroupExpanded(groupPosition);           
        lastExpandedGroupPosition = groupPosition;
    }
KOTIOS
  • 11,177
  • 3
  • 39
  • 66
5

If the ExpandableListView has more than 2 groups:

expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
    @Override
    public void onGroupExpand(int groupPosition) {
        for (int g = 0; g < expandableListAdapter.getGroupCount(); g++) {
            if (g != groupPosition) {
                expandableListView.collapseGroup(g);
            }
        }
    }
});    

It will collapse all groups except the clicked one.

MD TAREQ HASSAN
  • 1,188
  • 20
  • 46
3

Get ExpendableListView and then override the following method - setOnGroupExpandListener

expandableListView = (ExpandableListView) findViewById(R.id.exp_listview);

expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        int previousItem = -1;

        @Override
        public void onGroupExpand(int groupPosition) {
            if (groupPosition != previousItem)
                expandableListView.collapseGroup(previousItem);
            previousItem = groupPosition;
        }
    });
Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51
Mikheil Zhghenti
  • 734
  • 8
  • 28
1

Try putting this in your ExpandableListAdapter, listView is a reference to the ExpandableListView itself. And lastExpandedGroupPosition is a integer member variable defined inside your ExpandableListAdapter.

 @Override
    public void onGroupExpanded(int groupPosition)

{

//collapse the old expanded group, if not the same

//as new group to expand

if(groupPosition != lastExpandedGroupPosition)

{

  listView.collapseGroup(lastExpandedGroupPosition);

  }

        super.onGroupExpanded(groupPosition);           
        lastExpandedGroupPosition = groupPosition;
    }
Sathish
  • 1,455
  • 1
  • 16
  • 22
Sundeep Badhotiya
  • 810
  • 2
  • 9
  • 14
0
   elstListView1.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int groupPosition) {
            for(int i=0;i<listDataHeader.size();i++){
                if(i==groupPosition){
                    //do nothing}
                    else{
                        elstListView1.collapseGroup(i);
                    }
                }
            }
        });
Fahim
  • 12,198
  • 5
  • 39
  • 57
Dhaval Solanki
  • 4,589
  • 1
  • 23
  • 39
-5

I also faced the same problem. But I could solved my problem by the following code:

As I had 4 Headers in my expandable list, based on that i wrote like this

expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPos) {
                // TODO Auto-generated method stub
                expListView.collapseGroup(groupPos+1);
                expListView.collapseGroup(groupPos-1);
                expListView.collapseGroup(groupPos-2);
                expListView.collapseGroup(groupPos+2);
                expListView.collapseGroup(groupPos+3);
                expListView.collapseGroup(groupPos-3);
            }
        });`
Morvader
  • 2,317
  • 3
  • 31
  • 44
NJBoro
  • 11