ANDROID_MARS学习笔记_S02_004_ExpandableListActivity

1.main.xml

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context="com.example.s02_e03_expandablelistactivity.MainActivity" >
10
11     <ExpandableListView
12         android:layout_width="match_parent"
13         android:layout_height="match_parent"
14         android:id="@id/android:list"
15         android:background="#00FF00"
16         android:layout_weight="1"
17         android:drawSelectorOnTop="true"/>
18     <TextView
19         android:id="@id/android:empty"
20         android:layout_width="match_parent"
21         android:layout_height="match_parent"
22         android:background="#FF0000"
23         android:text="No data"/>
24
25 </RelativeLayout>

2.group.xml

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context="com.example.s02_e03_expandablelistactivity.MainActivity" >
10
11     <TextView android:id="@+id/groupTo"
12         android:layout_width="fill_parent"
13         android:layout_height="fill_parent"
14         android:paddingLeft="60px"
15         android:paddingTop="10px"
16         android:paddingBottom="10px"
17         android:textSize="26sp"
18         android:background="#cccccc"
19         android:text="No group data" />
20
21 </RelativeLayout>

3.child.xml

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context="com.example.s02_e03_expandablelistactivity.MainActivity" >
10
11     <TextView android:id="@+id/childTo"
12         android:layout_width="fill_parent"
13         android:layout_height="fill_parent"
14         android:paddingLeft="50px"
15         android:paddingTop="5px"
16         android:paddingBottom="5px"
17         android:textSize="20sp"
18         android:text="No child data" />
19
20 </RelativeLayout>

4.java

 1 package com.example.s02_e03_expandablelistactivity;
 2
 3 import java.util.ArrayList;
 4 import java.util.HashMap;
 5 import java.util.List;
 6 import java.util.Map;
 7
 8 import android.app.ExpandableListActivity;
 9 import android.os.Bundle;
10 import android.widget.SimpleExpandableListAdapter;
11
12 /*
13  * 创建一个Activity,继承ExpandableListAcitivty
14  */
15 public class MainActivity extends ExpandableListActivity {
16     /** Called when the activity is first created. */
17     @Override
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.activity_main);
21
22         //定义一个List,该List对象为一级条目提供数据
23         List<Map<String, String>> groups = new ArrayList<Map<String, String>>();
24         Map<String,String> group1 = new HashMap<String,String>();
25         group1.put("group", "group1");
26         Map<String, String> group2 = new HashMap<String, String>();
27         group2.put("group", "group2");
28         groups.add(group1);
29         groups.add(group2);
30
31         //定义一个List,该List对象为第一个一级条目提供二级条目的数据
32         List<Map<String,String>> child1 = new ArrayList<Map<String,String>>();
33         Map<String, String> child1Data1 = new HashMap<String, String>();
34         child1Data1.put("child", "child1Data1");
35         child1.add(child1Data1);
36         Map<String,String> child1Data2 = new HashMap<String,String>();
37         child1Data2.put("child", "child1Data2");
38         child1.add(child1Data2);
39
40         //定义一个List,该List对象为第二个一级条目提供二级条目的数据
41         List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();
42         Map<String, String> child2Data = new HashMap<String, String>();
43         child2Data.put("child", "child2Data");
44         child2.add(child2Data);
45
46         //定义一个List,该List对象用来存储所有的二级条目的数据
47         List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();
48         childs.add(child1);
49         childs.add(child2);
50
51         //生成一个SimpleExpandableListAdapter对象
52         //1.context
53         //2.一级条目的数据
54         //3.用来设置一级条目样式的布局文件
55         //4.指定一级条目数据的key
56         //5.指定一级条目数据显示控件的id
57         //6.指定二级条目的数据
58         //7.用来设置二级条目样式的布局文件
59         //8.指定二级条目数据的key
60         //9.指定二级条目数据显示控件的id
61         SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(this, groups, R.layout.group, new String[] {"group"}, new int[] {R.id.groupTo}, childs, R.layout.child, new String[] {"child"}, new int[] {R.id.childTo});
62         //将SimpleExpandableListAdapter对象设置给当前的ExpandableListActivity
63         setListAdapter(sela);
64     }
65
66 }
时间: 2024-08-02 02:43:21

ANDROID_MARS学习笔记_S02_004_ExpandableListActivity的相关文章

ANDROID_MARS学习笔记_S03_007_GoogleMap1

一.简介 二.代码1.xml(1)main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fi

ANDROID_MARS学习笔记_S04_004_用HTTPCLENT发带参数的get和post请求

一.代码 1.xml(1)activity_main.xml 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="

ANDROID_MARS学习笔记_S02_012_ANIMATION_利用AnimationListener在动画结束时删除或添加组件

一.代码 1.xml(1)activity_main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:id="@+id/layoutId" 4 android:orientation="

ANDROID_MARS学习笔记_S01原始版_013_广播机制二

一.代码1.xml(1)main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_pa

ANDROID_MARS学习笔记_S01原始版_012_广播机制一

一.简介 二.代码1.xml(1)activity_main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width

ANDROID_MARS学习笔记_S01原始版_007_Handler及线程的简单使用

一.运行结果 一.代码1.xml(1)activity_main.xml 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_heig

ANDROID_MARS学习笔记_S01原始版_008_Handler(异步消息处理机制)

一.代码1.xml(1)main.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_pa

ANDROID_MARS学习笔记_S02_005_AppWidget1

一.AppWidget介绍 1.Widget的定义创建流程 (1)在res新建xml文件夹,新建appwidget-provider.xml,为widget提供元数据 (2)在res/layout文件夹下定义widget的样式 (3)在src新建类,extends AppWidgetProvider,自定义widget (4)在AndroidManifest.xml中把自定义的widget添加为receiver,接收APPWIDGET_UPDATE广播 2. 二.代码 1.res/xml/exa

ANDROID_MARS学习笔记_S01_007Linear_layout嵌套与layout_weight的设置

一.介绍 二.1.linear_layout.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height=&