ExpandableListView的使用(一)
ExpandableListView的常用属性的使用(一)
在使用过RecyclerView的朋友们再使用这个可能会发现,这TM怎么会这样,原因无外乎两点:
1、 在从源头传递数据时RecyclerView是传递一个数据源过去,而ExpandListView是传递2个不同的数据源(前提是json数据不规范的情况下)
2、 RecyclerView在它的adapter中可以实现多布局,但是一定要再套入ListView或者GridView来展示多布局的内容(这样就又要给ListView写adapter),而ExpandListView在它的adapter中也可以实现多布局(一般为2种,也可以多种子项),但是不需要频繁的嵌入ListView或者GridView来展示内容,组件自带的只要数据源是List集合就可以实现ListView的展示效果。
下面展示ExpandListView的项目效果图:
看完上面的内容,相信你已迫不及待的想看看代码了,下面我将奉上我这没什么组织的代码层。
首先,我需要3个数据源,相信会问,为什么要3个,一个最外层的Group类目,一个内层的Child类目,那还有一个呢,是我们用于点击的Child类目下的id(实际运用中需要传递这样的id去执行刷新界面的操作)
1、创建数据源并初始化
private List<String> groupData; //1级类目数据源
private List<List<String>> childData; //2级类目数据源
private List<List<String>> childDataID; //2级类目数据源下对应的id
private ProductList_Filter_CategoryAdapter2 categoryAdapter2; //ExpandListView的适配器
private ExpandableListView product_store_categoryList;//记得去初始化它的id
... ...
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view = inflater.inflate(R.layout.productlist_filter_normal2, container, false);ButterKnife.inject(this, view);initDatas(); //初始化数据源initUI(); //UI调整initListener(); //监听事件return view;}
//初始化数据源
private void initDatas() {groupData = new ArrayList<String>();childData = new ArrayList<List<String>>();childDataID = new ArrayList<List<String>>();
}
2、UI界面的交互以及adapter的数据绑定
private void initUI() {categoryAdapter2 = new ProductList_Filter_CategoryAdapter2(groupData, childData, new WeakReference<Context>(getActivity()), null);categoryAdapter2.setData(groupData, childData);product_store_categoryList.setAdapter(categoryAdapter2);if (categoryAdapter2.getGroupCount() > 0) { //默认展开第一项分组product_store_categoryList.expandGroup(0);} product_store_categoryList.setGroupIndicator(null);//将控件默认的左边箭头去掉
}
3、ExpandListView的监听事件
一、2级类目的点击监听事件:
product_store_categoryList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {@Overridepublic boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long id) {String idChild = childDataID.get(groupPosition).get(childPosition); //获取2级类目中点击的哪一项的idgetProductByCategoryStoreChild(idChild);//自定义方法去执行跳转刷新操作(我项目中使用的是发广播的方式)return false;}});
二、1级类目的点击监听事件:
(实现只展开一个2级类目的效果:)
方法一:
//先定义一个标记来记录2级列表的展开状态private int sign = 0;//控制列表的展开
然后我们的思路是:
1、由于设置的是默认打开第一级类目下的二级类目,所以这里要将第一个位置和其他位置分开来做判断;
2、在有子类情况下,执行for循环来关闭(collapseGroup)其他无关非点击项下的2级类目,打开(expandGroup)当前点击项下的2级类目;
3、在group没有child的情况下,不展开group ,执行 最后return返回的boolean来判断。
//这个方法可实现打开点击项的2级类目,关闭其他2级类目(但是我使用中出现个问题就是:在我既使用了group监听又使用了child监听时会出现2级类目下的数据重复现象)
product_store_categoryList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {@Overridepublic boolean onGroupClick(ExpandableListView expandableListView, View view, int groupPosition, long id) {if (groupPosition == 0) { //第一个类目if (sign == 0) { // 0 的时候表示是打开状态product_store_categoryList.collapseGroup(sign);sign = 1;} else { //1 的时候表示是闭合状态for (int i = 0; i < categoryAdapter2.getGroupCount(); i++) {if (groupPosition != i) {product_store_categoryList.collapseGroup(i); //收起其他层}}product_store_categoryList.expandGroup(groupPosition, true);sign = 0;}} else {if (sign == 0){sign = 1;}else {sign = 0;}for (int i = 0; i < categoryAdapter2.getGroupCount(); i++) {if (groupPosition != i) {product_store_categoryList.collapseGroup(i); //收起其他层}}product_store_categoryList.expandGroup(groupPosition, true);}if (childData.get(groupPosition).isEmpty()) { //如果没有2级分类情况分析return true;} else {categoryAdapter2.notifyDataSetChanged();return false;}
// return false;//默认为false,设为true时,点击事件不会展开Group}});
方法二:(推荐使用)
//不用GroupClick的监听,改用GroupExpand来实现是否展开1级类目(不会有数据重复的现象,值得推荐)
product_store_categoryList.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {@Overridepublic void onGroupExpand(int i) {for (int j = 0; j < categoryAdapter2.getGroupCount(); j++) {if (j != i) {product_store_categoryList.collapseGroup(j); //收起其他层}}}});
补充:
对ExpandableListView去掉默认箭头和互斥展开
private ExpandableListView expandListView;
1、去掉默认箭头
expandListView.setGroupIndicator(null);
2、互斥展开
expandListView.setOnGroupExpandListener(new OnGroupExpandListener(){ public void onGroupExpand(int groupPosition) {for (int i = 0; i < adapter.getGroupCount(); i++) {// ensure only one expanded Group exists at every timeif (groupPosition != i && expandListView.isGroupExpanded(groupPosition)) {expandListView.collapseGroup(i);}}}
});
ExpandableListView的使用(一)相关推荐
- 使用ExpandableListView以及如何优化view的显示减少内存占用
上篇博客讲到如何获取手机中所有歌曲的信息.本文就把上篇获取到的歌曲按照歌手名字分类.用一个ExpandableListView显示出来. MainActivity .java public class ...
- android二级列表展开,ExpandableListView控件实现二级列表
效果图如下: 二级列表附有点击事件. 1.布局文件: 此处加了一个自定义的导航RelativeLayout,记得注activity的时候添加 android:theme="@style/Th ...
- ExpandableListView(一)替换系统默认的箭头
很多朋友可能在android开发中,用过ExpandableListView这个组件,这个组件功能强大,比传统的ListView有好多优势.然而在开发中,我相信有好多人,包括我个人都会遇到下面的一些问 ...
- ExpandableListView的使用以及信息的高亮显示
ExpandableListView是ListView控件的延伸,它能够对数据进行分组显示和隐藏,并统计总数量.可进行滚动,对某一内容高亮显示. <1>编写xml布局文件,用于获取Expa ...
- ExpandableListView 里面嵌套GridView实现高度自适应
很早之前做过一个商城的app 也是第一次做安卓. 实现的效果如下: 因为一开始做安卓,很多写的代码都不规范,在下面上代码的地方,还请高手指点(勿喷,楼主是自尊心很强的屌丝) 这个效果要解决2个大问题, ...
- Android中ExpandableListView的使用
Android中ExpandableListView的使用 ExpandableListView是Android中可以实现下拉list的一个控件,具体的实现方法如下: 首先:在layout的xml文件 ...
- 替换ExpandableListView右边箭头Group Indicator(小图标)
先在drawable目录下建立xml文件: <?xml version="1.0" encoding="utf-8"?> <select ...
- android 中使用ExpandableListView控件结合服务器json文件的下载
布局文件代码: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns ...
- android 之ExpandableListView详解
ExpandableListView是一种可应用于某种环境的下拉列表. 实例代码: package com.example.lenovo.expandablelistview_demo;import ...
- UI组件之AdapterView及其子类(六)ExpandableListView组件和ExpandableListActivity的使用
ExpandableListView是ListView的子类,他在ListView上进行了扩展,它把列表项分成了几组,每组里包含了多个列表项 ExpandableListView的列表项是由Expan ...
最新文章
- debian10 nfs简单搭建
- VLAN,trunk,以太网通道
- VC++ .Net 实例学习
- 数据结构与算法基础--错题集
- Hive_ 对比分区,分桶
- 远程服务器传数据库,怎样把本机的mysql数据库上传到远程主机上
- 【BZOJ2095】[Poi2010]Bridges 动态加边网络流
- 转: 参数修饰符ref,out ,params的区别
- 打印预览的时候,总是会多于一个空白页,怎么办?
- python浏览器复制粘贴到word里(带格式的)
- 国产化操作系统安装OpenJDK Icedtea插件
- 可实时配置的错误系统 errcode-errmsg
- 在vue项目中插入视频
- gdal gdal2tiles.py 的使用
- html 注销 按钮,用户登录/注销时隐藏/替换菜单项
- Android BLE4.+ 蓝牙开发国产手机兼容性解决方案
- python预测股票 keras_使用LSTM模型预测股价基于Keras
- html 音乐 QQ播放器 外链 代码 播放器 外链 代码
- “yield”关键字有什么作用?
- 基于51单片机数字电压表的设计 仿真、程序、原理图(转发)