android中ExpandableListView的使用

        看google官方BluetoothLeGatt代码时,碰到ExpandableListView,给它设置Adapter时使用的是SimpleExpandableListAdapter,设置的数据看的我是一脸懵逼,所以决定找个Demo理理思路。

       ExpandableListView是双层的ListView,所以设置数据的时候需要循环嵌套,外层循环设置一级数据,内层循环设置二级数据,一级数据就和普通的ListView的数据一样,需要一个ArrayList集合A去保存,二级数据本身就是一个ArrayList集合,它也需要一个ArrayList集合B去保存,且集合A的大小与集合B的大小相等。

     主要代码如下:

String[] names = {"腾讯","阿里","百度"};
String[][] childNames = {{"QQ","微信","管家"},{"支付宝","淘宝","优酷"},{"百度翻译","百度地图","百度一下"}};
List<HashMap<String,String>> companys = new ArrayList<> ();
ArrayList<ArrayList<HashMap<String,String>>> products = new ArrayList<> ();

for (int i=0;i<names.length;i++){
HashMap<String,String> keyValue = new HashMap<> ();
keyValue.put ("name",names[i]);
keyValue.put ("id1",""+i);
companys.add (keyValue);
ArrayList<HashMap<String,String>> child = new ArrayList<> ();
for (int j=0;j<childNames[i].length;j++){
HashMap<String,String> producKeyValue = new HashMap<> ();
producKeyValue.put ("childName",childNames[i][j]);
producKeyValue.put ("id2",i+""+j);
child.add (producKeyValue);
}
products.add (child);
}
SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter (this,companys,
android.R.layout.simple_expandable_list_item_2,
new String[]{"name","id1"},
new int[]{android.R.id.text1,android.R.id.text2},
products,
android.R.layout.simple_expandable_list_item_2,
new String[]{"childName","id2"},
new int[]{android.R.id.text1,android.R.id.text2});

expandableListView.setAdapter (adapter);
 

 

     运行效果如下:

 

也可以通过BaseExpandableListAdapter实现该效果,主要代码如下:

 

代码块四

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值