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 ...
随机推荐
- Hive执行过程
http://blog.csdn.net/wf1982/article/details/9122543
- 模板模式(TemplateMethod)
什么是Template Method模式 在父类中定义处理流程的框架,在子类中实现具体处理的模式就称为Template Mehtod模式.模板模式的关键是:子类可以置换掉父类的可变部分,但是子类却不可 ...
- Python -- Gui编程 -- MFC的使用
1.消息框 mfcDialog.py import win32ui import win32con from pywin.mfc import dialog class MyDialog(dialog ...
- 关于开发板用tftp下载失败分析
一.想实现开发板和PC ping通:(1)windows和linux桥接(2)用路由器将开发板和PC连接起来(3)将windows和linux以及开发板的IP设置成同一网段,注意不要和你同一个局域网的 ...
- 【好书分享】容器网络到kubernetes网络
Nginx 公司的 Michael Hausenblas 发布了一本关于 docker 和 kubernetes 中的容器网络的小册子.这份资料一共 72 页,是大家由浅入深的了解 Docker 和 ...
- 解决python3与python2的pip命令冲突问题冲突(window版)
解决方法再上一篇有大概讲解: python开发环境安装配置 这里做一些补充: 上一篇说过,删除python3和python2中的python.exe文件后关闭dos窗口,重新打开dos,就可以进行安装 ...
- 安装scrapy报错 error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools
报错内容:Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools" ...
- onsubmit解惑
1.onsubmit的位置: onsubmit只存在于html <form>中,js的form中 2.submit与onsubmit的区别 发生顺序:onsubmit -> subm ...
- kafka-spark streaming (一)
Kafka-spark streaming 1.安装包 kafka安装需要zookeeper.jdk. 官网下载最新的: https://kafka.apache.org/downloads http ...
- SQL Server中使用表值函数
函数有很多限制,不能使用动态语句,不能使用临时表等等...细看一下,直接写语句就行了,不用动态语句 insert into @re select id,parid,@I from videoclass ...