I want to make only one item in the parent list expand at a time, my code for the onCreate is currently the following. (It works as I want, But the method to allow only one parent open at a time doesnt work)
public void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final SimpleExpandableListAdapter expListAdapter =
new SimpleExpandableListAdapter(
//FOR LEVEL 1 SCREEN
this,
createGroupList(), // METHOD TO CREATE PARENTLIST Creating group List.
R.layout.group_row, // REF TO XML FILENAME.
new String[] { "Group Item" }, // STRING ARRAY HOLDING ONE KEY THAT REF'S ALL PARENT LIST ITEMS.
new int[] { R.id.row_name }, // REF TO I.D. OF PARENT XML. ID of each group item.-Data under the key goes into this TextView.
createChildList(), // METHOD TO CREATE CHILD LST. childData describes second-level entries.
R.layout.child_row, // XMLFILE NAME FOR CHILD LIST.Layout for sub-level entries(second level).
new String[] {"Sub Item"}, // KEY FOR CHILD ITEMS IN HASHMAP. Keys in childData maps to display.
new int[] { R.id.grp_child} // XML ID FOR TEXTVIEW. Data under the keys above go into these TextViews.
);
setListAdapter( expListAdapter ); // setting the adapter in the list.
//**THIS IS WHAT I DONT KNOW HOW TO CODE:**
list = (ExpandableListView) findViewById(R.id.row_name);
list.setAdapter( expListAdapter);
list.setGroupIndicator(null);
list.setOnGroupExpandListener(new OnGroupExpandListener() {
public void onGroupExpand(int groupPosition) {
int len = expListAdapter.getGroupCount();
for (int i = 0; i < len; i++) {
if (i != groupPosition) {
list.collapseGroup(i);
}
}
}
});
}catch(Exception e){
System.out.println("Errrr +++ " + e.getMessage());
}
}
I understand the above code, However, I dont use expandableListViews, Im refferncing my program to my .xml files with the layouts of the expandablelist. And so I dont know how to apply the above to my program...
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ExpandableListView android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bkg"/>
<TextView android:id="@+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No items"/>
</LinearLayout>
The method that I dont know how to apply is the following:
list = (ExpandableListView) findViewById(R.id.row_name);
list.setAdapter( expListAdapter);
list.setGroupIndicator(null);
list.setOnGroupExpandListener(new OnGroupExpandListener() {
public void onGroupExpand(int groupPosition) {
int len = expListAdapter.getGroupCount();
for (int i = 0; i < len; i++) {
if (i != groupPosition) {
list.collapseGroup(i);
}
}
}
});
1) how do I refference the 'list' to my expandablelist activity?
list is defined in the class as private ExpandableListView list;
I do not have any errors right now, But the method does not work.
Any help is greatly appreciated!
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.
How do I change the icon on expandable list view? Post the listView row xml. @RajeshCP see update one. Onclick of the onItemClick function you can change the source of the group_indicator for that you need a uparrow button or else you can rotate the bitmap by some dregree and set it as a source for that ImageView.
You have to use Adapter's getChild(groupPosition, childPosition) method to retrieve the instance of child.
// Declare variable
private static int prev = -1;
// and OnGroupExpandListener implement
mExpandableList.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
if(prev!=-1)
{
mExpandableList.collapseGroup(prev);
}
prev=groupPosition;
}
});
Updating with updated solution
mExpandableList.setOnGroupExpandListener(new OnGroupExpandListener() {
// Keep track of previous expanded parent
int previousGroup = -1;
@Override
public void onGroupExpand(int groupPosition) {
// Collapse previous parent if expanded.
if ((previousGroup != -1) && (groupPosition != previousGroup)) {
mExpandableList.collapseGroup(previousGroup);
}
previousGroup = groupPosition;
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With