布局文件
<?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:orientation="horizontal" >
<ExpandableListView
android:id="@+id/el_list"
android:layout_width="120dp"
android:layout_height="715dp"
android:layout_gravity="left"
android:background="#ffffff"
android:cacheColorHint="#abcdefff"
android:listSelector="#cdefabdc" >
<!-- android:cacheColorHint="#00000000",这个设置拖动view时背景色
android:listSelector="#00000000" ,设置选中时的底色 -->
</ExpandableListView>
<ExpandableListView
android:id="@+id/el_list2"
android:layout_width="120dp"
android:layout_height="698dp"
android:background="#ffffff"
android:cacheColorHint="#abcdefff"
android:listSelector="#cdefabdc" >
<!-- android:cacheColorHint="#00000000",这个设置拖动view时背景色
android:listSelector="#00000000" ,设置选中时的底色 -->
</ExpandableListView>
<ExpandableListView
android:id="@+id/el_list3"
android:layout_width="120dp"
android:layout_height="698dp"
android:background="#ffffff"
android:cacheColorHint="#abcdefff"
android:listSelector="#cdefabdc" >
<!-- android:cacheColorHint="#00000000",这个设置拖动view时背景色
android:listSelector="#00000000" ,设置选中时的底色 -->
</ExpandableListView>
<Button
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="返回"/>
</LinearLayout>
主函数
package com.example.test;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
public class TestExpandableListView2 extends Activity {
private ExpandableListView mView;
private ExpandableListView mView2;
private ExpandableListView mView3;
private String[] generalsTypes = new String[] { "作物: " };
//子视图显示文字
private String[][] generals = new String[][] {
{ "小麦", "玉米", "花生", "高粱", "水稻", "大豆" }};
private String[] generalsTypes2 = new String[] { "/亩" };
//子视图显示文字
private String[][] generals2 = new String[][] {
{ "200-250kg", "250-300kg", "300-400kg", "400-500kg" }};
private String[] generalsTypes3 = new String[] { "施肥模式" };
//子视图显示文字
private String[][] generals3 = new String[][] {
{ "化肥", "复合肥", "配方肥"}};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expandable_listview2);
mView =(ExpandableListView) findViewById(R.id.el_list);
mView.setAdapter(new MyAdapter());
mView2 =(ExpandableListView) findViewById(R.id.el_list2);
mView2.setAdapter(new MyAdapter2());
mView3 =(ExpandableListView) findViewById(R.id.el_list3);
mView3.setAdapter(new MyAdapter3());
//点击子条目的时候更新主条目的文字
mView.setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView v, View arg1, int group,
int child, long arg4) {
if(child==0){
generalsTypes = new String[] { "作物:小麦" };
mView.setAdapter(new MyAdapter());
}
if(child==1){
generalsTypes = new String[] { "作物:玉米" };
mView.setAdapter(new MyAdapter());
}
if(child==2){
generalsTypes = new String[] { "作物:花生" };
mView.setAdapter(new MyAdapter());
}
if(child==3){
generalsTypes = new String[] { "作物:高粱" };
mView.setAdapter(new MyAdapter());
}
if(child==4){
generalsTypes = new String[] { "作物:水稻" };
mView.setAdapter(new MyAdapter());
}if(child==5){
generalsTypes = new String[] { "作物:大豆" };
mView.setAdapter(new MyAdapter());
}
return false;
}
});
mView2.setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView v, View arg1, int group,
int child, long arg4) {
if(child==0){
generalsTypes2 = new String[] { "200-250kg/亩" };
mView2.setAdapter(new MyAdapter2());
}
if(child==1){
generalsTypes2 = new String[] { "250-300kg/亩" };
mView2.setAdapter(new MyAdapter2());
}
if(child==2){
generalsTypes2 = new String[] { "300-400kg/亩" };
mView2.setAdapter(new MyAdapter2());
}
if(child==3){
generalsTypes2 = new String[] { "400-500kg/亩" };
mView2.setAdapter(new MyAdapter2());
}
return false;
}
});
mView3.setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView v, View arg1, int group,
int child, long arg4) {
if(child==0){
generalsTypes3 = new String[] { "化肥" };
mView3.setAdapter(new MyAdapter3());
}
if(child==1){
generalsTypes3 = new String[] { "复合肥" };
mView3.setAdapter(new MyAdapter3());
}
if(child==2){
generalsTypes3 = new String[] { "配方肥" };
mView3.setAdapter(new MyAdapter3());
}
return false;
}
});
}
class MyAdapter extends BaseExpandableListAdapter {
//自己定义一个获得textview的方法
TextView getTextView() {
//设置条目的宽高
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 50);
TextView textView = new TextView(TestExpandableListView2.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL);
textView.setPadding(36, 0, 0, 0);
textView.setTextSize(20);
textView.setTextColor(Color.BLACK);
return textView;
}
@Override
public int getGroupCount() {
return generalsTypes.length;
}
@Override
public int getChildrenCount(int groupPosition) {
return generals[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
//返回的数据在后面填主条目充用
return generalsTypes[groupPosition];
}
@Override
public Object getChild(int groupPosition, int childPosition) {
//返回的数据在后面填充子条目用
return generals[groupPosition][childPosition];
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override //获得主条目view对象
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(TestExpandableListView2.this);
ll.setOrientation(0);
// ImageView logo = new ImageView(ExpandableList.this);
// logo.setImageResource(logos[groupPosition]);
// logo.setPadding(50, 0, 0, 0);
// ll.addView(logo);
TextView textView = getTextView();
textView.setTextColor(Color.BLUE);
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);
ll.setPadding(6, 0, 0, 0);
return ll;
}
@Override //将子条目内容加载到childview
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(TestExpandableListView2.this);
ll.setOrientation(0);
TextView textView = getTextView();
textView.setText(getChild(groupPosition, childPosition).toString());
ll.addView(textView);
return ll;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
class MyAdapter2 extends BaseExpandableListAdapter {
//自己定义一个获得textview的方法
TextView getTextView() {
//设置条目的宽高
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 50);
TextView textView = new TextView(TestExpandableListView2.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL);
textView.setPadding(36, 0, 0, 0);
textView.setTextSize(20);
textView.setTextColor(Color.BLACK);
return textView;
}
@Override
public int getGroupCount() {
return generalsTypes2.length;
}
@Override
public int getChildrenCount(int groupPosition) {
return generals2[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
//返回的数据在后面填主条目充用
return generalsTypes2[groupPosition];
}
@Override
public Object getChild(int groupPosition, int childPosition) {
//返回的数据在后面填充子条目用
return generals2[groupPosition][childPosition];
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
//获得主条目view对象,一个textview用于显示主条目
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(TestExpandableListView2.this);
ll.setOrientation(0);
TextView textView = getTextView();
textView.setTextColor(Color.BLUE);
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);
ll.setPadding(6, 0, 0, 0);
return ll;
}
@Override //将子条目内容加载到childview
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(TestExpandableListView2.this);
ll.setOrientation(0);
TextView textView = getTextView();
textView.setText(getChild(groupPosition, childPosition).toString());
ll.addView(textView);
return ll;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
class MyAdapter3 extends BaseExpandableListAdapter {
//自己定义一个获得textview的方法
TextView getTextView() {
//设置条目的宽高
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 50);
TextView textView = new TextView(TestExpandableListView2.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL);
textView.setPadding(36, 0, 0, 0);
textView.setTextSize(20);
textView.setTextColor(Color.BLACK);
return textView;
}
@Override
public int getGroupCount() {
return generalsTypes3.length;
}
@Override
public int getChildrenCount(int groupPosition) {
return generals3[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
//返回的数据在后面填主条目充用
return generalsTypes3[groupPosition];
}
@Override
public Object getChild(int groupPosition, int childPosition) {
//返回的数据在后面填充子条目用
return generals3[groupPosition][childPosition];
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
//获得主条目view对象,一个textview用于显示主条目
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(TestExpandableListView2.this);
ll.setOrientation(0);
TextView textView = getTextView();
textView.setTextColor(Color.BLUE);
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);
ll.setPadding(6, 0, 0, 0);
return ll;
}
@Override //将子条目内容加载到childview
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(TestExpandableListView2.this);
ll.setOrientation(0);
TextView textView = getTextView();
textView.setText(getChild(groupPosition, childPosition).toString());
ll.addView(textView);
return ll;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}
源码地址 http://download.csdn.net/detail/fei900724/8705943