文章目录

  • 效果图
  • ExpandableListView的简介与使用
  • 去掉ExpandableListView的箭头以及自定义Indicator
  • 解决setOnChildClickListener失效问题
  • 解决collapseGroup(i)崩溃问题
  • 解决group_item.xml中包含CheckBox、EditText等,点击不能展开的问题

1.效果图


2.ExpandableListView的简介与使用
ExpandableListVivew是ListView的子类,它在普通ListView的基础上进行了扩展,它把应用中的列表项分为几组,每组里 又可包含多个列表项。ExpandableListVivew的用法与普通ListView的用法非常相似,只是ExpandableListVivew 显示的列表项应该由ExpandableAdapter提供。

  • 重要方法
//在分组列表中展开指定组(若想全部展开,循环分组列表再调用此方法)
expandGroup (int groupPos) ;
//在分组列表中关闭指定组(若想全部展开,循环分组列表再调用此方法)
collapseGroup(int groupPos);
//设置选择指定的组
setSelectedGroup (int groupPosition) ;
//设置选择指定的子项
setSelectedChild (int groupPosition, int childPosition, boolean shouldExpandGroup);
//返回所选择的组
getPackedPositionGroup (long packedPosition);
//返回所选择的子项
getPackedPositionForChild (int groupPosition, int childPosition) ;
//返回所选择项的类型(Child/Group)
getPackedPositionType (long packedPosition);
//判断此组是否展开
isGroupExpanded (int groupPosition);
  • 使用步骤

1.在布局文件中引用ExpandableListView

<ExpandableListView
        android:id="@+id/listview"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_margin="5dp"android:childDivider="#cccccc"android:divider="#cccccc"android:dividerHeight="1px"/>

2.初始化与装配数据

public class MainActivity extends AppCompatActivity {private ExpandableListView listview;private AMapAdapter adapter;//先创建,防止NullPointExcaptionprivate ArrayList<Group> groups = new ArrayList<>();String[] groupStrs = {"广东省","湖南省","湖北省","江苏省","云南省","海南省"};String[] guangdong = {"深圳市","广州市","佛山市","东莞市","韶关市"};String[] hunan = {"长沙市","株洲市","衡阳市","湘潭市","益阳市"};String[] hubei = {"武汉市","襄阳市","荆州市","黄冈市","仙桃市"};String[] jiangsu = {"南京市","苏州市","泰州市","无锡市","徐州市"};String[] yunan = {"昆明市","普洱市","曲靖市","玉溪市","丽江市"};String[] hainan = {"海口市","东方市","琼海市","三亚市","文昌市"};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initview();initdata();}/** 初始化ExpandableListView等视图*/private void initview(){listview = (ExpandableListView) findViewById(R.id.listview);listview.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {@Overridepublic boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {if (adapter != null){Group group = adapter.getGroup(i);Toast.makeText(MainActivity.this,"点击了"+group.getGroupName(),Toast.LENGTH_SHORT).show();}return false;}});listview.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {@Overridepublic boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long l) {if (adapter != null){Child child = adapter.getChild(groupPosition, childPosition);Toast.makeText(MainActivity.this,"点击了"+child.getChildName(),Toast.LENGTH_SHORT).show();}return false;}});}/** 初始化数据-网络请求数据*/private void initdata(){//若是http请求,最好等数据回调成功后再装配数据(回调函数是主线程可直接装配,分线程可通过Handler再装配)//demo中数据均是伪造for (String groupName : groupStrs){ArrayList<Child> childs = new ArrayList<>();Group group = null;if (groupName.equals("广东省")){for (String name : guangdong){Child child = new Child(name);childs.add(child);}group = new Group(groupName,childs);}else if (groupName.equals("湖南省")){for (String name : hunan){Child child = new Child(name);childs.add(child);}group = new Group(groupName,childs);}else if (groupName.equals("湖北省")){for (String name : hubei){Child child = new Child(name);childs.add(child);}group = new Group(groupName,childs);}else if (groupName.equals("江苏省")){for (String name : jiangsu){Child child = new Child(name);childs.add(child);}group = new Group(groupName,childs);}else if (groupName.equals("云南省")){for (String name : yunan){Child child = new Child(name);childs.add(child);}group = new Group(groupName,childs);}else if (groupName.equals("海南省")){for (String name : hainan){Child child = new Child(name);childs.add(child);}group = new Group(groupName,childs);}groups.add(group);}if (groups.size() > 0){if (adapter == null){adapter = new AMapAdapter(this,groups);listview.setAdapter(adapter);}}}
}

