1. 效果示例图

2. 创建方法

(1)第一种方法与ListView等普通控件一样,直接在布局文件中添加ExpandableListView控件即可。

(2)第二种方法则是创建一个Activity继承自ExpandableListActivity,而后通过getExpandableListView()方法可获得一个ExpandableListView对象。

第二种方法仅适用于一个页面中只有一个ExpandableListView的情况。继承的Activity不需要再调用setContentView()方法,在ExpandableListActivity中已经关联了一个系统定义的布局文件。

3. 部分属性和点击事件

android:groupIndicator、android:childIndicator:组条目和子条目前面的图标,默认值为箭头,可设置自定义图片资源。若不显示该图标,则设置为@null。

android:divider、android:childDivider:组和子条目的分隔线。

ExpandableListView的点击事件有两个,分别对应组和子条目的点击事件:

设置组的点击事件:setOnGroupClickListener(OnGroupClickListener listener)

设置子条目的点击事件:setOnChildClickListener(OnChildClickListener listener)

5. 适配器

根据数据源的不同,可使用的适配器有两个:BaseExpandableListAdapter和CursorTreeAdapter,其中,CursorTreeAdapter用于数据源为Cursor对象的情况下,其它情况则使用BaseExpandableListAdapter。

(1)BaseExpandableListAdapter需要重写的方法:

getGroup():从数据源中获取组的数据内容。

getGroupCount():获取组的总数。

getGroupId():获取组的ID。

getGroupView():获取组的视图。

getChild():从数据源中获取子条目的内容。

getChildCount():获取指定组中的子条目总数,并非全部的子条目。

getChildId():获取子条目的ID。

getChildView():获取子条目的视图

hasStableIds():判断id对应的条目是否已经绘制,用于优化列表。

isChildSelectable():子条目是否允许点击,若返回false,则子条目点击事件无效。

(2)CursorTreeAdapter需要重写的方法:

CursorTreeAdapter():构造方法传入组的Cursor对象。

getChildrenCursor():传入组的Cursor对象,获取相应的组的子条目的Cursor对象。

newGroupView():创建组的视图,返回一个新的视图。

bindGroupView():在这里绑定组视图的数据内容,第一个参数即newGroupView()方法的返回值。

newChildView():创建子条目的视图。

bindChildView():绑定子条目视图的数据内容。

6. 简单范例

实现效果图中的例子。

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.studying.expandablelistviewdemo.MainActivity"> <ExpandableListView
android:id="@+id/elv_local_data"
android:layout_width="match_parent"
android:layout_height="match_parent" /> </LinearLayout>

Activity:

public class MainActivity extends Activity {

    private ExpandableListView elv;

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); elv = (ExpandableListView) findViewById(R.id.elv_local_data);
MyBaseExpandableListAdapter adapter = new MyBaseExpandableListAdapter(this, LoadData.getGroupData(), LoadData.getChildData());
elv.setAdapter(adapter);
}
}

加载测试数据用的工具类:

public class LoadData {

    // 组的数据内容
public static List<String> getGroupData() {
List<String> groupDataList = new ArrayList<>();
groupDataList.add("计算机基础");
groupDataList.add("安卓开发");
return groupDataList;
} // 子条目的数据内容
public static List<List<String>> getChildData() {
List<List<String>> childDataList = new ArrayList<>(); List<String> group1 = new ArrayList<>();
group1.add("数据结构");
group1.add("算法");
group1.add("计算机网络");
childDataList.add(group1); List<String> group2 = new ArrayList<>();
group2.add("控件使用");
group2.add("网络操作");
group2.add("数据存储");
group2.add("四大组件");
childDataList.add(group2); return childDataList;
}
}

适配器:

public class MyBaseExpandableListAdapter extends BaseExpandableListAdapter {

    private Context mContext;

    private List<String> groupName;
private List<List<String>> childName; public MyBaseExpandableListAdapter(Context mContext, List<String> groupName, List<List<String>> childName) {
this.mContext = mContext;
this.groupName = groupName;
this.childName = childName;
} @Override
public int getGroupCount() {
return groupName.size();
} @Override
public long getGroupId(int groupPosition) {
return groupPosition;
} @Override
public String getGroup(int groupPosition) {
return groupName.get(groupPosition);
} @Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { convertView = View.inflate(mContext, R.layout.item_group_name, null); TextView groupName = (TextView) convertView.findViewById(R.id.group_name);
groupName.setText(getGroup(groupPosition)); return convertView;
} @Override
public int getChildrenCount(int groupPosition) {
return childName.get(groupPosition).size();
} @Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
} @Override
public String getChild(int groupPosition, int childPosition) {
return childName.get(groupPosition).get(childPosition);
} @Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { convertView = View.inflate(mContext, R.layout.item_child_name, null); TextView childName = (TextView) convertView.findViewById(R.id.child_name);
childName.setText(getChild(groupPosition, childPosition)); return convertView;
} @Override
public boolean hasStableIds() {
return false;
} @Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}

