首先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:andro/>
    </LinearLayout>

  parent.xml文件的内容如下:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:andro/>
    </LinearLayout>

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

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:andro />
    </LinearLayout>

相关文章:

  • 2021-09-25
  • 2022-02-23
  • 2022-01-10
  • 2021-10-02
  • 2022-12-23
  • 2021-06-29
  • 2022-12-23
  • 2021-09-22
猜你喜欢
  • 2021-09-24
  • 2022-02-18
  • 2021-10-18
  • 2021-12-19
  • 2021-08-09
  • 2021-10-19
  • 2022-01-07
相关资源
相似解决方案