ExpandableListView控件实现二级列表
效果图如下:

二级列表附有点击事件。
1、布局文件:
此处加了一个自定义的导航RelativeLayout,记得注activity的时候添加 android:theme="@style/Theme.AppCompat.Light.NoActionBar" 去掉自带的导航。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/dimen_55"
android:background="@color/main_title"
>
<TextView
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消"
android:textColor="@color/white"
android:textSize="@dimen/dimen_18"
android:layout_marginLeft="@dimen/dimen_13"
android:onClick="goBack"
/>
<TextView
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="安卓二级列表"
android:textColor="@color/white"
android:textSize="@dimen/dimen_18"/>
</RelativeLayout> <!--二级菜单-->
<ExpandableListView
android:id="@+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ExpandableListView> </LinearLayout>
2、一级列表布局:
ImageView是用来自定义打开闭合图标的(不自定义也可以,箭头会默认在最左边),建议自己用一个ImageView控件来控制上下箭头。图标可以去阿里巴巴矢量图上下载。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--一级列表 item布局--> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_group"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:gravity="center"
android:text="group text"
/>
<ImageView
android:id="@+id/iv_group"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="@dimen/dimen_20"
android:layout_width="20dp"
android:layout_height="20dp" />
</RelativeLayout> </LinearLayout>
3、二级列表布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<!--二级列表 item布局--> <ImageView
android:id="@+id/iv_child"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tv_child"
android:layout_marginLeft="@dimen/dimen_20"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="item text" /> </LinearLayout>
4、activity代码:
private ExpandableListView expandableListView;
//一级列表数据源
private String[] groups = {"软件设计", "数据库技术", "操作系统"};
//二级列表数据源
private String[][] childs={{"架构设计","面向对象","设计模式","领域驱动设计"},{"SQL Server","Oracle","MySql", "Dameng "},{"Linux","Windows","嵌入式"}};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_expandable_listview);
initView();
}
private void initView() {
expandableListView = (ExpandableListView)findViewById(R.id.expandableListView);
//#TODO 去掉自带箭头,在一级列表中动态添加
expandableListView.setGroupIndicator(null);
expandableListView.setAdapter(new MyExpandableListView());
}
public void goBack(View view) {
finish();
}
5、ExpandableListView适配器:
继承自BaseExpandableListAdapter,重写ExpandableListAdapter中的10个方法
class MyExpandableListView extends BaseExpandableListAdapter {
/*一级列表个数*/
@Override
public int getGroupCount() {
return groups.length;
}
/*每个二级列表的个数*/
@Override
public int getChildrenCount(int groupPosition) {
return childs[groupPosition].length;
}
/*一级列表中单个item*/
@Override
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
/*二级列表中单个item*/
@Override
public Object getChild(int groupPosition, int childPosition) {
return childs[groupPosition][childPosition];
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
/*每个item的id是否固定,一般为true*/
@Override
public boolean hasStableIds() {
return true;
}
/*#TODO 填充一级列表
* isExpanded 是否已经展开
* */
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.list_item_expandablelistview,null);
}
TextView tv_group = (TextView) convertView.findViewById(R.id.tv_group);
ImageView iv_group = (ImageView) convertView.findViewById(R.id.iv_group);
tv_group.setText(groups[groupPosition]);
//控制是否展开图标
if (isExpanded) {
iv_group.setImageResource(R.drawable.expand_iv_up);
} else {
iv_group.setImageResource(R.drawable.expand_iv_down);
}
return convertView;
}
/*#TODO 填充二级列表*/
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.list_item_expandablelistview_child,null);
}
ImageView image = (ImageView) convertView.findViewById(R.id.iv_child);
TextView tv = (TextView) convertView.findViewById(R.id.tv_child);
tv.setText(childs[groupPosition][childPosition]);
return convertView;
}
/*二级列表中每个能否被选中,如果有点击事件一定要设为true*/
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
到这里基本就完成了,最后再配置一下每个二级列表的点击事件即可:
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
TextView childAt = (TextView)((LinearLayout) v).getChildAt(1);//获得点击列表中TextView的值,需要强转一下,否则找不到getChildAt方法
5 Toast.makeText(ExpandableListViewActivity.this, "点击了 "+childAt.getText()+" 列表", Toast.LENGTH_SHORT).show();
return true;
}
});
ExpandableListView控件实现二级列表的更多相关文章
- android ExpandAbleListView控件
ExpandAbleListView控件 1.API对ExpandAbleListView的解释:
- Android中ExpandableListView控件基本使用
本文採用一个Demo来展示Android中ExpandableListView控件的使用,如怎样在组/子ListView中绑定数据源.直接上代码例如以下: 程序结构图: layout文件夹下的 mai ...
- Qt实现表格控件-支持多级列表头、多级行表头、单元格合并、字体设置等
目录 一.概述 二.效果展示 三.定制表头 1.重写数据源 2.重写QHeaderView 四.设置属性 五.相关文章 原文链接:Qt实现表格控件-支持多级列表头.多级行表头.单元格合并.字体设置等 ...
- [WP8.1UI控件编程]SemanticZoom控件实现分组列表
11.1.5 SemanticZoom实现分组列表 SemanticZoom控件可以让用户实现一种更加高级的列表,这种列表可以对列表的项目进行分组,同时这个SemanticZoom控件会提供两个具有相 ...
- MFC程序实现窗口分割,视图快捷插入控件和插入列表
将视图中插入列表: 1.创建一个MFC应用程序,在MFC Wizard中,生成的类选项,如图 2.选择CListView作为基类 3.在CXXView.cpp(XX为你的程序名)重写虚函数OnInit ...
- PagedDataSource数据绑定控件和AspNetPager分页控件结合使用列表分页
1.引用AspNetPager.dll. 2.放置Repeater数据绑定控件. <asp:Repeater ID="Repeater1" runat="serve ...
- Android 关于ExpandableListView控件setOnChildClickListener无效问题
其实很简单,在适配器里面重写isChildSelectable的时候返回值切记为true,这样才能使得二级监听有响应. 其次注意继承的是BaseExpandableListAdapter
- winfrom 窗体控件实现二级联动
ComboBox绑定数据源时触发SelectedIndexChanged事件的处理办法 事件,而这个时候用户并没有选择内容,其SelectedValue也不是对应字段的值.那么时写在SelectedI ...
- Windows的公共控件窗口类列表
The following window class names are provided by the common control library: ANIMATE_CLASS Creates a ...
随机推荐
- 【hdu6035】 Colorful Tree dfs序
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6035 题目大意:给你一棵树,树上每个节点都有一个颜色. 现在定义两点间的距离为两点最短路径上颜色集合 ...
- Maven与Hudson集成
Hudson是一款优秀的持续集成产品,本文阐述Maven于Hudson的集成 Hudson的下载和安装 Hudson有两种安装模式,1:自运行(Hudson内建netty容器),2:放到如tomc ...
- redis集群配置及运行命令(windows和centos)附Python测试范例代码
表示配置主服务器器的IP和端口 slaveof <masterip> <masterport> # 设置slave是否是只读的.从2.6版起,slave默认是只读的. slav ...
- 手机访问电脑中部署的tomcat应用
手机访问电脑中部署的tomcat应用. 操作步骤: 第一种:有无线路由的情况. 1.建议局域通信. 操作如下:电脑,手机都自动连接到无线路由器中(无线路由不必非要联网). 2.启动电脑用的tomcat ...
- Android中实时预览UI和编写UI的各种技巧
一.啰嗦 之前有读者反馈说,你搞这个所谓的最佳实践,每篇文章最后就给了一个库,感觉不是很高大上.其实,我在写这个系列之初就有想过这个问题.我的目的是:给出最实用的库来帮助我们开发,并且尽可能地说明这个 ...
- Web自动化 - 选择操作元素 2
文章转自 白月黑羽教Python 前面我们看到了根据 id.class属性.tag名 选择元素. 如果我们要选择的 元素 没有id.class 属性, 这时候我们通常可以通过 CSS selector ...
- 前端h5遇到的问题及解决办法
以后遇到的问题都记录在这里. 1.由于先有的pc端后需求手机端,所以没有用框架做适配,而是手动媒体查询进行手机端.pad.pc 三端适配,界面比较简单,所以这么做也不复杂,就是坑比较多. 2.移动和p ...
- Mysql的预编译和批处理
MySQL的预编译功能 预编译的好处 大家平时都使用过JDBC中的PreparedStatement接口,它有预编译功能.什么是预编译功能呢?它有什么好处呢? 当客户发送一条SQL语句给服务器后,服务 ...
- 自动换行的两种代码(C#)
最近有个需求,需要将C# winform中的listBox中的内容自动换行, 其实在用listBox前,已经用textBox实现了大部分功能,可惜最后还是有个焦点的问题, 就是textBox中的文字会 ...
- IOS项目之弹出动画一
小区宝首页导航栏左边有一个物业按钮,点击时会出现一个视图动画,之前用的是一个POP第三方,想着几个POP动画就要引用一堆的第三方有点麻烦,就试着自己写了一下,功能实现了,下一步就是优化将其封装一下.下 ...