首先MainActivity继承自ExpandableListActivity,其中的声明如下:

    setContentView(R.layout.expandmain);
    //定义一个:List,该List对象为一级条目提供数据
    List<Map<String,String>> parents = new ArrayList<Map<String,String>>();
    Map<String,String> parent1 = new HashMap<String,String>();
    parent1.put("group", "parent1");
    Map<String,String> parent2 = new HashMap<String,String>();
    parent2.put("group", "parent2");
    parents.add(parent1);
    parents.add(parent2);
    //定义一个List,该List对象为第一个一级条目提供数据
    List<Map<String,String>> child1 = new ArrayList<Map<String,String>>();
    Map<String,String> childData1 = new HashMap<String,String>();
    childData1.put("child", "child1Data1");
    Map<String,String> childData2 = new HashMap<String,String>();
    childData2.put("child", "child1Data2");
    child1.add(childData1);
    child1.add(childData2);
    //定义一个List,该List对象为第二个一级条目提供数据
    List<Map<String,String>> child2 = new ArrayList<Map<String,String>>();
    Map<String,String> childData3 = new HashMap<String,String>();
    childData3.put("child", "child1Data3");
    Map<String,String> childData4 = new HashMap<String,String>();
    childData4.put("child", "child1Data4");
    child2.add(childData3);
    child2.add(childData4);
    //生成一个List,该List对象用来存储所有的二级条目的数据
    List<List<Map<String,String>>> childs = new ArrayList<List<Map<String,String>>>();
    childs.add(child1);
    childs.add(child2);
    //context
    //一级条目的数据
    //用来设置一级条目样式的布局文件
    //指定一级条目数据的key
    //指定一级条目显示控件的ID
    //指定二级条目的数据
    //用来设置二级条目的布局文件
    //指定二级条目数据的key
    //指定二级条目数据显示控件的ID
    SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(this, parents, R.layout.parent, new String[]{"group"}, new int[]                           {R.id.parentTo}, childs, R.layout.child, new String[]{"child"},new int[]{R.id.childTo});
    //将SimpleExpandableListAdapter设置给当前的ExpandableListActivity
    setListAdapter(sela);

  其中expandmain.xml文件中的内容

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" >
      <ExpandableListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="false"/>
    </LinearLayout>

  parent.xml文件的内容如下:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" >
      <TextView android:id="@+id/parentTo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="20px"
        android:textSize="26sp"
        android:text="No data"/>
    </LinearLayout>

  child.xml文件中的内容如下:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" >
      <TextView
        android:id="@+id/childTo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="20px"
        android:textSize="20sp"
        android:text="No data" />
    </LinearLayout>

最新文章

  1. Fresnel Reflection - 菲涅尔反射
  2. C++中string,wstring,CString的基本概念和用法
  3. IP地址的定义和含义
  4. HDU 1599 find the mincost route (无向图的最小环)
  5. ASP.NET遍历textbox文本框
  6. java Spring使用配置文件读取jdbc.properties
  7. MVC控制器方法返回类型
  8. java8接口定义增强
  9. JavaScript函数继承
  10. Spring boot jackson
  11. iPhone上将短信内容发送到指定邮箱的方法
  12. JS: 数组扁平化
  13. vue权限路由实现方式总结
  14. [翻译] CSStickyHeaderFlowLayout
  15. ORACLE检查死锁
  16. CH3401 石头游戏
  17. Java 中的类
  18. 在Team Foundation Server (TFS)的代码库或配置库中查找文件或代码
  19. Eclipse ftp插件
  20. postgresql centos6.5安装以及常用命令

热门文章

  1. 跨域无法获取自定义header的问题
  2. VS2010 中,windows服务不能添加 System.Web 引用
  3. C#中分割字符串输出字符数组
  4. 【转】最大流EK算法
  5. (转载)开始iOS 7中自动布局教程(一)
  6. Cocos2d-x游戏引擎实战开发炸弹超人项目教程 全套下载 1至6课
  7. 跟vczh看实例学编译原理——三:Tinymoe与无歧义语法分析
  8. 七天学会ASP.NET MVC(七)——创建单页应用
  9. Java,extends,继承
  10. Android开发学习之路-LruCache使用和源码分析