ExpandableListView实现方式之SimpleExpandableListView
来源:互联网 发布:河北软件学院 编辑:程序博客网 时间:2024/05/16 06:53
ExpandableListView是可以展开的列表项,分为组列表项和子列表项,可以使用SimpleExpandableListView,将两个List组合起来形成ExpandableListView。
其实现方法为:
1.建立两个list,其中一个作为组列表项,一个作为子列表项
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/group" //组列表项 android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="25sp" android:paddingLeft="35dp" android:paddingTop="10dp" android:paddingRight="5dp" android:paddingBottom="10dp" /></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/child" //子列表项 android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="25sp" android:paddingLeft="25dp" android:paddingTop="10dp" android:paddingRight="5dp" android:paddingBottom="10dp" /></LinearLayout>
而扩展列表界面的布局文件可按照下面来写:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ExpandableListView android:id ="@+id/expandableListView" android:layout_width ="fill_parent" android:layout_height ="wrap_content" /></LinearLayout>
上面三个就是ExpandableListView所用到的布局文件,下面具体说明在代码中如何实现该列表。
首先是要为组列表项和子列表项提供数据,简单的,可以按照下面来进行
<span style="white-space:pre">private String[] school={"一中","二中","三中"};<span style="white-space:pre"></span>private String[][] camera={{"摄像头1","摄像头2","摄像头3"},{"摄像头1","摄像头2"},{"摄像头1","摄像头2","摄像头3","摄像头4","摄像头5"}};</span>
<span style="white-space:pre"></span>List<HashMap<String,String>> groupData = new ArrayList<HashMap<String,String>>();List<List<HashMap<String,String>>> childData = new ArrayList<List<HashMap<String,String>>>();这是添加了两个List:
第一个List是以HashMap键值对(值为String)为内容的列表,是组列表项的数据;
第二个List是两个List的嵌套,是子列表项的数据。
然后需要向这两个List里面添加数据:
for(int i=0; i < school.length; i++){HashMap<String, String> maps = new HashMap<String, String>();maps.put("schoolname", school[i]);groupData.add(maps);List<HashMap<String,String>> child = new ArrayList<HashMap<String,String>>();System.out.println(camera[i].length);for(int j=0; j < camera[i].length; j++){HashMap<String, String> mapsj = new HashMap<String, String>();mapsj.put("camera", camera[i][j]);child.add(mapsj);}childData.add(child);}
最后是使用Adapter为列表加入数据
创建MySimpleExpandableListView类:
<span style="white-space:pre"></span>private class MySimpleExpandableListAdapter extends SimpleExpandableListAdapter{<span style="white-space:pre"></span>//构造函数@SuppressWarnings("unchecked")public MySimpleExpandableListAdapter(Context context,List<? extends Map<String, ?>> groupData,int expandedGroupLayout, String[] groupFrom, int[] groupTo,List<? extends List<? extends Map<String, ?>>> childData,int childLayout, String[] childFrom, int[] childTo) {super(context, groupData, expandedGroupLayout, groupFrom,groupTo, childData, childLayout, childFrom, childTo);// TODO Auto-generated constructor stub}@Overridepublic int getGroupCount() {// TODO Auto-generated method stubreturn super.getGroupCount();}@Overridepublic int getChildrenCount(int groupPosition) {// TODO Auto-generated method stubreturn super.getChildrenCount(groupPosition);}@Overridepublic Object getGroup(int groupPosition) {// TODO Auto-generated method stubreturn super.getGroup(groupPosition);}@Overridepublic Object getChild(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn super.getChild(groupPosition, childPosition);}@Overridepublic long getGroupId(int groupPosition) {// TODO Auto-generated method stubreturn super.getGroupId(groupPosition);}@Overridepublic long getChildId(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn super.getChildId(groupPosition, childPosition);}@Overridepublic boolean hasStableIds() {// TODO Auto-generated method stubreturn false;}<span style="white-space:pre"></span>//获得组列表项的View对象@Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {return super.getGroupView(groupPosition, isExpanded, convertView, parent);}<span style="white-space:pre"></span>//获得子列表项的View对象@Overridepublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {return super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn true;}}
在SimpleExpandableListAdapter的构造函数中,有9个参数,其含义分别为:
Context context:应用程序接口,this
List<? extends Map<String, ?>> groupData:组列表项的数据,在上述代码中应为groupData
int expandedGroupLayout:组列表项的布局文件,应为R.layout.**
String[] groupFrom:组列表项数据的键值对的键名,上述代码中为schoolname
int[] groupTo:组列表项的id值,应为R.id.group
List<? extends List<? extends Map<String, ?>>> childData:子列表项的数据,在上述代码中应为childData
int childLayout:子列表项的布局文件,应为R.layout.**
String[] childFrom:子列表项数据的键值对的键名,上述代码中为camera
int[] childTo:组列表项的id值,应为R.id.child
因此,最后创建MySimpleExpandableListAdapter的实例即可
<span style="white-space:pre"></span>MySimpleExpandableListAdapter adapter = new MySimpleExpandableListAdapter(this, groupData, R.layout.group_item, new String[]{"schoolname"}, new int[]{R.id.group},childData, R.layout.child_item, new String[]{"camera"}, new int[]{R.id.chile});elv.setAdapter(adapter);
其中,elv为扩展列表的实例
- ExpandableListView实现方式之SimpleExpandableListView
- 实现展开列ExpandableListView的三种方式之SimpleExpandableListAdapter实例
- ExpandableListView的三种方式之
- Android之实现QQ好友分组(ExpandableListView)
- Android之实现QQ好友分组(ExpandableListView)
- Android之实现QQ好友分组(ExpandableListView)
- Android之实现QQ好友分组(ExpandableListView) .
- Android之ExpandableListView下拉分组的实现
- Android之实现QQ好友分组(ExpandableListView)
- 他山之石之ExpandableListView的简单实现
- ExpandableListView 使用方式
- (VIEW控件)Android之实现QQ好友分组(ExpandableListView)
- ExpandableListView基本实现
- ExpandableListView 长按实现
- CheckBox的ExpandableListView实现
- ExpandableListView实现多级菜单
- ExpandableListView的简单实现
- ExpandableListView的实现
- Oracle SID在本机上已经存在,请指定一个不同的SID”的解决办法
- plot3+color
- 暑假第十天,7月15号
- ACM 暑期培训——最短路/最小生成树专题
- 编程小练习2:数组乘积
- ExpandableListView实现方式之SimpleExpandableListView
- UVA 12378 Ball Blasting Game
- 根据屏幕大小设置字体
- poj 3625 Building Roads
- no mapping found for http request with uri
- UVA 107 The Cat in the Hat
- 暑假第十一天,7月16号
- MFC编辑框自动换行,垂直滚动条自动下移
- 数据结构和算法设计专题之---二分查找(Java版)