码迷,mamicode.com
首页 > 其他好文 > 详细

仿qq联系人 学习笔记---ExpandableListActivity的使用

时间:2016-04-25 19:23:55      阅读:344      评论:0      收藏:0      [点我收藏+]

标签:

【转】原地址  http://blog.163.com/xygzx@126/blog/static/237809502011102010100331/

效果显示图:

技术分享

1.布局文件

main.xml(ExpandableListActivity布局文件)

注意事项:

ExpandableListActivity的布局文件中必须包含一个ExpandableListView,并且id必须为="@id/android:list"。还可以包含一个id为empty的TextView,在ExpandableListActivity中没有数据的时候显示该控件的text值。

 1  <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
 2     android:orientation="vertical"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent">
 5     <ExpandableListView android:id="@id/android:list"
 6         android:layout_width="fill_parent"
 7         android:layout_height="fill_parent"
 8         android:drawSelectorOnTop="false">
 9         </ExpandableListView>
10     <TextView
11         android:id="@id/android:empty"
12         android:layout_width="fill_parent"
13         android:layout_height="fill_parent"
14         android:text="没有数据">
15         </TextView>
16 </LinearLayout>

group.xml(一级条目布局文件,样式外观可根据需要自由发挥)

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout
 3   xmlns:android="http://schemas.android.com/apk/res/android"
 4   android:orientation="vertical"
 5   android:layout_width="match_parent"
 6   android:layout_height="match_parent">
 7   <TextView android:id="@+id/groupTo"
 8      android:layout_width="fill_parent"
 9      android:layout_height="fill_parent"
10      android:paddingLeft="60dip"
11      android:paddingTop="10dip"
12      android:paddingBottom="10dip"
13      android:text="No Date"
14      android:textSize="20sp">
15   </TextView>
16 </LinearLayout>

child.xml(二级条目布局文件,样式外观可根据需要自由发挥)

 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout
 3   xmlns:android="http://schemas.android.com/apk/res/android"
 4   android:orientation="vertical"
 5   android:layout_width="match_parent"
 6   android:layout_height="match_parent">
 7   <TextView android:id="@+id/childTo"
 8       android:layout_width="fill_parent"
 9       android:layout_height="fill_parent"
10       android:paddingLeft="50dip"
11       android:paddingTop="5dip"
12       android:paddingBottom="5dip"
13       android:textSize="20sp"
14       android:text="No Date">
15   </TextView>
16 </LinearLayout>

2.JAVA代码

 

 1 package com.test;
 2 
 3 import java.util.ArrayList;
 4 import java.util.HashMap;
 5 import java.util.List;
 6 import java.util.Map;
 7 import android.app.ExpandableListActivity;
 8 import android.os.Bundle;
 9 import android.widget.SimpleExpandableListAdapter;
10 public class Sample_ExpandableListActivityActivity extends ExpandableListActivity {
11     @Override
12     public void onCreate(Bundle savedInstanceState) {
13         super.onCreate(savedInstanceState);
14         setContentView(R.layout.main);
15         //一级条目
16         List<Map<String, String>>groups=new ArrayList<Map<String,String>>();
17         Map<String, String> group1=new HashMap<String, String>();
18         group1.put("group","第一组");
19         Map<String, String> group2=new HashMap<String, String>();
20         group2.put("group", "第二组");
21         groups.add(group1);
22         groups.add(group2);
23         //二组条目
24         List<List<Map<String, String>>> childs=new ArrayList<List<Map<String,String>>>();
25         //第一组二级科目数据
26         List<Map<String, String>> child1=new ArrayList<Map<String,String>>();
27         Map<String, String> item1=new HashMap<String, String>();
28         item1.put("child","科目-1");
29         Map<String, String> item2=new HashMap<String, String>();
30         item2.put("child","科目-2");
31         child1.add(item1);
32         child1.add(item2);
33         //第二组二级科目数据
34         List<Map<String, String>> child2=new ArrayList<Map<String,String>>();
35         Map<String, String> item3=new HashMap<String, String>();
36         item3.put("child","科目-3");
37         Map<String, String> item4=new HashMap<String, String>();
38         item4.put("child","科目-4");
39         Map<String, String> item5=new HashMap<String, String>();
40         item5.put("child","科目-5");
41         child2.add(item3);
42         child2.add(item4);
43         child2.add(item5);
44         childs.add(child1);
45         childs.add(child2);
46         //SimpleExpandableListAdapter构造函数参数
47         //1.content
48         //2.一级条目数据
49         //3.一级条目布局文件
50         //4.一级条目Key
51         //5.一级条目显示信息控件id
52         //6.二级条目数据
53         //7.二级条目布局文件
54         //8.二级条目Key
55         //9.二级条目显示信息控件id
56         SimpleExpandableListAdapter adapter=new SimpleExpandableListAdapter(this,groups,R.layout.group, 
57         new String[]{"group"},new int[]{R.id.groupTo}, childs, R.layout.child, new String[]{"child"}, 
58         new int[]{R.id.childTo});
59         setListAdapter(adapter);
60 
61     }
62 }

 

仿qq联系人 学习笔记---ExpandableListActivity的使用

标签:

原文地址:http://www.cnblogs.com/hongyedeboke/p/5431784.html

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