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?
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.
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;
}
});
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 incorrect –
Oligochaete 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.
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 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;
}
});
© 2022 - 2024 — McMap. All rights reserved.
ExpandableListView.OnGroupExpandListener
class. Also, you can keep track of the last expanded group, and collapse it when the next one is expanded (to prevent callingcollapseGroup()
on an arbitrarily large number of list items. – Nanny