3.继承BaseExpandableListAdapter(推荐使用)或实现ExpandableListAdapter

public class AMapAdapter extends BaseExpandableListAdapter {private Context mContext;private ArrayList<Group> mGroups;public AMapAdapter(){}public AMapAdapter(Context context, ArrayList<Group> groups){mContext = context;mGroups = groups;}@Overridepublic int getGroupCount() {return mGroups.size();}@Overridepublic int getChildrenCount(int i) {return mGroups.get(i).getChildList().size();}@Overridepublic Group getGroup(int i) {return mGroups.get(i);}@Overridepublic Child getChild(int groupPosition, int childPosition) {return mGroups.get(groupPosition).getChildList().get(childPosition);}@Overridepublic long getGroupId(int i) {return 0;}@Overridepublic long getChildId(int groupPosition, int childPosition) {return 0;}@Overridepublic boolean hasStableIds() {return false;}@Overridepublic View getGroupView(int i, boolean isExpand, View contentView, ViewGroup viewGroup) {GroupViewHolder groupViewHolder = null;if (contentView == null){contentView = View.inflate(mContext, R.layout.group_item,null);groupViewHolder = new GroupViewHolder();groupViewHolder.gName = (TextView) contentView.findViewById(R.id.group_name);contentView.setTag(groupViewHolder);}else{groupViewHolder = (GroupViewHolder) contentView.getTag();}Group group = getGroup(i);groupViewHolder.gName.setText(group.getGroupName());return contentView;}@Overridepublic View getChildView(int groupId, int childId, boolean isLastChild, View contentView, ViewGroup viewGroup) {ChildViewHolder childViewHolder = null;if (contentView == null){contentView = View.inflate(mContext,R.layout.child_item,null);childViewHolder = new ChildViewHolder();childViewHolder.childName = (TextView) contentView.findViewById(R.id.child_name);contentView.setTag(childViewHolder);}else{childViewHolder = (ChildViewHolder) contentView.getTag();}Child child = getChild(groupId, childId);childViewHolder.childName.setText(child.getChildName());return contentView;}@Overridepublic boolean isChildSelectable(int i, int i1) {//若为false,会导致setOnChildClickListener失效return true;}class GroupViewHolder{public TextView gName;}class ChildViewHolder{public TextView childName;}
}

以上的方法跟ListView相似,这里就不累赘了
demo下载:
http://download.csdn.net/detail/lzp2015/9793392


3.去掉ExpandableListView的箭头
在ExpandableListView标签中添加如下属性,即可

android:groupIndicator="@null"

listSelector属性是设置点击Item的颜色

android:listSelector="@drawable/custom_listview_seletor"

如不需要显示右侧滚动条,设置scrollbars属性为none

android:scrollbars="none"

如需自定义grouIndicator

//1.去掉自带的groupIndicator
android:groupIndicator="@null"
//2.在group_item.xml中添加ImagView(根据自己需求去规划)
//3.在getGroupView()方法中添加图片
@Overridepublic View getGroupView(int i, boolean isExpand, View contentView, ViewGroup viewGroup) {if (isExpand){groupViewHolder.expandTag.setBackgroundResource(R.mipmap.list_view_indicate_down);}else{groupViewHolder.expandTag.setBackgroundResource(R.mipmap.list_view_indicate_right);}return contentView;}

4.解决setOnChildClickListener失效问题

将Adapter中isChildSelectable返回值改为true,若还没解决,可能是你childitem中某个视图一直占用了焦点而没释放

//是否允许childItem被选中
@Overridepublic boolean isChildSelectable(int i, int i1) {//若为false,会导致setOnChildClickListener失效return true;}

5.解决collapseGroup(i)崩溃问题
应用场景:
当点击某一个Group时,关闭之前打开的Group
错误代码

结果是出现ANR,并抛出java.lang.IndexOutOfBoundsException

这是ExpandabeListView的bug,解决办法:

listview.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {@Overridepublic void onGroupExpand(int groupPosition) {if (oldPostion == -1){oldPostion = groupPosition;}else{listview.collapseGroup(oldPostion);oldPostion = groupPosition;}}
});

6.解决group_item.xml中包含CheckBox、EditText等,点击不能展开的问题
出现此类现象,原因是因为焦点被CheckBox、EditText获取导致的。
解决办法:
在item_item.xml(根据自己的xml文件)找到对应的视图(CheckBox、EditText等),设置其焦点初始值为false,完美解决

android:focusable="false"

ExpandableListView详解相关推荐

