Frage

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?

War es hilfreich?

Lösung

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.

Andere Tipps

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

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

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; 
         }
        });
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top