前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >关于安卓开发实现可展开的列表组件

关于安卓开发实现可展开的列表组件

作者头像
听着music睡
发布于 2018-05-18 04:21:05
发布于 2018-05-18 04:21:05
1.1K00
代码可运行
举报
文章被收录于专栏:Android干货Android干货
运行总次数:0
代码可运行

三个布局文件 main.xml      childs.xml      groups.xml

一个java文件  List_lianxi.java

main.xml文件代码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6     <ExpandableListView 
 7         android:id="@id/android:list"
 8         android:layout_width="fill_parent"
 9         android:layout_height="fill_parent"
10         android:drawSelectorOnTop="false"
11                 />
12     <TextView 
13         android:layout_width="fill_parent"
14         android:layout_height="fill_parent"
15         android:id="@id/android:empty"
16         android:text="No Data"/> 
17 
18 </LinearLayout>

注意android:id="@id/android:list"  id 固定  否则无法运行

groups.xml文件代码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6     <TextView
 7         android:layout_width="fill_parent"
 8         android:layout_height="fill_parent"
 9         android:id="@+id/group"
10         android:textSize="25sp"
11         android:paddingLeft="35dp"
12         android:paddingRight="5dp"
13         android:paddingTop="10dp"
14         android:paddingBottom="10dp"
15         android:text="No Data"/>
16 
17 </LinearLayout>

childs.xml文件代码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6     <TextView
 7         android:layout_width="fill_parent"
 8         android:layout_height="fill_parent"
 9         android:id="@+id/child"
10         android:textSize="15sp"
11         android:paddingLeft="25dp"
12         android:paddingTop="10dp"
13         android:paddingRight="5dp"
14         android:paddingBottom="10dp"
15         android:text="No Data" 
16         />
17     
18 
19 </LinearLayout>

List_lianxi.java

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 1 package lianxi;
 2 
 3 import java.util.ArrayList;
 4 import java.util.HashMap;
 5 import java.util.List;
 6 import java.util.Map;
 7 
 8 import com.example.jichu_lianxi.R;
 9 
10 
11 import android.app.ExpandableListActivity;
12 import android.os.Bundle;
13 import android.view.View;
14 import android.widget.ExpandableListView;
15 import android.widget.SimpleExpandableListAdapter;
16 
17 public class List_lianxi extends ExpandableListActivity{            //继承ExpandableListActivity
18     @Override
19     public void onCreate(Bundle savedInstanceState) {
20         // TODO Auto-generated method stub
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.main);                               
23         
24         //创建一级条目容器
25         List<Map<String,String>>groups = new ArrayList<Map<String,String>>();
26         //创建两个一级条目标题
27         Map<String, String>group1 = new HashMap<String, String>();
28         group1.put("group", "音乐");
29         Map<String, String>group2 = new HashMap<String, String>();
30         group2.put("group", "歌词");
31         groups.add(group1);
32         groups.add(group2);
33         //创建一级条目下的二级条目
34         List<Map<String, String>> child_one = new  ArrayList<Map<String,String>>();
35         //在一级条目目录下创建两个对应的二级条目目录
36         Map<String, String> child_one_1 = new HashMap<String, String>();
37         child_one_1.put("child", "青花瓷");
38         Map<String, String> child_one_2 = new HashMap<String, String>();
39         child_one_2.put("child", "东风破");
40         child_one.add(child_one_1);
41         child_one.add(child_one_2);
42         
43         //第二个二级条目目录
44         List<Map<String, String>> child_two = new  ArrayList<Map<String,String>>();
45         Map<String, String> child_two_1 = new HashMap<String, String>();
46         child_two_1.put("child", "青花瓷.lrc");
47         Map<String, String> child_two_2 = new HashMap<String, String>();
48         child_two_2.put("child", "东风破.lrc");
49         child_two.add(child_two_1);
50         child_two.add(child_two_2);
51         
52         //将二级条目目录放在一个集合里供显示时使用
53         List<List<Map<String, String>>>childs = new ArrayList<List<Map<String,String>>>();
54         childs.add(child_one);
55         childs.add(child_two);
56         
57         /*
58          * 使用SimpleExpandableListAdapter显示ExpandableListView
59          * 参数1:上下文对象Context
60          * 参数2:一级条目目录集合
61          * 参数3:一级条目对应的布局文件
62          * 参数4:fromto,map中的key,指定要显示的对象
63          * 参数5:与参数4对应,指定要显示在groups中的id
64          * 参数6:二级条目目录集合
65          * 参数7:二级条目对应的布局文件
66          * 参数8:fromto.map中的key,指定要显示的对象
67          * 参数9:与参数8对应,指定要显示在childs中的id
68          */
69         SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(this, groups, R.layout.groups, new String[]{"group"}, new int[]{R.id.group}, childs, R.layout.childs, new String[]{"child"}, new int[]{R.id.child});
70         setListAdapter(adapter);
71 
72     }
73     //设置哪个二级目录被默认选中
74     @Override
75     public boolean setSelectedChild(int groupPosition, int childPosition,
76             boolean shouldExpandGroup) {
77         // TODO Auto-generated method stub
78         return super.setSelectedChild(groupPosition, childPosition, shouldExpandGroup);
79     }
80     //设置哪个一级目录被默认选中
81     @Override
82     public void setSelectedGroup(int groupPosition) {
83         // TODO Auto-generated method stub
84         super.setSelectedGroup(groupPosition);
85     }//当二级条目被点击时响应
86     @Override
87     public boolean onChildClick(ExpandableListView parent, View v,
88             int groupPosition, int childPosition, long id) {
89         // TODO Auto-generated method stub
90         return super.onChildClick(parent, v, groupPosition, childPosition, id);
91     }
92 }

