Android学习笔记之ExpandableListView

<1>简介

android中有一种expandablelistview,可以扩展的listview,就是那种点击一下可以扩展出子项,再点一下收缩回去的显示list。

一个视图显示项目的垂直滚动的两级列表。这不同于ListView,允许有两级列表。

分组能单独地被扩展出到显出它的子项目。各子项目来自ExpandableListAdapter相关的View。

<2>重要方法

   expandGroup(int groupPos) :在分组列表视图中展开一组,

setSelectedGroup(int groupPosition) :设置选择指定的组。

  setSelectedChild(int groupPosition, int childPosition, boolean shouldExpandGroup) :设置选择指定的子项。

  getPackedPositionGroup(long packedPosition) :返回所选择的组

  getPackedPositionForChild(int groupPosition, int childPosition) :返回所选择的子项

  getPackedPositionType(long packedPosition) :返回所选择项的类型(Child,Group)

  isGroupExpanded(int groupPosition) :判断此组是否展开

<3>范例

package xiaosi.ExpandableList; 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.widget.SimpleExpandableListAdapter; public class ExpandableLists extends ExpandableListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //为一级条目提供数据 //Groups 分组 List<Map<String,Object>> Groups = new ArrayList<Map<String,Object>>(); Map<String,Object> groupone = new HashMap<String,Object>(); groupone.put("groupname", "流行歌曲"); Groups.add(groupone); Map<String,Object> grouptwo = new HashMap<String,Object>(); grouptwo.put("groupname", "校园歌曲"); Groups.add(grouptwo); //定义两个List<Map<String,Object>> childone和childtwo //为二级条目提供数据 //childone List<Map<String,Object>> childone = new ArrayList<Map<String,Object>>(); Map<String,Object> childonedateone = new HashMap<String,Object>(); //childonedateone.put("mp3logo", R.drawable.logo0); childonedateone.put("mp3ID", "1"); childonedateone.put("mp3name", "流星雨"); childone.add(childonedateone); Map<String,Object> childonedatetwo = new HashMap<String,Object>(); //childonedatetwo.put("mp3logo", R.drawable.logo1); childonedatetwo.put("mp3ID", "2"); childonedatetwo.put("mp3name", "千里之外"); childone.add(childonedatetwo); Map<String,Object> childonedatethree = new HashMap<String,Object>(); //childonedatethree.put("mp3logo", R.drawable.logo2); childonedatethree.put("mp3ID", "3"); childonedatethree.put("mp3name", "菊花台"); childone.add(childonedatethree); //childtwo List<Map<String,Object>> childtwo = new ArrayList<Map<String,Object>>(); Map<String,Object> childtwodateone = new HashMap<String,Object>(); //childtwodateone.put("mp3logo", R.drawable.logo0); childtwodateone.put("mp3ID", "1"); childtwodateone.put("mp3name", "同桌的你"); childtwo.add(childtwodateone); //Childs List<List<Map<String,Object>>> Childs = new ArrayList<List<Map<String,Object>>>(); Childs.add(childone); Childs.add(childtwo); /** * 使用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 simpleExpandListAdapter = new SimpleExpandableListAdapter(ExpandableLists.this, Groups, R.layout.group, new String[]{"groupname"},new int[]{R.id.group}, Childs, R.layout.child, new String[]{"mp3ID","mp3name"},new int[]{R.id.id,R.id.name}); setListAdapter(simpleExpandListAdapter); } }


main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ExpandableListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:drawSelectorOnTop="false"/> <TextView android:id="@id/android:empty" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="No Data"/> </LinearLayout>


group.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:id="@+id/group" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="60px" android:textSize="20sp" android:text="No data" /> </LinearLayout>


child.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <TextView android:id="@+id/id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="50px"/> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="50px" android:text="No data"/> </LinearLayout>


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值