mainActivity.java
import java.util.ArrayList;
import java.util.List;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.widget.ExpandableListView;
public class mainActivity extends ExpandableListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
List<String> gl = new ArrayList<String>();
List<List<String>> cl = new ArrayList<List<String>>();
gl.add("gr1");
gl.add("gr2");
gl.add("gr3");
List<String> chil1 = new ArrayList<String>();
chil1.add("c11");
chil1.add("c12");
chil1.add("c13");
chil1.add("c14");
chil1.add("c15");
chil1.add("c16");
chil1.add("c17");
chil1.add("c18");
List<String> chil2 = new ArrayList<String>();
chil2.add("c21");
chil2.add("c22");
chil2.add("c23");
chil2.add("c24");
chil2.add("c25");
chil2.add("c26");
chil2.add("c27");
chil2.add("c28");
List<String> chil3 = new ArrayList<String>();
chil3.add("c31");
chil3.add("c32");
chil3.add("c33");
chil3.add("c34");
chil3.add("c35");
chil3.add("c36");
chil3.add("c37");
chil3.add("c38");
cl.add(chil1);
cl.add(chil2);
cl.add(chil3);
adapter e = new adapter(this, gl, cl);
ExpandableListView expandableListView = getExpandableListView();
// 设置适配器
expandableListView.setAdapter(e);
// 设置组图标 可以使用selector
expandableListView.setGroupIndicator(getResources().getDrawable(
R.drawable.away));
// 设置组图标位置
expandableListView.setIndicatorBounds(0, 40);
// 设置子项图标
expandableListView.setChildIndicator(getResources().getDrawable(
R.drawable.busy));
// 设置子项图标位置
expandableListView.setChildIndicatorBounds(0, 40);
}
}
adapter.java
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
/**
* @version 2012-8-27 上午10:36:36
**/
public class adapter extends BaseExpandableListAdapter {
List<String> GroupList = new ArrayList<String>();
List<List<String>> ChildrenList = new ArrayList<List<String>>();
Context context = null;
public adapter(Context c, List<String> gl, List<List<String>> cl) {
context = c;
GroupList.addAll(gl);
ChildrenList.addAll(cl);
}
// 返回组的总数
@Override
public int getGroupCount() {
return GroupList.size();
}
// 返回子项总数
@Override
public int getChildrenCount(int groupPosition) {
return ChildrenList.get(groupPosition).size();
}
// 返回组对象
@Override
public Object getGroup(int groupPosition) {
return GroupList.get(groupPosition);
}
// 返回子项对象
@Override
public Object getChild(int groupPosition, int childPosition) {
return ChildrenList.get(groupPosition).get(childPosition);
}
// 返回组ID
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
// 返回子项ID
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
// 是否具有固定ID
@Override
public boolean hasStableIds() {
return false;
}
// 返回组VIEW
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView tv = new TextView(context);
tv.setText(GroupList.get(groupPosition));
tv.setHeight(40);
return tv;
}
// 返回子项VIEW
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView tv = new TextView(context);
tv.setText(ChildrenList.get(groupPosition).get(groupPosition));
tv.setHeight(40);
return tv;
}
// 子项是否可以被选中
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
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/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ExpandableListView>
</LinearLayout>