Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expandable ListView

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

like image 206
Jimmy Avatar asked Jul 27 '11 18:07

Jimmy


People also ask

What is expandable ListView?

Android ExpandableListView is a view that shows items in a vertically scrolling two-level list. It differs from a ListView by allowing two levels which are groups that can be easily expanded and collapsed by touching to view and their respective children items.


2 Answers

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

});
like image 64
DRiFTy Avatar answered Oct 12 '22 00:10

DRiFTy


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.

like image 1
Reed Avatar answered Oct 12 '22 00:10

Reed