Quiero hacer que solo un elemento de la lista principal se expanda a la vez, mi código para onCreate es actualmente el siguiente. (Funciona como yo quiero, sino el método para permitir que sólo un padre abierto en un tiempo no funciona)ExpandableLists en Android, quiero permitir que solo una lista primaria se expanda a la vez
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());
}
}
entiendo el código anterior, sin embargo, yo no uso expandableListViews, Im refferncing mi programa a mi .xml archivos con los diseños de la lista desplegable. Y así que no sé cómo aplicar lo anterior a mi programa ...
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>
El método que no sé cómo aplicar es la siguiente:
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) ¿cómo refiero la 'lista' a mi actividad expandiblelist?
lista se define en la clase como private ExpandableListView list;
no tengo ningún error en este momento, pero el método no funciona.
¡Cualquier ayuda es muy apreciada!
Todavía estoy atascado en este .. – user1180937
¿No hay manera de que pueda topar esto en la parte superior de los listados? – user1180937
puede encontrar la solución en el siguiente enlace: http://stackoverflow.com/questions/4314777/programmatically-collapse-a-group-in-expandablelistview –