本文实例为大家分享了ExpandableListView实现手风琴效果的具体代码,供大家参考,具体内容如下

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
# 手风琴
# Android之带group指示器的ExpandableListView(自写)
# Android ExpandableListView展开列表控件使用实例
# Android ExpandableListView长按事件的完美解决办法
# Android之IphoneTreeView带组指示器的ExpandableListView效果
# Android UI控件ExpandableListView基本用法详解
# Android中ExpandableListView的用法实例
# Android 关于ExpandableListView刷新问题的解决方法
# android使用ExpandableListView控件实现小说目录效果的例子
# Android改变ExpandableListView的indicator图标实现方法
# Android 关于ExpandableListView去掉里头分割线的方法
# 种方法
# 重写
# 有两个
# 在这里
# 第一个
# 则是
# 适用于
# 数据结构
# 自定义
# 只有一个
# 不需
# 设置为
# 大家分享
# 若不
# 绑定
# 创建一个
# 图中
# 具体内容
# 值为
# 要再
相关文章:
建站主机系统SEO优化与智能配置核心关键词操作指南
如何高效生成建站之星成品网站源码?
建站主机服务器选型指南与性能优化方案解析
外汇网站制作流程,如何在工商银行网站上做外汇买卖?
制作农业网站的软件,比较好的农业网站推荐一下?
宝塔建站教程:一键部署配置流程与SEO优化实战指南
家庭服务器如何搭建个人网站?
建站VPS能否同时实现高效与安全翻墙?
小视频制作网站有哪些,有什么看国内小视频的网站,求推荐?
如何在IIS服务器上快速部署高效网站?
如何通过老薛主机一键快速建站?
天津个人网站制作公司,天津网约车驾驶员从业资格证官网?
C++中引用和指针有什么区别?(代码说明)
高防网站服务器:DDoS防御与BGP线路的AI智能防护方案
Android滚轮选择时间控件使用详解
如何通过WDCP绑定主域名及创建子域名站点?
建站一年半SEO优化实战指南:核心词挖掘与长尾流量提升策略
php能控制zigbee模块吗_php通过串口与cc2530 zigbee通信【介绍】
清除minerd进程的简单方法
高防服务器租用如何选择配置与防御等级?
香港服务器建站指南:外贸独立站搭建与跨境电商配置流程
宝塔Windows建站如何避免显示默认IIS页面?
高性能网站服务器配置指南:安全稳定与高效建站核心方案
在线流程图制作网站手机版,谁能推荐几个好的CG原画资源网站么?
高端企业智能建站程序:SEO优化与响应式模板定制开发
建站之星手机一键生成:多端自适应+小程序开发快速建站指南
湖南网站制作公司,湖南上善若水科技有限公司做什么的?
网站建设设计制作营销公司南阳,如何策划设计和建设网站?
C++如何将C风格字符串(char*)转换为std::string?(代码示例)
平台云上自主建站:模板化设计与智能工具打造高效网站
如何在Golang中处理模块冲突_解决依赖版本不兼容问题
建站之星代理如何获取技术支持?
山东网站制作公司有哪些,山东大源集团官网?
如何通过虚拟主机快速完成网站搭建?
淘宝制作网站有哪些,淘宝网官网主页?
独立制作一个网站多少钱,建立网站需要花多少钱?
头像制作网站在线观看,除了站酷,还有哪些比较好的设计网站?
如何快速重置建站主机并恢复默认配置?
建站主机与服务器功能差异如何区分?
建站上传速度慢?如何优化加速网站加载效率?
如何通过wdcp面板快速创建网站?
建站之星在线客服如何快速接入解答?
如何挑选优质建站一级代理提升网站排名?
javascript中的try catch异常捕获机制用法分析
青浦网站制作公司有哪些,苹果官网发货地是哪里?
北京网站制作网页,网站升级改版需要多久?
建站之星如何优化SEO以实现高效排名?
如何零成本快速生成个人自助网站?
北京制作网站的公司,北京铁路集团官方网站?
*请认真填写需求信息,我们会在24小时内与您取得联系。