Show only one child of expandable list at a time
Asked Answered
H

4

44

Is it possible to only expand one child of an ExpandableListView at a time, thus opening a second child would close the previously opened child?

Heshvan answered 22/10, 2011 at 20:32 Comment(0)
F
37

I'm not aware of any automatic methods for this, but you can implement ExpandableListView.OnGroupClickListener, in where you run collapseGroup() for all list groups except the one being clicked. This will do what you want.

Frentz answered 22/10, 2011 at 21:57 Comment(3)
Better yet, use the ExpandableListView.OnGroupExpandListener class. Also, you can keep track of the last expanded group, and collapse it when the next one is expanded (to prevent calling collapseGroup() on an arbitrarily large number of list items.Nanny
Thanks guys, I will try out your suggestions tonight!Heshvan
Just wanted to follow up that this suggestion worked perfectly, I just keep track of the last expanded group and close it before expanding the next group to be expanded! Thanks bos & Jason!Heshvan
O
142

Just to confirm bos's answer in code:

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

        @Override
        public void onGroupExpand(int groupPosition) {
            if(groupPosition != previousGroup)
                expandableList.collapseGroup(previousGroup);
            previousGroup = groupPosition;
        }
    });
Oligochaete answered 6/4, 2013 at 21:7 Comment(5)
can we get group view when user group expand event firePaperboard
Shoudn't "int previousGroup = -1" be "int previousGroup = 0" ?Quintana
@Quintana no because 0 is a valid position in the list, so when you start you there is no "previous group" therefore -1 is correct and 0 would be incorrectOligochaete
Perfect answer. Thanks mate. Also, I have a concern, while expanding particular group, the view automatically scrolls to the last child of the expanded group... How to make the scrollstate to be on the group instead of child? What happens is, if the child has more than 10+ records and it goes beyond the mobile height and screen scroll down to the last child pos. @Oligochaete need your assistanceLustral
Hello Everyone,This scroll expandablelistview scroll to bottom,how can I stop expandablelistview to scroll bottom. I tried android:transcriptMode="disabled" but not works for me any help will be appreciated.Tuesday
F
37

I'm not aware of any automatic methods for this, but you can implement ExpandableListView.OnGroupClickListener, in where you run collapseGroup() for all list groups except the one being clicked. This will do what you want.

Frentz answered 22/10, 2011 at 21:57 Comment(3)
Better yet, use the ExpandableListView.OnGroupExpandListener class. Also, you can keep track of the last expanded group, and collapse it when the next one is expanded (to prevent calling collapseGroup() on an arbitrarily large number of list items.Nanny
Thanks guys, I will try out your suggestions tonight!Heshvan
Just wanted to follow up that this suggestion worked perfectly, I just keep track of the last expanded group and close it before expanding the next group to be expanded! Thanks bos & Jason!Heshvan
R
0

Also you can simple use the withOnlyOneExpandedItem() which is in FastAdapter

that is in FastAdapter External Library by mikepenz

For example as I am using it in my Drawer

drawer.getAdapter().withOnlyOneExpandedItem(true);
Reitman answered 4/1, 2017 at 14:58 Comment(2)
As I know there is no such method in ExpandableListVeiw.Ricardo
sorry, I forgot that the source code I was reading is for an external library, also here it's link if are interested in it github.com/sathishmscict/FastAdapter/blob/master/library/src/…Reitman
D
-5

you can use this for various conditions as you want to do in expansion of list -

      expList.setOnChildClickListener(new OnChildClickListener() 
        {
         @Override
         public boolean onChildClick(ExpandableListView parent, View v,
           int childGroupPosition, int childPosition, long id) {

        // Log.e("OnChildClickListener", "OK "+childGroupPosition+"  "+childPosition);javainterviewquestion
             if(childGroupPosition ==0 && childPosition == 0)
             {

             }
             if(childGroupPosition ==0 && childPosition == 1)
             {

             } 

          return false; 
         }
        });
Dena answered 24/11, 2012 at 6:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.