Collapse all group except selected group in expandable listview android
Asked Answered
V

8

67

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.

Verst answered 11/7, 2013 at 6:7 Comment(0)
K
209

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;
    }
});
Kono answered 11/7, 2013 at 6:17 Comment(5)
hello user936414, i feel good that I give you 100th reputation point. Thanks for answer.Bultman
If the ExpandableListView has more than 2 groups, it will not close all groupsJea
@HassanTareq As expandableListView do not have its groups open by default, You would get the desired work by collapsing the previously opened group.Toothsome
@Kono This makes the expandablelstview to scroll to the end or middle of the expanded group. Any solution?Conal
Hi, how can i implement this above code on a custom made list view with a child group as a relative layoutTumer
I
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;
    }
});
Interdictory answered 11/7, 2013 at 6:11 Comment(4)
but in this case, it's lose the focus on current group..Please help me for thatUnburden
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;)Airliah
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?Eta
@Eta I am having the same issue. Did you manage to fix it?Conal
J
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;
    }
Jeromejeromy answered 11/7, 2013 at 6:16 Comment(1)
Method name is misspelled, and there is no super AFAICT.Cleave
J
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.

Jea answered 7/4, 2017 at 7:43 Comment(2)
there is no method like getGroupCount() in expandable list view??Oldcastle
If you really look close, you can see the method is on the adapterTrinee
C
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;
        }
    });
Cash answered 30/12, 2015 at 19:22 Comment(0)
B
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;
    }
Basic answered 30/7, 2015 at 11:41 Comment(0)
K
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);
                    }
                }
            }
        });
Kandicekandinsky answered 20/1, 2015 at 10:9 Comment(0)
H
-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);
            }
        });`
Homoeroticism answered 7/10, 2014 at 11:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.