ExpandableListActivity 与ExpandableListView的关系就向 ListActivity与ListView一样总是一起出现的
先上个个效果图
1.首先在main.xml添加ExpandableListView
- <?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" //ExpandableListView的ID为Android自定义,不可改变
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/background"/>
- </LinearLayout>
2.为父目录创建布局文件
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <TextView
- android:id="@+id/parent_group"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:paddingTop="8px"
- android:paddingLeft="50px"
- android:paddingBottom="6px"
- android:textColor="#ff00ff"
- android:textStyle="bold"/>
- </LinearLayout>
3.为子目录创建布局文件
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:paddingLeft="20dp"
- android:orientation="horizontal">
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:paddingLeft="20px"
- android:paddingTop="4dp"
- android:background="@drawable/child_image"/>
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <TextView
- android:id="@+id/child_group"
- android:textSize="20sp"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="地址:xx省xx市xx"/>
- </LinearLayout>
- </LinearLayout>
4.DisplayExpandableList.java
- package com.yin;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import android.app.ExpandableListActivity;
- import android.os.Bundle;
- import android.widget.ExpandableListAdapter;
- import android.widget.ExpandableListView;
- import android.widget.SimpleExpandableListAdapter;
- public class DisplayExpandableList extends ExpandableListActivity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- List<HashMap<String, String>> parent_groups = new ArrayList<HashMap<String, String>>();
- String[] parent_group_names = {"我的好友","初中同学","高中同学","大学同学","黑名单"};
- parent_groups = this.addParentItems(parent_groups, parent_group_names);
- List<List<HashMap<String, String>>> child_groups = new ArrayList<List<HashMap<String, String>>>();
- List<HashMap<String, String>> child_lists = new ArrayList<HashMap<String, String>>();
- child_groups = this.addChildsItems(child_groups, child_lists,
- "小华");
- /**
- * SimpleExpandableListAdapter的参数那是相当的多啊
- * 参数 1:context
- * 2:父级目录的数据
- * 3:父级目录的布局文件
- * 4: 夫级目录的数据来源
- * 5:指定父级目录显示数据的控件
- * 6:子级目录的数据
- * 7:子级目录的布局文件
- * 8:子级目录的数据来源
- * 9:指定子级目录显示数据的控件
- */
- SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
- this, parent_groups, R.layout.parent_layout,
- new String[]{"parent_group"}, new int[]{R.id.parent_group},
- child_groups, R.layout.child_layout, new String[]{"child_group"},
- new int[]{R.id.child_group});
- setListAdapter(adapter);
- }
- //为每个父母目录下子目录添加数据
- public List<List<HashMap<String, String>>> addChildsItems(
- List<List<HashMap<String, String>>> child_groups,
- List<HashMap<String, String>> child_list, String item) {
- child_groups = new ArrayList<List<HashMap<String, String>>>();
- child_list = new ArrayList<HashMap<String, String>>();
- HashMap<String, String> map = new HashMap<String, String>();
- map.put("child_group", item);
- child_list.add(map);
- child_groups.add(child_list);
- return child_groups;
- }
- //为父级目录分组,添加标识
- public List<HashMap<String, String>> addParentItems(
- List<HashMap<String, String>> parent_groups, String[] parent_group_names) {
- parent_groups = new ArrayList<HashMap<String, String>>();
- for(int i=0;i<parent_group_names.length;i++){
- HashMap<String, String> groups = new HashMap<String, String>();
- groups.put("parent_group", parent_group_names[i]);
- parent_groups.add(groups);
- }
- return parent_groups;
- }
- }