ExpandableListView控件实现二级列表

效果图如下:

二级列表附有点击事件。

1、布局文件:

此处加了一个自定义的导航RelativeLayout,记得注activity的时候添加 android:theme="@style/Theme.AppCompat.Light.NoActionBar" 去掉自带的导航。

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent">
 6
 7     <RelativeLayout
 8         android:layout_width="match_parent"
 9         android:layout_height="@dimen/dimen_55"
10         android:background="@color/main_title"
11         >
12         <TextView
13             android:layout_centerVertical="true"
14             android:layout_width="wrap_content"
15             android:layout_height="wrap_content"
16             android:text="取消"
17             android:textColor="@color/white"
18             android:textSize="@dimen/dimen_18"
19             android:layout_marginLeft="@dimen/dimen_13"
20             android:onClick="goBack"
21             />
22         <TextView
23             android:layout_centerVertical="true"
24             android:layout_centerHorizontal="true"
25             android:layout_width="wrap_content"
26             android:layout_height="wrap_content"
27             android:text="安卓二级列表"
28             android:textColor="@color/white"
29             android:textSize="@dimen/dimen_18"/>
30     </RelativeLayout>
31
32     <!--二级菜单-->
33     <ExpandableListView
34         android:id="@+id/expandableListView"
35         android:layout_width="match_parent"
36         android:layout_height="wrap_content">
37     </ExpandableListView>
38
39 </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代码:

 1     private ExpandableListView expandableListView;
 2
 3     //一级列表数据源
 4     private String[] groups = {"软件设计", "数据库技术", "操作系统"};
 5     //二级列表数据源
 6     private String[][] childs={{"架构设计","面向对象","设计模式","领域驱动设计"},{"SQL Server","Oracle","MySql", "Dameng "},{"Linux","Windows","嵌入式"}};
 7
 8
 9     @Override
10     protected void onCreate(Bundle savedInstanceState) {
11         super.onCreate(savedInstanceState);
12         setContentView(R.layout.activity_expandable_listview);
13
14         initView();
15     }
16     private void initView() {
17         expandableListView = (ExpandableListView)findViewById(R.id.expandableListView);
18         //#TODO 去掉自带箭头,在一级列表中动态添加
19         expandableListView.setGroupIndicator(null);
20         expandableListView.setAdapter(new MyExpandableListView());
21
22     }
23     public void goBack(View view) {
24         finish();
25     }

5、ExpandableListView适配器:

继承自BaseExpandableListAdapter,重写ExpandableListAdapter中的10个方法
 1 class MyExpandableListView extends BaseExpandableListAdapter {
 2
 3         /*一级列表个数*/
 4         @Override
 5         public int getGroupCount() {
 6             return groups.length;
 7         }
 8
 9         /*每个二级列表的个数*/
10         @Override
11         public int getChildrenCount(int groupPosition) {
12             return childs[groupPosition].length;
13         }
14
15         /*一级列表中单个item*/
16         @Override
17         public Object getGroup(int groupPosition) {
18             return groups[groupPosition];
19         }
20
21         /*二级列表中单个item*/
22         @Override
23         public Object getChild(int groupPosition, int childPosition) {
24             return childs[groupPosition][childPosition];
25         }
26
27         @Override
28         public long getGroupId(int groupPosition) {
29             return groupPosition;
30         }
31
32         @Override
33         public long getChildId(int groupPosition, int childPosition) {
34             return childPosition;
35         }
36
37         /*每个item的id是否固定,一般为true*/
38         @Override
39         public boolean hasStableIds() {
40             return true;
41         }
42
43         /*#TODO 填充一级列表
44         * isExpanded 是否已经展开
45         * */
46         @Override
47         public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
48             if (convertView == null) {
49                 convertView = getLayoutInflater().inflate(R.layout.list_item_expandablelistview,null);
50             }
51             TextView tv_group = (TextView) convertView.findViewById(R.id.tv_group);
52             ImageView iv_group = (ImageView) convertView.findViewById(R.id.iv_group);
53             tv_group.setText(groups[groupPosition]);
54             //控制是否展开图标
55             if (isExpanded) {
56                 iv_group.setImageResource(R.drawable.expand_iv_up);
57             } else {
58                 iv_group.setImageResource(R.drawable.expand_iv_down);
59             }
60             return convertView;
61         }
62
63         /*#TODO 填充二级列表*/
64         @Override
65         public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
66             if (convertView == null) {
67                 convertView = getLayoutInflater().inflate(R.layout.list_item_expandablelistview_child,null);
68             }
69             ImageView image = (ImageView) convertView.findViewById(R.id.iv_child);
70             TextView tv = (TextView) convertView.findViewById(R.id.tv_child);
71             tv.setText(childs[groupPosition][childPosition]);
72             return convertView;
73         }
74
75         /*二级列表中每个能否被选中,如果有点击事件一定要设为true*/
76         @Override
77         public boolean isChildSelectable(int groupPosition, int childPosition) {
78             return true;
79         }
80
81
82     }

