2010-06-24 14 views

Respuesta

7

Me encontré con esto hace dos días. Aquí está mi solución:

Primera extender ExpandableListActivity y reemplazar el método onCreate() así:

public class ExpandableExample extends ExpandableListActivity { 
    @Override 
    public void onCreate() { 
     super.onCreate(savedInstanceState); 
     setListAdapter(new BaseExpandableListAdapterExample()); 
    } 
} 

La definición de BaseExpanableListAdapterExample es (clase interna de ExpandableExample):

protected class BaseExpandableListAdapterExample extends BaseExpandableListAdapter { 
} 

entonces usted necesita para implementar getChildView y getGroupView. Lo que hice fue:

public View getGroupView(int groupPosition, boolean isExpanded, 
         View convertView, ViewGroup parent) { 
    View groupRow = getLayoutInflater().inflate(R.layout.group_row, null); 
    TextView textView = (TextView) groupRow.findViewById(R.id.text_group); 
    textView.setText(getGroup(groupPosition).toString()); 
    return groupRow; 
} 

public View getChildView(int groupPosition, int childPosition, 
         boolean isLastChild, View convertView, ViewGroup parent) { 
    View childRow = getLayoutInflater().inflate(R.layout.child_row, null); 
    TextView textView = (TextView) childRow.findViewById(R.id.text_child); 
    textView.setText(getChild(groupPosition, childPosition).toString()); 
    return childRow; 
} 

Luego hay que definir tanto group_row.xml y child_row.xml bajo /res/layout/ directorio. Por ejemplo, mi group_row.xml es el siguiente:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
    <TextView 
     android:id="@+id/text_element" 
     android:layout_width="fill_parent" 
     android:layout_height="48dip" 
     android:textSize="20sp" 
     android:textColor="#fff" 
     android:layout_marginLeft="48dip" 
     android:gravity="center_vertical" /> 
</RelativeLayout> 

Espero que ayude. Comente si tiene alguna pregunta.

+0

Muchas gracias. Eso es genial, pero también necesito recuperar contenido de XML. ¿Tal vez debería considerar analizar mi XML manualmente a estructuras de datos adecuadas para usar en SimpleExpandableListAdapter? –

+0

Aleksander, consulte http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/ExpandableList1.html para ver un ejemplo sobre cómo llenar manualmente una ExpandableListView, sin usar un Adaptador. – licorna

+0

¿Estás seguro de que el enlace es correcto? Ese código usa un proveedor. Simplemente define su propio :) Pero la creación de un proveedor propio que analizará datos XML parece una solución. –

Cuestiones relacionadas