I created an ExpandableListView with a custom adapter and all works perfectly including the click, collapse and expand events. If anyone needs assistance with the expandable listview I followed the tutorial at: http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/.
I now want to write code in my setOnGroupExpandListener method to collapse all other Groups that are open apart from the one that is currently focussed. (Content aware - in a sense) To clarify, if I have the following:
Item 1
Item 2
Item 3
If I click on Item 1, it should expand Item 1 (this already works) and it should collapse Item 2 and Item 3 groups for me. Is this possible and how can I achieve that?
Here is my current setOnGroupExpandListener:
gpsMenuListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(), "Expanded: " + gpsMenuListDataHeader.get(groupPosition).toString(), Toast.LENGTH_SHORT).show();
}
});
As you can see I currently only have a Toast that prints a message but I want to add the collapse functionality.
listView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
YourExpandableListAdapter customExpandAdapter = (YourExpandableListAdapter)listView.getExpandableListAdapter();
if (customExpandAdapter == null) {return;}
for (int i = 0; i < customExpandAdapter.getGroupCount(); i++) {
if (i != groupPosition) {
listView.collapseGroup(i);
}
}
}
});