Android::ExpandableListView二级列表控件的使用

1.在图形界面创建二级列表控件,此控件名为ExpandableList,在Comosite子选单下。

    <ExpandableListView
        android:id="@+id/expandableListView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" >
    </ExpandableListView></span>
2.在布局文件中声明group样式。

   <TextView
        android:id="@+id/tv_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="35dp"
        android:textSize="20sp"
        android:text="TextView" /></span>
3.在布局文件中声明child样式。

   <TextView
        android:id="@+id/tv_child"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="35dp"
        android:textSize="20sp"
        android:text="TextView" /></span>
4.创建一个链表groups,以维护map类型的数据。然后创建map类型的数据作为组名,键值均为group,加入到map的链表中。

        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);</span>
5.创建一个二维链表,维护map类型的数据,二维链表的第一维对应着第几组,第二维对应着相应组的数据集。然后将数据传进去。

        List<List<Map<String,String>>> childs = new ArrayList<List<Map<String,String>>>();
        List<Map<String,String>> child1 = new ArrayList<Map<String,String>>();
        Map<String,String> child1data1 = new HashMap<String, String>();
        child1data1.put("child", "张翼德");
        Map<String,String> child1data2 = new HashMap<String,String>();
        child1data2.put("child", "关云长");
        child1.add(child1data1);
        child1.add(child1data2);
        childs.add(child1);
        List<Map<String,String>> child2 = new ArrayList<Map<String,String>>();
        Map<String,String> child2data1 =new HashMap<String, String>();
        Map<String,String> child2data2 =new HashMap<String,String>();
        child2data1.put("child", "司马懿");
        child2data2.put("child","夏侯渊");
        child2.add(child2data1);
        child2.add(child2data2);
        childs.add(child2);</span>
6.建立一个SimpleExpandableListAdapter适配器,并对数据进行处理。
  适配器参数
  arg1:当前对象
  arg2:组数据
  arg3:组布局文件
  arg4:组中map的key数组
  arg5:组布局文件中textview的id数组
  arg6:子项数据
  arg7:子项布局文件
  arg8:子项布局文件中map的key数组
  arg9:子项布局文件中textview的id数组

        SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
        		this, 
        		groups, 
        		R.layout.group, 
        		new String[]{"group"}, 
        		new int[]{R.id.tv_group}, 
        		childs, 
        		R.layout.child, 
        		new String[]{"child"}, 
        		new int[]{R.id.tv_child});</span>
7.建立ExpandableListView对象,并将适配器的值传进去。

        ExpandableListView elv = (ExpandableListView) findViewByID(R.id.expandableListView1);
        elv.setAdapter(adapter);</span>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值