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>();     //定义curGroupMapMap结构,存储需要用到的键值对      

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)

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

 



 

 首先看下布局文件:在定义布局时,这里要定义三个布局文件,全在res/layout目录下,我先把布局文件附在下面,具体有什么用,我在下面会详细说明

  main.xml:

  <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns: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"/>
    <TextView 
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:id="@id/android:empty"
       android:text="No Data"/>
   </LinearLayout> 

  groups.xml:

  <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       >
    <TextView 
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:id="@+id/group"
       android:textSize="25sp"
       android:paddingLeft="35px"
       android:paddingTop="10px"
       android:paddingRight="5px"
       android:paddingBottom="10px"
       android:text="No Data"/>
   </LinearLayout>

  childs.xml:

  <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       >
    <TextView 
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:id="@+id/child"
       android:textSize="15sp"
       android:paddingLeft="25px"
       android:paddingTop="10px"
       android:paddingRight="5px"
       android:paddingBottom="10px"
       android:text="No Data"/>
   </LinearLayout>

  首先,第一个布局文件main.xml是在主界面定义一个ExpandableListView ,在这里我们就要和在做ListView是的方法做下对比了,其实这个listView的显示类似。主要是用来显示ExpandableListView 下的数据。第二个布局文件是定义ExpandableListView 下的一级条目目录。在这里我只为一级条目目录添加一个textView控件。第三个布局文件是定义ExpandableListView 下的二级条目目录,和一级条目目录一样,布局文件里也只有一个TextView控件。

  Java代码:

  ExpandableActivity:

  package cn.yj3g.ExpandableListActivity;
  
   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.view.View;
   import android.widget.ExpandableListView;
   import android.widget.SimpleExpandableListAdapter;
   /**
    * 继承ExpandableListActivity
    */
   public class ExpandableActivity 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<Map<String, String>> child1 = new ArrayList<Map<String, String>>();
           //同样是在一级条目目录下创建两个对应的二级条目目录
           Map<String, String> childdata1 = new HashMap<String, String>();
           childdata1.put("child", "青花瓷");
           Map<String, String> childdata2 = new HashMap<String, String>();
           childdata2.put("child", "东风破");
           child1.add(childdata1);
           child1.add(childdata2);
           //同上
           List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();
           Map<String, String> childdata3 = new HashMap<String, String>();
           childdata3.put("child", "青花瓷.lrc");
           Map<String, String> childdata4 = new HashMap<String, String>();
           childdata4.put("child", "东风破.lrc");
           child2.add(childdata3);
           child2.add(childdata4);
           // 将二级条目放在一个集合里,供显示时使用
           List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();
           childs.add(child1);
           childs.add(child2);
           /**
            * 使用SimpleExpandableListAdapter显示ExpandableListView
            * 参数1.上下文对象Context
            * 参数2.一级条目目录集合
            * 参数3.一级条目对应的布局文件
            * 参数4.fromto,就是map中的key,指定要显示的对象
            * 参数5.与参数4对应,指定要显示在groups中的id
            * 参数6.二级条目目录集合
            * 参数7.二级条目对应的布局文件
            * 参数8.fromto,就是map中的key,指定要显示的对象
            * 参数9.与参数8对应,指定要显示在childs中的id
            */
           SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
                   this, groups, R.layout.groups, new String[] { "group" },
                   new int[] { R.id.group }, childs, R.layout.child,
                   new String[] { "child" }, new int[] { R.id.child });
           setListAdapter(adapter);
   
       }
       /**
        * 设置哪个二级目录被默认选中
        */
       @Override
       public boolean setSelectedChild(int groupPosition, int childPosition,
               boolean shouldExpandGroup) {
               //do something
           return super.setSelectedChild(groupPosition, childPosition,
                   shouldExpandGroup);
       }
       /**
        * 设置哪个一级目录被默认选中
        */
       @Override
       public void setSelectedGroup(int groupPosition) {
           //do something
           super.setSelectedGroup(groupPosition);
       }
       /**
        * 当二级条目被点击时响应
        */
       @Override
       public boolean onChildClick(ExpandableListView parent, View v,
               int groupPosition, int childPosition, long id) {
               //do something
           return super.onChildClick(parent, v, groupPosition, childPosition, id);
       }
 
   }

  上面在显示ExpandableListView 是用的是SimpleExpandableListAdapter ,这里要传的参数比较多,大家在用时别一看到参数多就头疼,没事的,看注释你就知道各个参数的意思了。下面的三个重写方法是在显示ExpandableListView 是调用的,具体的用法就要根据需求来确定了。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值