到这里基本就完成了,最后再配置一下每个二级列表的点击事件即可:

1 expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
2             @Override
3             public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
4                 TextView childAt = (TextView)((LinearLayout) v).getChildAt(1);//获得点击列表中TextView的值,需要强转一下,否则找不到getChildAt方法5                 Toast.makeText(ExpandableListViewActivity.this, "点击了 "+childAt.getText()+" 列表", Toast.LENGTH_SHORT).show(); 

6                 return true; 
7              } 8  });

原文地址:https://www.cnblogs.com/blog4wei/p/8875889.html

时间: 2024-08-25 16:21:52

ExpandableListView控件实现二级列表的相关文章

Android中ExpandableListView控件基本使用

本文採用一个Demo来展示Android中ExpandableListView控件的使用,如怎样在组/子ListView中绑定数据源.直接上代码例如以下: 程序结构图: layout文件夹下的 main.xml 文件源代码例如以下: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/a

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

目录 一.概述 二.效果展示 三.定制表头 1.重写数据源 2.重写QHeaderView 四.设置属性 五.相关文章 原文链接:Qt实现表格控件-支持多级列表头.多级行表头.单元格合并.字体设置等 一.概述 最近在研究QTableView支持多级表头的事情,百度了下网上资料还是挺多的.实现的方式总的来说有2种,效果都还不错,最主要是搞懂其中的原理,做到以不变应万变. 实现多级表头的方式有以下两种方案 行表头和列表头都是用一个表格去模拟 重写QHeadView 以上两种方式都可以实现多级表头,各

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

11.1.5 SemanticZoom实现分组列表 SemanticZoom控件可以让用户实现一种更加高级的列表,这种列表可以对列表的项目进行分组,同时这个SemanticZoom控件会提供两个具有相同内容的不同视图,其中有一个是主视图,另外一个视图可以让用户进行快速导航的分组视图.例如,Windows Phone里面的人脉通讯录列表就是使用SemanticZoom控件实现的. SemanticZoom控件支持对GridView和ListView控件的视图效果进行缩放,在SemanticZoom

Windows Phone 7 LongListSelector控件实现分类列表和字母索引

在wp7手机里面的联系人列表和程序里面里面我们可以看到一个根据字母索引来定位联系人或者应用程序的控件,那么这个控件就是LongListSelector控件了. LongListSelector是一种比ListBox更加强大的列表控件,你可以根据你列表的信息来分类排列,根据类别快速定位到你选中的类别的列表下,在数据量很大的情况下这种分类的优势很明显.LongListSelector可以自定义列表头,列表尾.类表头.列别尾等的样式和数据,可以实现各种个性化的列表样式和不同的数据的展现方式.Windo

ExpandableListView控件基本使用

第一种用法:效果图如下: 1.三个布局文件: main.xml  注意:  android:id="@id/android:list"不能写自己的 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="

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

1.引用AspNetPager.dll. 2.放置Repeater数据绑定控件. <asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> //绑定显示的列表代码 </ItemTemplate> </asp:Repeater> 3.在页面添加AspNetPager分页控件,会出现以下代码. <%@ Register Assembly="Asp

android使用自定控件实现城市列表展示并且实现当前城市定位

自定义控件,点击控件,展示城市列表,使用百度地图定位当前城市,并且展示当前城市 代码类 package com.example.test0504; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import androi

windows窗体控件之listview列表视图

1.添加标题 winform.listview.gridLines=true;//显示列表线,也可在属性表设置 winform.listview.insertColumn("列标题",列宽,位置,样式) winform.listview.insertColumn("标题2",列宽,位置,样式)//后加的在前面(若不注明位置) winform.listview.insertColumn("第一列", 40, 1); winform.listview

winfrom 窗体控件实现二级联动

事件,而这个时候用户并没有选择内容,其SelectedValue也不是对应字段的值.那么时写在SelectedIndexChanged中的处理代码就会因为SelectedValue的内容不正确引发异常.一般网上找到的方法是添加一个标记位,在绑定前设置为false,绑定完成后设置回true. 绑定到ComboBox void BindComboBox() { flag=false; ComboxBox1.ValueMember="ValueColumn"; ComboxBox1.Disp