sunplusedu的个人空间 https://blog.eetop.cn/sunplusedu [收藏] [复制] [分享] [RSS]

空间首页 动态 记录 日志 相册 主题 分享 留言板 个人资料

日志

【凌阳教育学堂】Android控件之ExpandableListView

已有 1082 次阅读| 2011-12-5 10:43

来源: 凌阳教育嵌入式培训
  凌阳教育是全国唯一“学员就业双100%品牌”,保证“学员100%就业”,保证“学员100%满意就业”。凌阳教育也是全国唯一“按班公布学员就业去向诚信机构”,每个班所有学员来自哪个学校就业后签约哪家企业都真实发布。截止2011年9月,凌阳教育学员已连续27期“双100%就业”。凌阳教育是全国唯一“原厂嵌入式培训机构”,以凌阳科技产业链企业的人才需求为契机,先后与2500多家企业建立学员就业合作关系。

  先把运行效果附在下面:

嵌入式培训

  首先看下布局文件:在定义布局时,这里要定义三个布局文件,全在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>> 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 是调用的,具体的用法就要根据需求来确定了。

  凌阳教育“学员就业双100%”的诚信获得了社会的广泛认可,2008年、2009年、2010年,凌阳教育连续三年荣膺新浪“中国十大品牌IT教育机构”、新浪“最具就业竞争力IT教育机构”、搜狐“中国十大品牌IT培训机构”、网易“中国十大IT教育品牌”荣誉称号。凌阳教育是嵌入式培训领域,唯一全部获得三大门户网站“十大IT品牌”表彰的机构。

嵌入式培训

凌阳教育嵌入式培训,值得信赖的嵌入式教育品牌!

点赞

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 注册

  • 关注TA
  • 加好友
  • 联系TA
  • 0

    周排名
  • 0

    月排名
  • 0

    总排名
  • 0

    关注
  • 1

    粉丝
  • 0

    好友
  • 0

    获赞
  • 2

    评论
  • 1927

    访问数
关闭

站长推荐 上一条 /1 下一条

小黑屋| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-5-10 06:51 , Processed in 0.017837 second(s), 6 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
返回顶部