Custom ExpandableListView

From WikiCode

Jump to: navigation, search

Contents

Objective

For android development is sometimes necessary to create customs ListViews. The code shows the simplest way to redefine the class BaseExpandableListAdapter to fill a dropdown list in the format you want.

Files

  • Cuestom_ExpandableListView.java
  • main.xml
  • expandablelistview_child.xml
  • expandablelistview_group.xml

Custom_ExpandableListView.java

package wikicode.es.Custom_ExpandableListView;
 
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 Custom_ExpandableListView extends Activity {
 
	private ArrayList<String> groups;
	private ArrayList<ArrayList<ArrayList<String>>> childs;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        setContentView(R.layout.main);
 
        ExpandableListView l = (ExpandableListView) findViewById(R.id.ExpandableListView01);
 
        loadData();
 
        myExpandableAdapter adapter = new myExpandableAdapter(this, groups, childs);
		l.setAdapter(adapter);
    }
 
    public class myExpandableAdapter extends BaseExpandableListAdapter {
 
    	private ArrayList<String> groups;
 
        private ArrayList<ArrayList<ArrayList<String>>> children;
 
    	private Context context;
 
    	public myExpandableAdapter(Context context, ArrayList<String> groups, ArrayList<ArrayList<ArrayList<String>>> children) {
            this.context = context;
            this.groups = groups;
            this.children = childs;
        }
 
 
    	@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 child = (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_child, null);
            }
 
            TextView childtxt = (TextView) convertView.findViewById(R.id.TextViewChild01);
 
            childtxt.setText(child);
 
            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_group, null);
            }
 
            TextView grouptxt = (TextView) convertView.findViewById(R.id.TextViewGroup);
 
            grouptxt.setText(group);
 
            return convertView;
        }
 
        @Override
        public boolean hasStableIds() {
            return true;
        }
 
        @Override
        public boolean isChildSelectable(int arg0, int arg1) {
            return true;
        }
 
    }
 
    private void loadData(){
    	groups= new ArrayList<String>();
    	childs= new ArrayList<ArrayList<ArrayList<String>>>();
 
    	groups.add("Group 1");
        groups.add("Group 2");
        groups.add("Group 3");
 
        childs.add(new ArrayList<ArrayList<String>>());
        childs.get(0).add(new ArrayList<String>());
        childs.get(0).get(0).add("Child 1 group 1");
        childs.get(0).add(new ArrayList<String>());
        childs.get(0).get(1).add("Child 2 group 1");
        childs.get(0).add(new ArrayList<String>());
        childs.get(0).get(2).add("Child 3 group 1");
 
        childs.add(new ArrayList<ArrayList<String>>());
        childs.get(1).add(new ArrayList<String>());
        childs.get(1).get(0).add("Child 1 group 2");
        childs.get(1).add(new ArrayList<String>());
        childs.get(1).get(1).add("Child 2 group 2");
        childs.get(1).add(new ArrayList<String>());
        childs.get(1).get(2).add("Child 3 group 2");
 
        childs.add(new ArrayList<ArrayList<String>>());
        childs.get(2).add(new ArrayList<String>());
        childs.get(2).get(0).add("Child 1 group 3");
        childs.get(2).add(new ArrayList<String>());
        childs.get(2).get(1).add("Child 2 group 3");
        childs.get(2).add(new ArrayList<String>());
        childs.get(2).get(2).add("Child 3 group 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 - Custom ExpandableListView" 
    />
 
	<ExpandableListView 
		android:id="@+id/ExpandableListView01" 
		android:layout_height="wrap_content"
 		android:layout_width="fill_parent"
 	>
 	</ExpandableListView>
</LinearLayout>

expandablelistview_child.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/TextViewChild01" 
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_marginLeft="30px"
	>
	</TextView>
 
	<TextView
		android:id="@+id/TextViewChild02" 
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_marginLeft="10px"
	>
	</TextView>
 
	<TextView
		android:id="@+id/TextViewChild03" 
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_marginLeft="10px"
	>
	</TextView>
 
</LinearLayout>

expandablelistview_group.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/TextViewGroup" 
		android:layout_width="wrap_content"
		android:layout_height="50px"
		android:layout_marginLeft="50px"
		android:gravity="center_vertical"
	>
	</TextView>
 
</LinearLayout>

Results

Custom ExpandableListView.png

Personal tools
Namespaces
Variants
Actions
Navigation
In other languages
Categories
support
Sites
Toolbox