zoukankan      html  css  js  c++  java
  • android学习:ExpandableListView的运用

    android中有一种expandablelistview,可以扩展的listview,就是那种点击一下可以扩展出子项,再点一下收缩回去的显示list。

    因为需要查看一堆文件的目录结构,就使用了expandablelist以便于直观地看到结构形式。

    根据APIDemos中的实例,进行细微的改动就可以够自己使用了。

    自己建立的activity继承自ExpandableListActivity,设定好的是显示两级结构。至于知否能够扩展更多的级,还没有实验,准备实验一下。顶层是group,第二层是child。

    顶层的结构定义为:List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();是个Arraylist结构,其中是Map键值对。

    第二层的结构定义为: List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();这个可以看成是比顶层多出了个List,是在顶层list中的list,其中也是键值对,所以定义成list<list<map>>>。

    之后就是要向每个键值对中写入数据,在list中显示。对于顶层:

    Map<String, String> curGroupMap = new HashMap<String, String>();     //定义curGroupMap是Map结构,存储需要用到的键值对      

    groupData.add(curGroupMap);            //添加到List中,map结构作为参数,现在list中的单元是map结构存储的数据,表示顶层的数据列表

    curGroupMap.put(NAME, "Group " + i);//向map中写入键值对

    对于第二层:

    因为每个child也是list,所以要先为每个group下的child定义一个List,有多少个group项就应该有多少个如下的children项,这个在使用的时候要注意,否则会出现不同的child键值对覆盖等问题。

     List<Map<String, String>> children = new ArrayList<Map<String, String>>();//这个同顶层的类似,也可以看成,从这一步起又开始了刚才定义顶层是的步骤

    for (int j = 0; j < 15; j++) {  

     Map<String, String> curChildMap = new HashMap<String, String>();              //参见以上group中的相关方法

     children.add(curChildMap);                

    curChildMap.put(NAME, "Child " + j);              

    }          

     childData.add(children);//最后将children这个list添加成为childData中的一个元素。一个child中可能会有好几项,children表示第二层的元素列表

    再理清两层数据的具体层次关系后,通过SimpleExpandableListAdapter准备数据匹配器,

    mAdapter = new SimpleExpandableListAdapter(              

    this,//1.上下文              

    groupData,//2.顶层数据列表              

    android.R.layout.simple_expandable_list_item_1,//3.1层显示样式 ,系统自定义        

    new String[] {ENTRY}, //4.顶层map的键,还可以有一个键        

    new int[] { android.R.id.text1},// 5.顶层数据显示的View ID        ,系统自定义       

    childData,               //原理同上

    android.R.layout.simple_expandable_list_item_2,            

     new String[] {ENTRY},              

    new int[] { android.R.id.text1}              

    );

    然后设定匹配器 setListAdapter(mAdapter)

    最后程序的运行结果如下:

    

  • 相关阅读:
    MySQL存储过程
    [转载]JDBC应该始终以PreparedStatement代替Statement
    Restlet入门例子 RESTful web framwork for java
    Cglib的使用方法(3)Mixin
    HDOJ_1220
    精华:OracleHelper类
    web.config详解
    ADO.NET结构
    字典树
    WCF、Net remoting、Web service概念及区别
  • 原文地址:https://www.cnblogs.com/pengwang/p/2097677.html
Copyright © 2011-2022 走看看