ExpandableListActivity使用
使用方法(可以和ListActivity进行对比)
创建一个Activity类继承ExpandableListActvity
三个layout
主布局 (注意这里2个id都是android内置的 不是+id)
包含<ExpandableListView> 注意其中android:id="@id/android:list" 还有一个android:drawSelectorOnTop="false"(选中时是否遮盖文字)
<TextView android:id="@id/android:empty"> 当无数据时
一级目录布局
二级目录布局(条目item样式)
创建Adapter
将simpleExpandableListAdapter对象设置给当前ExpandableListActivity
setListAdapter(adapter);
SimpleExpandableListAdapter使用
为ExpandableListActivity提供数据
//定义List 为一级条目提供数据
List<Map<String, String>> groups=new ArrayList<Map<String, String>>();
需要几个条目 生成几个Map对象
Map<String, String> m1=new HashMap<String, String>();
m1.put(group,Group1);
m2.put(group,Group2);
groups.add(m1);
groups.add(m2);
//定义List 设置二级子条目 一个子条目一个List 一个项 一个Map
方法同上 建List(child1 child2)和Map(child:child1Data1;child:child1Data2)
//定义一个List 存储所有二级条目数据
*List<List<Map<String, String>>> childs = new ArrayListM<List<Map<String, String>>>();
childs.add(child1);
childs.add(child2);
//生成一个SimpleExpandableListAdapter对象
new SimpleExpandableListAdapter(...);
参数包括
(context上下文对象,一级条目List对象,一级条目布局, 
new String[]{"groups"})指定一级条目数据的key,new int[]{R.id.groupTo}指定一级条目数据显示的控件id,
二级条目的数据childs,二级条目布局,二级条目数据key(child),二级条目控件id}
下图是实现的截图:
这个实例用到三个布局文件:
第一个布局文件---主框架布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent" android:layout_height="match_parent"><ExpandableListView android:id="@id/android:list"android:layout_width="match_parent" android:layout_height="match_parent"android:drawSelectorOnTop="false"/><TextView android:id="@id/android:empty"android:layout_width="match_parent" android:layout_height="match_parent"android:text="No data"/></LinearLayout>

第二个布局文件---组布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/group"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingLeft="60px"android:paddingTop="10px"android:paddingBottom="10px"android:textSize="26sp"android:text="No data"/></LinearLayout>

第三个布局文件---子视图布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/child"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingLeft="50px"android:paddingTop="5px"android:paddingBottom="5px"android:textSize="20sp"android:text="No data"/></LinearLayout>

具体的实现代码如下:

public class ExpandableListActivity_Activity extends ExpandableListActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_expandable_list_activity_);//定义一个List,这个List为一级条目提供数据List<Map<String, String>> group=new ArrayList<Map<String,String>>();Map<String, String> group1=new HashMap<String, String>();group1.put("group", "group1");Map<String, String> group2=new HashMap<String, String>();group2.put("group", "group2");group.add(group1);group.add(group2);//定义一个List,这个List为第一个一级条目提供二级条目数据List<Map<String, String>> child1=new ArrayList<Map<String,String>>();Map<String, String> child1Data1=new HashMap<String, String>();child1Data1.put("child", "child1Data1");Map<String, String> child1Data2=new HashMap<String, String>();child1Data2.put("child", "child1Data2");child1.add(child1Data1);child1.add(child1Data2);//定义一个List,这个List为第二个一级条目提供二级条目数据List<Map<String, String>> child2=new ArrayList<Map<String,String>>();Map<String, String> child2Data1=new HashMap<String, String>();child2Data1.put("child", "child2Data1");Map<String, String> child2Data2=new HashMap<String, String>();child2Data2.put("child", "child2Data2");child2.add(child2Data1);child2.add(child2Data2);//定义一个List,这个List存储所有的二级条目数据List<List<Map<String, String>>> childs=new ArrayList<List<Map<String,String>>>();childs.add(child1);childs.add(child2);//生成一个SimpleExpandableListAdapter对象/** 1.context* 2.一级条目的数据* 3.用来设置一级条目样式的布局文件* 4.指定一级条目数据的key* 5.指定一级条目数据显示控件的id* 6.指定二级条目的数据* 7.设置二级条目样式的布局文件* 8.指定二级条目数据的key* 9.指定二级条目数据显示控件的id*/SimpleExpandableListAdapter selaListActivity=new SimpleExpandableListAdapter(this, group, R.layout.group, new String[]{"group"}, new int[]{R.id.group}, childs,R.layout.child,new String[] {"child"}, new int[]{R.id.child});setListAdapter(selaListActivity);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_expandable_list_activity_,menu);return true;}}

