仿qq联系人 学习笔记---ExpandableListActivity的使用
2016-04-25 17:37
260 查看
【转】原地址 http://blog.163.com/xygzx@126/blog/static/237809502011102010100331/
效果显示图:
1.布局文件
main.xml(ExpandableListActivity布局文件)
注意事项:
ExpandableListActivity的布局文件中必须包含一个ExpandableListView,并且id必须为="@id/android:list"。还可以包含一个id为empty的TextView,在ExpandableListActivity中没有数据的时候显示该控件的text值。
group.xml(一级条目布局文件,样式外观可根据需要自由发挥)
child.xml(二级条目布局文件,样式外观可根据需要自由发挥)
2.JAVA代码
效果显示图:
1.布局文件
main.xml(ExpandableListActivity布局文件)
注意事项:
ExpandableListActivity的布局文件中必须包含一个ExpandableListView,并且id必须为="@id/android:list"。还可以包含一个id为empty的TextView,在ExpandableListActivity中没有数据的时候显示该控件的text值。
<LinearLayoutxmlns: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" android:layout_width="fill_parent" android:layout_height="fill_parent" android:drawSelectorOnTop="false"> </ExpandableListView> <TextView android:id="@id/android:empty" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="没有数据"> </TextView> </LinearLayout>
group.xml(一级条目布局文件,样式外观可根据需要自由发挥)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/groupTo" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="60dip" android:paddingTop="10dip" android:paddingBottom="10dip" android:text="No Date" android:textSize="20sp"> </TextView> </LinearLayout>
child.xml(二级条目布局文件,样式外观可根据需要自由发挥)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/childTo" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="50dip" android:paddingTop="5dip" android:paddingBottom="5dip" android:textSize="20sp" android:text="No Date"> </TextView> </LinearLayout>
2.JAVA代码
package com.test; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.ExpandableListActivity; import android.os.Bundle; import android.widget.SimpleExpandableListAdapter; public class Sample_ExpandableListActivityActivity extends ExpandableListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //一级条目 List<Map<String, String>>groups=new ArrayList<Map<String,String>>(); Map<String, String> group1=new HashMap<String, String>(); group1.put("group","第一组"); Map<String, String> group2=new HashMap<String, String>(); group2.put("group", "第二组"); groups.add(group1); groups.add(group2); //二组条目 List<List<Map<String, String>>> childs=new ArrayList<List<Map<String,String>>>(); //第一组二级科目数据 List<Map<String, String>> child1=new ArrayList<Map<String,String>>(); Map<String, String> item1=new HashMap<String, String>(); item1.put("child","科目-1"); Map<String, String> item2=new HashMap<String, String>(); item2.put("child","科目-2"); child1.add(item1); child1.add(item2); //第二组二级科目数据 List<Map<String, String>> child2=new ArrayList<Map<String,String>>(); Map<String, String> item3=new HashMap<String, String>(); item3.put("child","科目-3"); Map<String, String> item4=new HashMap<String, String>(); item4.put("child","科目-4"); Map<String, String> item5=new HashMap<String, String>(); item5.put("child","科目-5"); child2.add(item3); child2.add(item4); child2.add(item5); childs.add(child1); childs.add(child2); //SimpleExpandableListAdapter构造函数参数 //1.content //2.一级条目数据 //3.一级条目布局文件 //4.一级条目Key //5.一级条目显示信息控件id //6.二级条目数据 //7.二级条目布局文件 //8.二级条目Key //9.二级条目显示信息控件id SimpleExpandableListAdapter adapter=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}); setListAdapter(adapter); } }
相关文章推荐
- The Doors--poj1556(最短路+判断点与线段的关系)
- 无废话MVC入门教程二
- 汇编语言学习第八章-数据处理的两个基本问题
- fragment中的头像更改后在另一个fragment中同时更新头像
- 中国剩余定理(互质+非互质)
- quick动态控制动画的播放速度
- Android-Notification推送栏通知
- HDOJ 2063-过山车【匈牙利算法】
- 邻接表递归深搜(无向图)
- Android开源项目分类汇总
- 使用r.js压缩整个项目的JavaScript文件
- USACO 2.1-Healthy Holsteins
- 冒泡型事件、捕获型事件
- Android之Glide使用详解--集成、配置、使用、特性
- Note: there were 157 duplicate class definitions
- 将json字符串转化为java对象
- C语言结构体复习
- 一款陌生人状态社交软件--same
- useradd 命令
- 用VS2005(其他版本也可以)调试dump文件