码迷,mamicode.com
首页 > 移动开发 > 详细

ANDROID_MARS学习笔记_S02_004_ExpandableListActivity

时间:2016-02-12 18:39:52      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

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 }

 

ANDROID_MARS学习笔记_S02_004_ExpandableListActivity

标签:

原文地址:http://www.cnblogs.com/shamgod/p/5187151.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!