ExpandableListView实现手风琴效果的更多相关文章

  1. 基于css3实现手风琴效果

    终于在凌晨一点钟逼迫自己写博客.一直想记录自己的前端工程师之路,但毕竟拖延症晚期.因为第一篇随笔,所以多写一点废话吧.刚刚从学校毕业,放弃了一直学习的java,而想从事前端的工作.第一是觉得osgi这 ...

  2. 使用 jQuery & CSS3 实现优雅的手风琴效果

    手风琴效果常用于切换显示一组内容,这种方式既可以节省网页空间又可以有动画效果.今天,我们将创造一个优雅的手风琴内容效果.这个想法是有悬停时滑出一些垂直手风琴标签.我们将添加一些 CSS3 属性来提升外 ...

  3. 基于 jQuery 实现垂直滑动的手风琴效果

    今天我们要与大家分享一个漂亮而灵活的垂直 jQuery 手风琴效果.其主要思想是扩大手风琴片上的点击和显示更多的信息.其他内容片段将变得不那么透明.当使用一个导航箭头导航下一个片段,新的片会从顶部或底 ...

  4. JS+JQ手风琴效果

    最新在学习JS写一些实用的小玩意——手风琴 CSS样式: <style type="text/css"> * { margin: 0px; border: 0px; p ...

  5. 纯CSS手风琴效果

    CSS手风琴效果 主体代码如下:                                                                                     ...

  6. jquery 图片手风琴效果

    这篇主要是手风琴效果和无缝切换相结合,在Demo里的Demo3.html.Demo4.html. 手风琴原理比较简单,当鼠标经过的时候改变图片的路径,鼠标移到另一张图片时还原路径. 虽然原理简单,但是 ...

  7. 一步步教你css3手风琴效果的实现

    什么是手风琴效果? 首先我们先来看一段动画,如下图所示: 在上面动画中,我们不难发现,一排照片正常排列,当我鼠标移上(:hover)时,照片会变大显示并且把其它照片挤小.那么在鼠标来回移动的过程中,画 ...

  8. 使用JS实现手风琴效果

    想要实现简单的手风琴切换效果,需要使用JS实现,如下是使用javascript源码实现,后续会更新使用jQuery实现. 1. 先进行简单的布局:我们可以再ul下添加几个li实现html的简单布局,再 ...

  9. 超简单jQuary链式操作代码实现手风琴效果

    超简单jQuery代码实现手风琴效果 HTML代码 <div id="cont"> <div> <p>人生若只如初见</p> < ...

随机推荐

  1. Postgresql快速写入/读取大量数据(.net)

    环境及测试 使用.net驱动npgsql连接post数据库.配置:win10 x64, i5-4590, 16G DDR3, SSD 850EVO. postgresql 9.6.3,数据库与数据都安 ...

  2. Html 学习

    行内元素和块级元素 行内元素(行级元素) 多个元素会在一行内显示 块级元素 独立成行 注意:块级元素能够嵌套行内元素 <div> <span></span> < ...

  3. java字符串,包,数组及空心正方形,菱形的实例

    一.数组:相同类型的多个对像引用类型:所有的类,接口,数组,int[] ints(变量名) = new int[3]new:指的是在内存空间重新开辟一块区域 String s1 = "abc ...

  4. C# 调用.exe文件

    process da = new process(); da.startinfo.filename = @""D:\BM0002\BM0002.exe";  //要调用的 ...

  5. 使用Vue-resource完成交互

    使用vue-resource 引入vue-resource vue-resource就像jQuery里的$.ajax,是用来跟后端交互数据的,vue-resource是vue的一个插件,所以我们在开始 ...

  6. RabbitMq学习一入门篇(hello world)

    简介 RabbitMQ是一个开源的AMQP实现,服务器端用Erlang语言编写,支持多种客户端,如:Python.Ruby..NET.Java,也是众多消息队列中表现不俗的一员,作用就是提高系统的并发 ...

  7. 运行Jmeter.bat出错:Not able to find java executor or version. Please check your installation. errorlevel=2

    下载JMeter. 解压后运行Jmeter.bat竟然报错了. 解决办法整理: 方法1: 1.检查JDK环境变量配置: ①系统变量→新增JAVA_HOME. 变量值填写jdk的安装目录(本人是 E:\ ...

  8. (转)springMVC框架下JQuery传递并解析Json数据

    springMVC框架下JQuery传递并解析Json数据 json作为一种轻量级的数据交换格式,在前后台数据交换中占据着非常重要的地位.Json的语法非常简单,采用的是键值对表示形式.JSON 可以 ...

  9. (转)搬瓦工(bandwagonhost)后台管理VPS

    1. Bandwagonghost使用建议 购买了搬瓦工(bandwagonhost)的VPS,如何使用呢? 首先插几句使用建议,老高认为十分重要,为什么呢?搬瓦工如果监控到有大量的垃圾信息从我们的主 ...

  10. jqthumb.js缩略图插件-让缩略图正常显示而不变形

    插件介绍 项目中有图片的时候,有的宽大于高,有的宽小于高,尤其在做图片列表的时候,经常发现缩略图会有变形的.我们怎样来解决这一问题呢. 方法一:用photoshop来处理缩略图.小的项目还可以,大的项 ...