利用SimpleExpandableListAdapter为ExpandableListActivity提供数据
2015-08-13 22:31
501 查看
首先MainActivity继承自ExpandableListActivity,其中的声明如下:
setContentView(R.layout.expandmain);
//定义一个:List,该List对象为一级条目提供数据
List<Map<String,String>> parents = new ArrayList<Map<String,String>>();
Map<String,String> parent1 = new HashMap<String,String>();
parent1.put("group", "parent1");
Map<String,String> parent2 = new HashMap<String,String>();
parent2.put("group", "parent2");
parents.add(parent1);
parents.add(parent2);
//定义一个List,该List对象为第一个一级条目提供数据
List<Map<String,String>> child1 = new ArrayList<Map<String,String>>();
Map<String,String> childData1 = new HashMap<String,String>();
childData1.put("child", "child1Data1");
Map<String,String> childData2 = new HashMap<String,String>();
childData2.put("child", "child1Data2");
child1.add(childData1);
child1.add(childData2);
//定义一个List,该List对象为第二个一级条目提供数据
List<Map<String,String>> child2 = new ArrayList<Map<String,String>>();
Map<String,String> childData3 = new HashMap<String,String>();
childData3.put("child", "child1Data3");
Map<String,String> childData4 = new HashMap<String,String>();
childData4.put("child", "child1Data4");
child2.add(childData3);
child2.add(childData4);
//生成一个List,该List对象用来存储所有的二级条目的数据
List<List<Map<String,String>>> childs = new ArrayList<List<Map<String,String>>>();
childs.add(child1);
childs.add(child2);
//context
//一级条目的数据
//用来设置一级条目样式的布局文件
//指定一级条目数据的key
//指定一级条目显示控件的ID
//指定二级条目的数据
//用来设置二级条目的布局文件
//指定二级条目数据的key
//指定二级条目数据显示控件的ID
SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(this, parents, R.layout.parent, new String[]{"group"}, new int[] {R.id.parentTo}, childs, R.layout.child, new String[]{"child"},new int[]{R.id.childTo});
//将SimpleExpandableListAdapter设置给当前的ExpandableListActivity
setListAdapter(sela);
其中expandmain.xml文件中的内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ExpandableListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"/>
</LinearLayout>
parent.xml文件的内容如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@+id/parentTo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20px"
android:textSize="26sp"
android:text="No data"/>
</LinearLayout>
child.xml文件中的内容如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/childTo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20px"
android:textSize="20sp"
android:text="No data" />
</LinearLayout>
setContentView(R.layout.expandmain);
//定义一个:List,该List对象为一级条目提供数据
List<Map<String,String>> parents = new ArrayList<Map<String,String>>();
Map<String,String> parent1 = new HashMap<String,String>();
parent1.put("group", "parent1");
Map<String,String> parent2 = new HashMap<String,String>();
parent2.put("group", "parent2");
parents.add(parent1);
parents.add(parent2);
//定义一个List,该List对象为第一个一级条目提供数据
List<Map<String,String>> child1 = new ArrayList<Map<String,String>>();
Map<String,String> childData1 = new HashMap<String,String>();
childData1.put("child", "child1Data1");
Map<String,String> childData2 = new HashMap<String,String>();
childData2.put("child", "child1Data2");
child1.add(childData1);
child1.add(childData2);
//定义一个List,该List对象为第二个一级条目提供数据
List<Map<String,String>> child2 = new ArrayList<Map<String,String>>();
Map<String,String> childData3 = new HashMap<String,String>();
childData3.put("child", "child1Data3");
Map<String,String> childData4 = new HashMap<String,String>();
childData4.put("child", "child1Data4");
child2.add(childData3);
child2.add(childData4);
//生成一个List,该List对象用来存储所有的二级条目的数据
List<List<Map<String,String>>> childs = new ArrayList<List<Map<String,String>>>();
childs.add(child1);
childs.add(child2);
//context
//一级条目的数据
//用来设置一级条目样式的布局文件
//指定一级条目数据的key
//指定一级条目显示控件的ID
//指定二级条目的数据
//用来设置二级条目的布局文件
//指定二级条目数据的key
//指定二级条目数据显示控件的ID
SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(this, parents, R.layout.parent, new String[]{"group"}, new int[] {R.id.parentTo}, childs, R.layout.child, new String[]{"child"},new int[]{R.id.childTo});
//将SimpleExpandableListAdapter设置给当前的ExpandableListActivity
setListAdapter(sela);
其中expandmain.xml文件中的内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ExpandableListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"/>
</LinearLayout>
parent.xml文件的内容如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@+id/parentTo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20px"
android:textSize="26sp"
android:text="No data"/>
</LinearLayout>
child.xml文件中的内容如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/childTo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20px"
android:textSize="20sp"
android:text="No data" />
</LinearLayout>
相关文章推荐
- HUNAN 11569 Just Another Knapsack Problem(AC自动机+dp)
- UDP编程
- HDU 5389 Zero Escape(动态规划)——多校练习8
- 10个中文分词器
- manacher hihoCoder1032 最长回文子串
- 【HDU3943】【K-th Nya Number】【数位+二分找位置】
- Havok_2014-1-0_Pc_Xs_User_Guide(1.5.7-计时器)
- 工厂模式
- RADOS工作原理
- angularJs 过滤器
- POJ 2262 Goldbach's Conjecture(哥德巴赫猜想)
- 高级组件之自动完成文本框
- 【HDU】5390 tree【线段树套字典树】
- java 文件操作 读取txt文本(兄弟常开心)
- 教你怎么把一天24小时变成48小时?
- UIViewController _loadViewFromNibNamed:bundle:
- Linux时间子系统之四:定时器的引擎:clock_event_device
- 【POJ 1836】 Alignment
- URAL 1784 K - Rounders 找规律
- 部分真题整理