public class ExpandableListViewActivity extends Activity {
//声明对象
private SimpleExpandableListAdapter adapter=null;
List<Map<String,String>> groups;
List<List<Map<String,String>>> childs;
ExpandableListView expandableListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expandable_list_view);
//为ExpandableListView 准备数据
groups=new ArrayList<Map<String,String>>();
Map<String,String> group1= new HashMap<String, String>();
group1.put("group", "我的家人");
Map<String,String> group2=new HashMap<String,String>();
group2.put("group", "我的朋友");
groups.add(group1);
groups.add(group2);
childs =new ArrayList<List<Map<String,String>>>();
List<Map<String,String>> child1=new ArrayList<Map<String,String>>();
Map<String,String> child1Data1=new HashMap<String, String>();
child1Data1.put("child", "妈妈");
child1.add(child1Data1);
Map<String,String> child1Data2=new HashMap<String, String>();
child1Data2.put("child", "爸爸");
child1.add(child1Data2);
List<Map<String,String>> child2=new ArrayList<Map<String,String>>();
Map<String,String> child1Data3=new HashMap<String, String>();
child1Data3.put("child", "小飞");
child2.add(child1Data3);
Map<String,String> childData4=new HashMap<String, String>();
childData4.put("child", "小武");
child2.add(childData4);
childs.add(child1);
childs.add(child2);
//得到ExpandableListView对象
expandableListView =(ExpandableListView)findViewById(R.id.expandable_1);
//实例化适配器
adapter=new SimpleExpandableListAdapter(this,groups,R.layout.tree_group,
new String[]{"group"},new int[]{R.id.tree_group_textView},
childs,R.layout.tree_child,new String[]{"child"},new int[]{R.id.textView1}
);
//设置适配器
expandableListView.setAdapter(adapter);
//设置监听器
expandableListView.setOnChildClickListener(new OnChildClickListener(){
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Log.d("text","group"+groupPosition);
Log.d("text", "child"+childPosition);
return false;
}});
}
}
//声明对象
private SimpleExpandableListAdapter adapter=null;
List<Map<String,String>> groups;
List<List<Map<String,String>>> childs;
ExpandableListView expandableListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expandable_list_view);
//为ExpandableListView 准备数据
groups=new ArrayList<Map<String,String>>();
Map<String,String> group1= new HashMap<String, String>();
group1.put("group", "我的家人");
Map<String,String> group2=new HashMap<String,String>();
group2.put("group", "我的朋友");
groups.add(group1);
groups.add(group2);
childs =new ArrayList<List<Map<String,String>>>();
List<Map<String,String>> child1=new ArrayList<Map<String,String>>();
Map<String,String> child1Data1=new HashMap<String, String>();
child1Data1.put("child", "妈妈");
child1.add(child1Data1);
Map<String,String> child1Data2=new HashMap<String, String>();
child1Data2.put("child", "爸爸");
child1.add(child1Data2);
List<Map<String,String>> child2=new ArrayList<Map<String,String>>();
Map<String,String> child1Data3=new HashMap<String, String>();
child1Data3.put("child", "小飞");
child2.add(child1Data3);
Map<String,String> childData4=new HashMap<String, String>();
childData4.put("child", "小武");
child2.add(childData4);
childs.add(child1);
childs.add(child2);
//得到ExpandableListView对象
expandableListView =(ExpandableListView)findViewById(R.id.expandable_1);
//实例化适配器
adapter=new SimpleExpandableListAdapter(this,groups,R.layout.tree_group,
new String[]{"group"},new int[]{R.id.tree_group_textView},
childs,R.layout.tree_child,new String[]{"child"},new int[]{R.id.textView1}
);
//设置适配器
expandableListView.setAdapter(adapter);
//设置监听器
expandableListView.setOnChildClickListener(new OnChildClickListener(){
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Log.d("text","group"+groupPosition);
Log.d("text", "child"+childPosition);
return false;
}});
}
}