7

I am using expandableListView for UI design, so I am wondering for Android expandable listview, is there a way to allow only one list item expanded, i.e. when you click and expand an item, all other items are collapsed automatically.

Thanks

Jimmy
  • 573
  • 4
  • 9
  • 14
  • 1
    Use answer from http://stackoverflow.com/questions/4314777/programmatically-collapse-a-group-in-expandablelistview – Londeren Jul 27 '11 at 18:20
  • 1
    I put some code in another question that does what you're looking for:http://stackoverflow.com/questions/4314777/programmatically-collapse-a-group-in-expandablelistview/4315162#4315162 – danh32 Jul 27 '11 at 18:20

3 Answers3

10

When you click one item you could loop through the rest and collapse each one except for the one you just clicked...

list.setOnGroupExpandListener(new OnGroupExpandListener() {

    public void onGroupExpand(int groupPosition) {
        int len = mAdapter.getGroupCount();

        for(int i=0; i<len; i++) {
            if(i != groupPosition) {
                list.collapseGroup(i);
            }
        }
    }

});
DRiFTy
  • 11,269
  • 11
  • 61
  • 77
1

You could do as kieran suggested, or if you only have one open at a time, you could just keep track of which one you last clicked. You could do this by declaring int lastclicked in the class body then in the listener as Korean suggested, put list.collapseGroup(lastclicked)

I would give a code example, but I'm on my mobile. Sorry.

But I just personally prefer the lastclicked method of doing it than using a for loop. It seems more efficient.

Reed
  • 14,703
  • 8
  • 66
  • 110
0

First of all implement OnGroupExpandListener in your Activity that will allow you to add its default method, and after add that default method you need to do like this:

@Override
    public void onGroupExpand(int groupPosition) {
        // TODO Auto-generated method stub
        int len = expadapter.getGroupCount();           
        for(int i=0;i<len;i++)
        {
            if(i!=groupPosition)
            {
                expandlst.collapseGroup(i);
            }
        }
    }
NicoTek
  • 1,127
  • 1
  • 14
  • 34