Android的ExpandableListView的学习
2014-11-06 16:40
169 查看
最近在公司做项目,有一个小模块,需要用到ExpandableListView这个控件,所以在工作中边学习边工作。根据需求,在进入ExpandableLisView的时候默认展开第一组,需要用到这个属性,expandableListView.expandGroup(0);
在ExpandableListView中,当点击一个组展开时,其他展开的组是不自动关上的,这需要在监听事件中处理一下:
@Override
public void onGroupExpand(int groupPosition) {
// TODO Auto-generated method stub
for (int i = 0, count = expandableListView.getExpandableListAdapter()
.getGroupCount(); i < count; i++) {
if (groupPosition != i) {// 关闭其他分组
expandableListView.collapseGroup(i);
}
}
}
还有一个地方需要注意的就是在Adapter的getChildrenCount(int groupPosition)方法,要return child.get(groupPosition).size(),否则会在点击组得时候,报数组越界的问题。
下边是部分代码:
MainActivity.java
View Code
![](http://images.cnitblog.com/blog/305269/201411/061638362365614.jpg)
![](http://images.cnitblog.com/blog/305269/201411/061639094247876.jpg)
在ExpandableListView中,当点击一个组展开时,其他展开的组是不自动关上的,这需要在监听事件中处理一下:
@Override
public void onGroupExpand(int groupPosition) {
// TODO Auto-generated method stub
for (int i = 0, count = expandableListView.getExpandableListAdapter()
.getGroupCount(); i < count; i++) {
if (groupPosition != i) {// 关闭其他分组
expandableListView.collapseGroup(i);
}
}
}
还有一个地方需要注意的就是在Adapter的getChildrenCount(int groupPosition)方法,要return child.get(groupPosition).size(),否则会在点击组得时候,报数组越界的问题。
下边是部分代码:
MainActivity.java
<?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:background="#6f737d" android:orientation="vertical" > <TextView android:id="@+id/tv_address" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginLeft="10dp" android:gravity="center_vertical" android:textColor="@android:color/white" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal" > <LinearLayout android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center_vertical" android:orientation="horizontal" android:padding="10dp" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/fax_icon" /> <TextView android:id="@+id/tv_fax" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:textColor="@android:color/white" /> </LinearLayout> <LinearLayout android:id="@+id/tel_layout" android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/station_tel_selected" android:gravity="center_vertical" android:orientation="horizontal" android:padding="10dp" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src=<?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:background="#6f737d" android:orientation="vertical" > <TextView android:id="@+id/tv_address" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginLeft="10dp" android:gravity="center_vertical" android:textColor="@android:color/white" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal" > <LinearLayout android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center_vertical" android:orientation="horizontal" android:padding="10dp" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/fax_icon" /> <TextView android:id="@+id/tv_fax" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:textColor="@android:color/white" /> </LinearLayout> <LinearLayout android:id="@+id/tel_layout" android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/station_tel_selected" android:gravity="center_vertical" android:orientation="horizontal" android:padding="10dp" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/tel_icon" /> <TextView android:id="@+id/tv_tel" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:textColor="@android:color/white" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0.5dp" android:background="@android:color/black" > </LinearLayout> </LinearLayout>"@drawable/tel_icon" /> <TextView android:id="@+id/tv_tel" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:textColor="@android:color/white" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0.5dp" android:background="@android:color/black" > </LinearLayout> </LinearLayout>
View Code
![](http://images.cnitblog.com/blog/305269/201411/061638362365614.jpg)
![](http://images.cnitblog.com/blog/305269/201411/061639094247876.jpg)
![](http://images.cnitblog.com/blog/305269/201411/061639577679041.jpg)
相关文章推荐
- 我的学习之旅:android expandablelistview的应用
- Android的ExpandableListView-android学习之旅(二十八)
- Android组件学习之ExpandableListView
- android中expandablelistview的学习
- Android学习第五天————ExpandableListView组件通过适配器BaseExpandableListAdapter实现两层列表项
- Android学习指南之四十三:用户界面View之ExpandableListView(手风琴效果Accordion)
- Android的ExpandableListView-android学习之旅(二十八)
- 学习Android实现可折叠ListView(ExpandableListView)
- android学习:ExpandableListView的运用
- Android的ExpandableListView-android学习之旅(二十八)
- android 学习之ExpandableListView
- android学习之----ExpandableListView组件
- Android 学习笔记之ExpandableListView UI的简单用法
- android学习:ExpandableListView的运用
- android-ExpandableListView(2)
- android ExpandableListView使用
- Android ExpandableListView的使用
- Android ExpandableListView的使用
- android 学习笔记(1) ExpandableListActivity 自定义view
- 【Android】学习笔记(6)——ListView(上)