steve-gregory
steve-gregory

Reputation: 7456

Show only one child of expandable list at a time

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?

Upvotes: 44

Views: 25876

Answers (4)

Dasser Basyouni
Dasser Basyouni

Reputation: 3252

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

Upvotes: 0

Blundell
Blundell

Reputation: 76466

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

Upvotes: 142

Dilshad
Dilshad

Reputation: 369

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

Upvotes: -5

bos
bos

Reputation: 6535

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.

Upvotes: 37

Related Questions