  1. android 之ExpandableListView详解

    ExpandableListView是一种可应用于某种环境的下拉列表. 实例代码: package com.example.lenovo.expandablelistview_demo;import ...

  2. 从命令行到IDE,版本管理工具Git详解(远程仓库创建+命令行讲解+IDEA集成使用)

    首先,Git已经并不只是GitHub,而是所有基于Git的平台,只要在你的电脑上面下载了Git,你就可以通过Git去管理"基于Git的平台"上的代码,常用的平台有GitHub.Gi ...

  3. JVM年轻代,老年代,永久代详解​​​​​​​

    秉承不重复造轮子的原则,查看印象笔记分享连接↓↓↓↓ 传送门:JVM年轻代,老年代,永久代详解 速读摘要 最近被问到了这个问题,解释的不是很清晰,有一些概念略微模糊,在此进行整理和记录,分享给大家.在 ...

  4. docker常用命令详解

    docker常用命令详解 本文只记录docker命令在大部分情境下的使用,如果想了解每一个选项的细节,请参考官方文档,这里只作为自己以后的备忘记录下来. 根据自己的理解,总的来说分为以下几种: Doc ...

  5. 通俗易懂word2vec详解词嵌入-深度学习

    https://blog.csdn.net/just_so_so_fnc/article/details/103304995 skip-gram 原理没看完 https://blog.csdn.net ...

  6. 深度学习优化函数详解(5)-- Nesterov accelerated gradient (NAG) 优化算法

    深度学习优化函数详解系列目录 深度学习优化函数详解(0)– 线性回归问题 深度学习优化函数详解(1)– Gradient Descent 梯度下降法 深度学习优化函数详解(2)– SGD 随机梯度下降 ...

  7. CUDA之nvidia-smi命令详解---gpu

    nvidia-smi是用来查看GPU使用情况的.我常用这个命令判断哪几块GPU空闲,但是最近的GPU使用状态让我很困惑,于是把nvidia-smi命令显示的GPU使用表中各个内容的具体含义解释一下. ...

  8. Bert代码详解(一)重点详细

    这是bert的pytorch版本(与tensorflow一样的,这个更简单些,这个看懂了,tf也能看懂),地址:https://github.com/huggingface/pytorch-pretr ...

  9. CRF(条件随机场)与Viterbi(维特比)算法原理详解

    摘自:https://mp.weixin.qq.com/s/GXbFxlExDtjtQe-OPwfokA https://www.cnblogs.com/zhibei/p/9391014.html C ...

最新文章

  1. 【每天一个linux命令】read
  2. semget创建文件_linux信号灯操作
  3. 【Nginx】错误: [emerg] “proxy_set_header“ directive is not allowed here in D:\sde\phpstudy_pro\...
  4. Hadoop的伪分布式安装
  5. 智能视频内容生产中专业视频数据导出工具的研发
  6. Windows Azure Marketplace增加对六种语言和HTML5应用程序的支持
  7. 753 Cracking the Safe
  8. 1-8:学习shell之高级键盘技巧
  9. 图解算法之排序算法(5)——归并排序
  10. SVN和Maven及Jenkins(转)
  11. 计算机控制的液压提升,一种液压提升监控系统及其在液压提升控制上的运用
  12. SEGGER_RTT
  13. Maya2018生成pyd文件
  14. 经典企业文化书籍推荐,有了这6本书企业文化落地不再是难事
  15. 小白开始学RTOS 1
  16. vue-element-admin 框架结构粗解
  17. Microarchitecture: HyperThreading(超线程)
  18. mix2s android p,待遇堪比“亲儿子” 小米MIX 2s迎来Android P升级
  19. 新西兰.net和java_使用Linux容器分析气候变化和土壤对新西兰农作物的影响
  20. breakpoint

热门文章

  1. acm新手小白必看系列之(1)——二维数组及结构体精讲附带例题
  2. 如何在华为应用市场上架新应用
  3. chome(谷歌浏览器)上传文件崩溃/上传图片崩溃/打开浏览文件未响应 解决方案
  4. busybox配置telnetd
  5. JVM 工作原理和流程
  6. 公司计算机系统忘记用户密码怎么办,win10系统重置修改已经忘记的登录密码的解决步骤...
  7. 天涯各版回复过10000帖子大汇总(2006.9月3日更新版)
  8. 使用 Visual Studio 2022 开发 Linux C++ 应用程序
  9. 攻防世界 supersqli
  10. 为什么探测任何IP的25和110端口都能通?