运行效果:

参考代码源于:<<Android经典应用>>赵书兰 编著 p101---p105

其中有2个错误

1、main.xml中的 android:id="@+id/list" 应该为 android:id="@id/android:list"    否则运行出错

2、p102页 grouds.xml文件代码应该为groups.xml

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2013-03-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
站在巨人的肩膀上---重新自定义 android- ExpandableListView 收缩类,实现列表的可收缩扩展
林冠宏-指尖下的幽灵
2018/01/03
1.7K0
站在巨人的肩膀上---重新自定义 android- ExpandableListView 收缩类,实现列表的可收缩扩展
Android之ExpandableListView下拉分组的实现
ExpandableListView是android中可以实现下拉list的一个控件,具体的实现方法如下: 首先:在layout的xml文件中定义一个ExpandableListView <LinearLayout android:id="@+id/linearLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" androidrientation="vertical" > <
xiangzhihong
2018/01/26
7860
ExpandableListView实现商品列表折叠
简介 在日常的开发中,有可能会遇到需要一些可以展开的列表,比如QQ的好友列表,电商的购物车折叠显示。Android也给我们提供ExpandableListView类,完美实现这样类似的需求, 极大的方
xiangzhihong
2018/01/26
1.6K0
重走Android路之挑几个基本控件玩玩(上卷)
Android为我们提供了很多UI样式,但是在实际开发中,我们往往会根据UI图去为我们选择的绝大部分的控件进行渲染,从而达到产品更加的美观。
贺biubiu
2019/06/11
6590
Android-ListView 点击展开与收起效果实现
实际上这是采用一个ExpandableListView实现的 布局文件很简单:
圆号本昊
2021/09/24
1.3K0
Android-ListView 点击展开与收起效果实现
Android实现商城购物车功能
最近公司项目做商城模块,需要实现购物车功能,主要实现了单选、全选,金额合计,商品删除,商品数量加减等能,先看看效果图:
SoullessCoder
2019/08/07
4.6K0
Android实现商城购物车功能
android 三级级联筛选列表
实现的思路:左边这个是listView ,右边是两级的expandListView 
再见孙悟空_
2023/02/10
8540
android 三级级联筛选列表
ExpandableListView简单应用及listview模拟ExpandableListView
  首先我们还是来看一些案例,还是拿搜狐新闻客户端,因为我天天上下班没事爱看这个东东,上班又没时间看新闻,上下班路途之余浏览下新闻打发时间嘛. 看这个效果挺棒吧,其实实现起来也不难,我简单说明下.
xiangzhihong
2018/01/30
1.2K0
ExpandableListView简单应用及listview模拟ExpandableListView
可折叠列表ExpandableListView多级选择
上一期学习了AutoCompleteTextView和MultiAutoCompleteTextView,你已经掌握了吗?本期开始学习ExpandableListView的使用。 一、认识ExpandableListView ExpandableListView 是 ListView 的子类,它在普通ListView的基础上进行了扩展,它把应用中的列表项分为几组,每组里又可包含多个列表项。 ExpandableListView的用法与普通 ListView的用法非常相似,只是 Exp
分享达人秀
2018/02/05
2.6K0
可折叠列表ExpandableListView多级选择
Android仿淘宝购物车,玩转电商购物车
前言 其实做一个电商购物车,还真不是一个轻松的活。但是只要掌握思路,一步一步来做,就会发现也就这样。废物不多说,直接上效果图 完整代码,github链接,希望能给个星,谢谢 效果图 GIF1.gif
用户2032165
2018/06/05
2.9K0
【Android】使用Android开发应用过程中遇到ViewGroup的简单效以及aw和assets文件夹下的文件(Http协议的底层工作)
在Android APP中,所有用户界面元素都由View和ViewGroup对象组成。视图是用户在屏幕上绘制时可以与之交互的对象。ViewGroup是用于存储其他View(和ViewGroup)对象的布局容器!Android为我们提供了View和ViewGroup子类的集合,其中提供了一些常见的输入控件(如按钮和文本字段)和各种布局模式(如线性或相对布局)
上进小菜猪
2022/12/22
6840
【Android】使用Android开发应用过程中遇到ViewGroup的简单效以及aw和assets文件夹下的文件(Http协议的底层工作)
相关推荐
站在巨人的肩膀上---重新自定义 android- ExpandableListView 收缩类,实现列表的可收缩扩展
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文