效果图如下:

二级列表附有点击事件。

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控件实现二级列表的更多相关文章

  1. android ExpandAbleListView控件

    ExpandAbleListView控件 1.API对ExpandAbleListView的解释:

  2. Android中ExpandableListView控件基本使用

    本文採用一个Demo来展示Android中ExpandableListView控件的使用,如怎样在组/子ListView中绑定数据源.直接上代码例如以下: 程序结构图: layout文件夹下的 mai ...

  3. Qt实现表格控件-支持多级列表头、多级行表头、单元格合并、字体设置等

    目录 一.概述 二.效果展示 三.定制表头 1.重写数据源 2.重写QHeaderView 四.设置属性 五.相关文章 原文链接:Qt实现表格控件-支持多级列表头.多级行表头.单元格合并.字体设置等 ...

  4. [WP8.1UI控件编程]SemanticZoom控件实现分组列表

    11.1.5 SemanticZoom实现分组列表 SemanticZoom控件可以让用户实现一种更加高级的列表,这种列表可以对列表的项目进行分组,同时这个SemanticZoom控件会提供两个具有相 ...

  5. MFC程序实现窗口分割,视图快捷插入控件和插入列表

    将视图中插入列表: 1.创建一个MFC应用程序,在MFC Wizard中,生成的类选项,如图 2.选择CListView作为基类 3.在CXXView.cpp(XX为你的程序名)重写虚函数OnInit ...

  6. PagedDataSource数据绑定控件和AspNetPager分页控件结合使用列表分页

    1.引用AspNetPager.dll. 2.放置Repeater数据绑定控件. <asp:Repeater ID="Repeater1" runat="serve ...

  7. Android 关于ExpandableListView控件setOnChildClickListener无效问题

    其实很简单,在适配器里面重写isChildSelectable的时候返回值切记为true,这样才能使得二级监听有响应. 其次注意继承的是BaseExpandableListAdapter

  8. winfrom 窗体控件实现二级联动

    ComboBox绑定数据源时触发SelectedIndexChanged事件的处理办法 事件,而这个时候用户并没有选择内容,其SelectedValue也不是对应字段的值.那么时写在SelectedI ...

  9. Windows的公共控件窗口类列表

    The following window class names are provided by the common control library: ANIMATE_CLASS Creates a ...

随机推荐

  1. 【BZOJ2300】【HAOI2011】防线修建

    题目大意:给你m+3个点,有q个操作,每次要么询问当前点集构所构成的上凸壳总长度,要么在当前点集中删除一个点. 这题是吼题啊!!! 刚开始想着如何正常地做,考虑过用线段树维护一个区间内的凸包,发现并不 ...

  2. Linux 上安装 Couchbase服务

    down: http://www.couchbase.com/downloads/ doc:  http://docs.couchbase.com/archive-index/ forums: htt ...

  3. Scala的下载和安装(本地)

    前言 Scala版本的选法: 目前,Kafka库和JDBC并不支持Scala2.11的编译,以及结合大多数人的使用请来看.         scala2.10.*为主,在这,scala2.10.4版本 ...

  4. Disconf 学习系列之Disconf 的模块架构图

    不多说,直接上干货!  Disconf 的模块架构主要包括: Disconf-Tools . Disconf-Web. Disconf-client 和  Disconf-Core. 每个模块的简单介 ...

  5. GRU

    GRU模型(比LSTM减少了计算量) LSTM的模型,LSTM的重复网络模块的结构很复杂,它实现了三个门计算,即遗忘门.输入门和输出门. 而GRU模型如下,它只有两个门了,分别为更新门和重置门,即图中 ...

  6. mongodb-分组分页

    1, 添加测试数据 @Test public void save() { News n = null; ; i < ; i++) { n = new News(); n.setTitle(&qu ...

  7. 使用signtool.exe来验证程序的数字签名是否成功(命令行)

    signtool.exe是微软提供的数字签名工具,使用如下 signtool verify /pa YourApplicationName

  8. C#的SubString(int start,int end);

    C#的SubString(); 例子: string str = "i am a girl"; string temp = str.Substring(2,2);//从索引2开始, ...

  9. 笔记二:python编码详解

    一:学习内容 python编码讲解 python编码说明 python中文乱码解决三部曲 二:python编码讲解 1. ASCII编码 美国信息交换标准代码(American Standard Co ...

  10. lucene源码分析(7)Analyzer分析

    1.Analyzer的使用 Analyzer使用在IndexWriter的构造方法 /** * Constructs a new IndexWriter per the settings given ...