您的位置:首页 > 移动开发 > Android开发

android-ExpandableListView(2)

2011-08-04 13:55 232 查看


package zhang.example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.Toast;

public class ExpandableListActivity extends android.app.ExpandableListActivity {

/** Called when the activity is first created. */
@SuppressWarnings("unchecked")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

List topList = new ArrayList();

Map topMap1 =new HashMap();
Map topMap2 =new HashMap();
Map topMap3 = new HashMap();
Map topMap4 = new HashMap();

topMap1.put("month", "1月测评项");
topMap2.put("month", "2月测评项");
topMap3.put("month" ,"3月测试项");
topMap4.put("month" ,"4月测试项");

topList.add(topMap1);
topList.add(topMap2);
topList.add(topMap3);
topList.add(topMap4);

List nestList = new ArrayList();

List nestList1 = new ArrayList();

Map nestMap1 = new HashMap();
Map nestMap2 = new HashMap();
Map nestMap3 = new HashMap();

nestMap1.put("test", "look");
nestMap2.put("test", "leat");
nestMap3.put("test", "play");

nestList1.add(nestMap1);
nestList1.add(nestMap2);
nestList1.add(nestMap3);

List nestList2= new ArrayList();
Map nestMap4 = new HashMap();
Map nestMap5 = new HashMap();

nestMap4.put("test", "trun");
nestMap5.put("test", "direction");

nestList2.add(nestMap4);
nestList2.add(nestMap5);

List nestList3= new ArrayList();
Map nestMap6 = new HashMap();
Map nestMap7 =new HashMap();
Map nestMap8 = new HashMap();

nestMap6.put("test", "aaa");
nestMap7.put("test", "bbb");
nestMap8.put("test", "ccc");

nestList3.add(nestMap6);
nestList3.add(nestMap7);
nestList3.add(nestMap8);

List nestList4 =new ArrayList();
Map nestMap9 = new HashMap();
Map nestMap10 = new HashMap();

nestMap9.put("test", "111");
nestMap10.put("test","222");

nestList4.add(nestMap9);
nestList4.add(nestMap10);

nestList.add(nestList1);
nestList.add(nestList2);
nestList.add(nestList3);
nestList.add(nestList4);

SimpleExpandableListAdapter adapter = new
SimpleExpandableListAdapter(
this,//1
topList, //2顶层数据列表
android.R.layout.simple_dropdown_item_1line, //3一层显示样式
new String[]{"month"}, //4顶层Map
new int[]{android.R.id.text1}, //5顶层数据显示的View
nestList, //6二层数据列表
android.R.layout.simple_list_item_1, //7二层显示的样式
new String[]{"test"}, //8二层map
new int[]{android.R.id.text1}//9二层数据的View
);
this.setListAdapter(adapter);
}
public boolean onClickClick(ExpandableListView parent,View v,int groupPosition,int ChildPosition ,long id){
Toast.makeText(this, "嵌套列表被点击,顶层列表定位"+groupPosition+"二层列表定位"+ChildPosition,Toast.LENGTH_LONG).show();
return super.onChildClick(parent, v, groupPosition, ChildPosition, id);

}
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(this, "顶层列表收缩,列表定位"+groupPosition, Toast.LENGTH_LONG).show();
super.onGroupCollapse(groupPosition);
}
@Override
public void onGroupExpand(int groupPosition) {
// TODO Auto-generated method stub
Toast.makeText(this, "顶层列表展开,列表定位"+groupPosition, Toast.LENGTH_LONG).show();
super.onGroupExpand(groupPosition);
}
}


<?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:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@id/android:list">

</ExpandableListView>

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: