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 ...
随机推荐
- 析构方法 deinit
/* 析构方法: 对象的内存被回收前被隐式调用的方法, 对应OC的dealloc方法 主要执行一些额外操作, 例如释放一些持有资源, 关闭文件, 断开网络等 */ class FileHandle { ...
- CAS优缺点
cas优点:如一描述在并发量不是很高时cas机制会提高效率.cas缺点:1.cpu开销大,在高并发下,许多线程,更新一变量,多次更新不成功,循环反复,给cpu带来大量压力.2.只是一个变量的原子性操作 ...
- python3 + zabbix api 的使用
喜欢需要理由吗?需要吗?当然需要,zabbix的那么多功能足以让你喜欢她,现在还有zabbix API,zabbix真让我疯了,太牛逼了,太让人喜欢了.有zabbix API我们可以做很多,自己开发w ...
- tcp ip三次握手链接和四次挥手断开
先来个整体的流程图 一 三次握手目的是为了建立连接... 1 核心的就是client端和service端,进行数据"报文" 交换 2 报文,目的是互相通知,确认链接 ...
- (转)MySQL慢查询日志总结
慢查询日志概念 原文:http://www.cnblogs.com/kerrycode/p/5593204.html MySQL的慢查询日志是MySQL提供的一种日志记录,它用来记录在MySQL中响应 ...
- Centos 7 安装 Visual stdio Code
最近微软正式发布了.net code 和asp.net code.尝试了下在linux下.net code和asp.net code使用. 具体怎么使用.net code 和asp.net code ...
- 《LeetBook》leetcode题解(15):3Sum[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- IntelliJ IDEA的黑白色背景切换(Ultimate和Community版本皆通用)
不多说,直接上干货! IntelliJ IDEA的黑白色背景切换 File -> Setting -> Editor -> Colors & F ...
- jieba分词加入特殊字符和空格
将原始jieba字典中的空格更改为 @@ 1.原来:雅不可攀 3 nr 2.现在:雅不可攀@@3@@nr 将自定义用户词典加入的词与词性中间的空格更改为@@ 1.原来:牵连关系 50 n 2.现在:牵 ...
- WPF将TextBox的边框设为圆角的
将TextBox的边框设为圆角的,因为TextBox默认的样式中边框就是由Border类型来实现的, 所以只需要真的当前的TextBox的Border修改属性即可,为了不影响界面中别的Border的样 ...