Android开发--浅谈ExpandableListActivity相关推荐

  1. 转载: Android开发浅谈:关于如何把手机壳颜色设置为我们App的主题颜色

    昨天一则新闻刷爆了微博,并迅速的攻占了我们的朋友圈. 中国平安财险科技中心,一个程序员把产品经理给打了,原因是产品经理提出一个需求:根据用户手机壳的颜色可以改变App的主题颜色-然后他就被程序员给打了 ...

  2. Android开发浅谈:关于如何把手机壳颜色设置为我们App的主题颜色

    昨天一则新闻刷爆了微博,并迅速的攻占了我们的朋友圈. 中国平安财险科技中心,一个程序员把产品经理给打了,原因是产品经理提出一个需求:根据用户手机壳的颜色可以改变App的主题颜色.....然后他就被程序 ...

  3. android开发浅谈之KeyEvent事件处理

    一个key事件的调用过程 一个偶然的机会,有一个报错的问题,日志如下: AndroidRuntime: at android.view.View.performClick(View.java:7259 ...

  4. 校园兼职网站php设计,基于PHP+MySql的校园兼职信息平台的开发浅谈

    Data Base Technique 0数据库技术基于PHP+MySqI的校园兼职信息平台的开发浅谈文刘晓智1杨雨锋2李万星2 表1:数据库一蹬表 摘要 首光简要介绍了编程语孬和MySql数据库的主 ...

  5. [转]Android蓝牙开发浅谈

    转自:http://www.eoeandroid.com/thread-18993-7-1.html 对于一般的软件开发人员来说,蓝牙是很少用到的,尤其是Android的蓝牙开发,国内的例子很少    ...

  6. 嵌入式开发-浅谈嵌入式MCU开发中的三个常见误区

    浅谈嵌入式MCU开发中的三个常见误区 原创 2017-09-30 胡恩伟 汽车电子expert成长之路 目录 (1)嵌入式MCU与MPU的区分 (2)误区一:MCU的程序都是存储在片上Flash上,然 ...

  7. Android智能电视应用程序开发浅谈(二)

    Android应用程序的布局有两种,一种是在res/layout下面的xml文件里布局,一种是直接在java代码里布局,而Android智能电视有多种分辨率,我们可以建立多个适应不同分辨率的资源文件, ...

  8. Android智能电视应用程序开发浅谈(一)

    最近新出的一些智能电视,都是基于Android系统,而本人最近也一直在从事Android智能电视的应用开发,想和大家分享下经验. Android智能电视是什么呢?它与我们平时用的移动设备有什么区别呢? ...

  9. Android智能电视应用程序开发浅谈(三)

    虽然我现在开发智能电视应用程序都是在java代码里面布局,但我倾向于在xml文件里布局,因为这样更容易控制整个布局,特别是在有ListView参与的程序,你会感觉到比较麻烦. Android智能电视上 ...

最新文章

  1. LocalResizeIMG前端HTML5本地压缩图片上传,兼容移动设备IOS,android
  2. Servlet_生命周期详解
  3. html显示hdf5文件,python读取hdf5文件
  4. linux make编译报错 mv,Linux下安装redis
  5. 编程之美2——N!的二进制表示中最低位1的位置
  6. 域用用户怎么允许共享_w7如何共享打印机 w7共享打印机步骤【详细介绍】
  7. c语言广播程序,C语言socket编程---udp通信及广播
  8. db2数据库错误代码集合
  9. 针对关键字是字符串的一个比较好的散列函数
  10. linux下配置防火墙
  11. AB Micro800编程环境CCW安装
  12. mouseenter和mouseleave跟mouseover和mouseout
  13. verilog语法检查
  14. promise .then和async await的使用
  15. 减少手机页面跳转的方法(转)
  16. 【LTE基础知识】GUTI(Globally Unique Temporary UE Identity)分配
  17. C++ 实验3-2本月有几天?
  18. 【Centos】EFAK(kafka-eagle)对ZK、Kafka可视化管理工具容器化安装与配置
  19. 用友php漏洞,用友GRP-u8 注入-RCE漏洞复现
  20. Greenplum安装手册

热门文章

  1. Spring Boot 中的容器配置
  2. 设计模式学习(十六) 模板方法模式
  3. 关于System Volume Information占用的大量磁盘空间
  4. 让自己的主机成为证书颁发机构
  5. IBM DB2 For Linux安装指南
  6. 掘金小册Jenkins大纲准备
  7. Elasticsearch: 权威指南 » 聚合 » Doc Values and Fielddata » 聚合与分析
  8. win8改win7笔记
  9. 中国象棋人机博弈程序(扁平化棋局) C语言实现
  10. android 4.0(ICS)源码下载方法