ExpandableListView personalizado

De WikiCode

Saltar a navegación, buscar
Delicious

Contenido

Objetivo

El código muestra la forma más simple de redefinir la clase BaseExpandableListAdapter para rellenar una lista desplegable con el formato que queramos.

Archivos

ExpandableListView_Personalizado.java

package wikicode.es.ExpandableListView_Personalizado;
 
import java.util.ArrayList;
 
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
 
public class ExpandableListView_Personalizado extends Activity {
 
	private ArrayList<String> grupos;
	private ArrayList<ArrayList<ArrayList<String>>> hijos;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        setContentView(R.layout.main);
 
        ExpandableListView l = (ExpandableListView) findViewById(R.id.ExpandableListView01);
 
        cargarDatos();
 
        miExpandableAdapter adaptador = new miExpandableAdapter(this, grupos, hijos);
		l.setAdapter(adaptador);
    }
 
    public class miExpandableAdapter extends BaseExpandableListAdapter {
 
    	private ArrayList<String> groups;
 
        private ArrayList<ArrayList<ArrayList<String>>> children;
 
    	private Context context;
 
    	public miExpandableAdapter(Context context, ArrayList<String> groups, ArrayList<ArrayList<ArrayList<String>>> children) {
            this.context = context;
            this.groups = grupos;
            this.children = hijos;
        }
 
 
    	@Override
        public boolean areAllItemsEnabled()
        {
            return true;
        }
 
 
        @Override
        public ArrayList<String> getChild(int groupPosition, int childPosition) {
            return children.get(groupPosition).get(childPosition);
        }
 
        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }
 
 
        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild,View convertView, ViewGroup parent) {
 
        	String hijo = (String) ((ArrayList<String>)getChild(groupPosition, childPosition)).get(0);
 
            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.expandablelistview_hijo, null);
            }
 
            TextView hijotxt = (TextView) convertView.findViewById(R.id.TextViewHijo01);
 
            hijotxt.setText(hijo);
 
            return convertView;
        }
 
        @Override
        public int getChildrenCount(int groupPosition) {
            return children.get(groupPosition).size();
        }
 
        @Override
        public String getGroup(int groupPosition) {
            return groups.get(groupPosition);
        }
 
        @Override
        public int getGroupCount() {
            return groups.size();
        }
 
        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }
 
        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
 
        	String group = (String) getGroup(groupPosition);
 
        	if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.expandablelistview_grupo, null);
            }
 
            TextView grouptxt = (TextView) convertView.findViewById(R.id.TextViewGrupo);
 
            grouptxt.setText(group);
 
            return convertView;
        }
 
        @Override
        public boolean hasStableIds() {
            return true;
        }
 
        @Override
        public boolean isChildSelectable(int arg0, int arg1) {
            return true;
        }
 
    }
 
    private void cargarDatos(){
    	grupos= new ArrayList<String>();
    	hijos= new ArrayList<ArrayList<ArrayList<String>>>();
 
    	grupos.add("Grupo 1");
        grupos.add("Grupo 2");
        grupos.add("Grupo 3");
 
        hijos.add(new ArrayList<ArrayList<String>>());
        hijos.get(0).add(new ArrayList<String>());
        hijos.get(0).get(0).add("Hijo 1 grupo 1");
        hijos.get(0).add(new ArrayList<String>());
        hijos.get(0).get(1).add("Hijo 2 grupo 1");
        hijos.get(0).add(new ArrayList<String>());
        hijos.get(0).get(2).add("Hijo 3 grupo 1");
 
        hijos.add(new ArrayList<ArrayList<String>>());
        hijos.get(1).add(new ArrayList<String>());
        hijos.get(1).get(0).add("Hijo 1 grupo 2");
        hijos.get(1).add(new ArrayList<String>());
        hijos.get(1).get(1).add("Hijo 2 grupo 2");
        hijos.get(1).add(new ArrayList<String>());
        hijos.get(1).get(2).add("Hijo 3 grupo 2");
 
        hijos.add(new ArrayList<ArrayList<String>>());
        hijos.get(2).add(new ArrayList<String>());
        hijos.get(2).get(0).add("Hijo 1 grupo 3");
        hijos.get(2).add(new ArrayList<String>());
        hijos.get(2).get(1).add("Hijo 2 grupo 3");
        hijos.get(2).add(new ArrayList<String>());
        hijos.get(2).get(2).add("Hijo 3 grupo 3");
    }
}

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"
>
 
	<TextView  
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    android:text="WikiCode - ExpandableListView Personalizado" 
    />
 
	<ExpandableListView 
		android:id="@+id/ExpandableListView01" 
		android:layout_height="wrap_content"
 		android:layout_width="fill_parent"
 	>
 	</ExpandableListView>
</LinearLayout>

expandablelistview_hijo.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:background="#666666"
>
 
	<TextView
		android:id="@+id/TextViewHijo01" 
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_marginLeft="30px"
	>
	</TextView>
 
	<TextView
		android:id="@+id/TextViewHijo02" 
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_marginLeft="10px"
	>
	</TextView>
 
	<TextView
		android:id="@+id/TextViewHijo03" 
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_marginLeft="10px"
	>
	</TextView>
 
</LinearLayout>

expandablelistview_grupo.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
>
 
	<TextView
		android:id="@+id/TextViewGrupo" 
		android:layout_width="wrap_content"
		android:layout_height="50px"
		android:layout_marginLeft="50px"
		android:gravity="center_vertical"
	>
	</TextView>
 
</LinearLayout>

Resultado

ExpandableListView Personalizado.png

¿Te ha sido útil este artículo?

20 Valoración: 8.0/10 (11 votos)

Herramientas personales
Espacios de nombres
Variantes
Acciones
Navegación
Otros idiomas
Categorías
support
Sitios
Herramientas