android之ExpandableListActivity

本文详细介绍如何使用Android中的ExpandableListView组件。包括设置三种布局样式、创建数据源及加载适配器的过程。通过实例演示如何实现点击父项展开子项列表的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

LixtView的升级版,点击item选项有下拉列表的功能,展示出Listitem的List数据

1.先在xml设置三个布局。

ExpandableListView样式Layout,这个布局是总体布局,相当于activity布局,这里最好引用android自带id,因为java中最好是用ExpandableListActivity来实现这个功能。

     <ExpandableListView android:id="@id/android:list"
               android:layout_width="match_parent" 
               android:layout_height="match_parent"
               android:background="#00FF00"
               android:layout_weight="1"
               android:drawSelectorOnTop="false"/>

一级条目样式groupLayout,这个布局是ListItem的布局,自己定义。

二级条目样式childLayout,这个布局是ListItem点击之后下拉别表里的item的布局。

2.创建两个数据源ArrayList<Hash<String,自己定义>>。

一个是给ExpandableListView提供数据,ExpandableListView有几个item就add几个Hash<String,自己定义>。

一个是给ExpandableListView里的item提供数据,点击item后的下拉列表有几个ChildItem就add几个HashMap<String,自己定义>。

至于HashMap<String,自己定义>里put几个数据,就要看,自己定义的布局样式了。

3.加载适配器。

我们选用的是继承ExpandableListActivity,这个类里有一些特殊的方法。我把Adapter里面的参数写得直白点了,应该能看懂吧。。。。。

        SimpleExpandableListAdapter simpleExpandableListAdapter = new SimpleExpandableListAdapter(<span style="font-family: Arial, Helvetica, sans-serif;">context,</span>
<span style="font-family: Arial, Helvetica, sans-serif;">groupList,groupLayout,new String[]{"groupMapKey"},new int[]{groupWidgetId},</span>
<span style="font-family: Arial, Helvetica, sans-serif;">childList,childLayout,new String[]{"childMapKey"},new int[]{childWidgetId});</span>
这个完了之后,采用和ListAdapter相同的方法setListAdapter来加载适配器。就Ok了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值