ExpandableListView的使用详解

article/2025/11/4 16:01:38
在Android开发中,我们知道经常会用到ListView来加载一些列表数据,但有时候ListView并不能完全十分满足我们的需求。比如如下图的效果用 ExpandableListView实现起来就更方便点,我们直接用ExpandableListView,设置Group不能点击即可。好,费话不多说。下面详细介绍 ExpandableListView的使用。

          图(一)
ExpandableListView主要由组与子元素组成。所以我们要分别对组元素以及子元素进行配置及操作。由于ExpandableListView与ListView类似,所以ExpandableListView必不可少的配置过程是必不可少的。首先在XML文件里配置ExpandableListView。
main.xml配置如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity" ><ExpandableListView android:id="@+id/list"android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="#ffffff"android:cacheColorHint="#00000000"android:listSelector="#00000000"></ExpandableListView> 
</RelativeLayout>
group.xml配置如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:orientation="vertical" ><TextViewandroid:id="@+id/group_text"android:layout_width="match_parent"android:layout_height="wrap_content"android:paddingTop="10dip"android:paddingBottom="10dip"android:gravity="center_horizontal"android:text="122"/></LinearLayout>
child.xml配置如下:
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal" ><ImageViewandroid:id="@+id/image"android:layout_width="60dip"android:layout_height="60dip"android:src="@drawable/ic_launcher"/><LinearLayout android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:id="@+id/textOne"android:layout_width="match_parent"android:layout_height="match_parent"android:text="1"/><TextViewandroid:id="@+id/textTwo"android:layout_width="match_parent"android:layout_height="match_parent"android:text="2"/><TextViewandroid:id="@+id/textThree"android:layout_width="match_parent"android:layout_height="match_parent"android:text="3"/></LinearLayout>
</LinearLayout>
 

前面介绍过ExpandableListView与ListView类似,所以ListView   Adapter中存在的方法,ExpandableListView  Adapter必定存在,只是Group和Child分别重写了ListView   Adapter中的方法,同时新增加了两个方法,分别是hasStableIds() 和isChildSelectable(int groupPosition, int childPosition),所以ExpandableListView  Adapter中总共重写了10个方法。
其中要注意hasStableIds() 和isChildSelectable(int groupPosition, int childPosition)这两个方法。hasStableIds() 主要是用来判断ExpandableListView内容id是否有效的(返回true or false),系统会跟据id来确定当前显示哪条内容,也就是firstVisibleChild的位置。而 isChildSelectable(int groupPosition, int childPosition)用来判断某Group某个child是否可可选。我们可以添加条件控制某Group某个child可点或不可点击。当不加任何条件直接返回false,所有的组的child均不可点击。
经过对ExpandableListView Adapter简单的说明,下面我们来看下TestExpandableListViewDemo源码。
其MainActiivty.java如下所示:

<pre name="code" class="java">package com.example.testexpandlistview;import java.util.ArrayList;
import java.util.List;import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;public class MainActivity extends Activity {private ExpandableListView mListView;private List<StrBean> group_list;private List<memBean> child_list;private StrBean strbean;private memBean membean;private LayoutInflater mInflater;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();}private void initView(){mListView = (ExpandableListView) findViewById(R.id.list);mInflater = LayoutInflater.from(MainActivity.this);group_list = new ArrayList<StrBean>();for(int i=0;i<5;i++){strbean =new StrBean();strbean.spacename = "深圳航港"+i;strbean.childsize = 8;child_list = new ArrayList<memBean>();for(int j=0;j<8;j++){membean = new memBean();membean.price=1+j+"万";membean.secondPrice="1.6万";membean.flag = i;membean.title = "商务卡";child_list.add(membean);}strbean.setList(child_list); group_list.add(strbean);}Adapter adapter = new Adapter();mListView.setGroupIndicator(null);/*** ExpandableListView的组监听事件*/mListView.setOnGroupClickListener(new OnGroupClickListener() {@Overridepublic boolean onGroupClick(ExpandableListView parent, View v,int groupPosition, long id) {Toast.makeText(MainActivity.this, "第"+groupPosition+"组被点击了", 0).show();return true;}});/*** ExpandableListView的组展开监听*/mListView.setOnGroupExpandListener(new OnGroupExpandListener() {@Overridepublic void onGroupExpand(int groupPosition) {Toast.makeText(MainActivity.this, "第"+groupPosition+"组展开", 0).show();}});/*** ExpandableListView的组合拢监听*/mListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {@Overridepublic void onGroupCollapse(int groupPosition) {Toast.makeText(MainActivity.this, "第"+groupPosition+"组合拢", 0).show();}});/*** ExpandableListView的子元素点击监听*/mListView.setOnChildClickListener(new OnChildClickListener() {@Overridepublic boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) {Toast.makeText(MainActivity.this, "第"+groupPosition+"组的第"+childPosition+"被点击了", 0).show();return true;}});mListView.setAdapter(adapter);
                 //设置Group默认展开int groupCount = mListView.getCount();for(int i=0;i<groupCount;i++){mListView.expandGroup(i);}}//自定义适配器class Adapter extends BaseExpandableListAdapter{//获取子元素对象@Overridepublic Object getChild(int groupPosition, int childPosition) {return null;}//获取子元素Id@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}//加载子元素并显示@Overridepublic View getChildView(final int groupPosition, final int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {View view=null;ChildHolder childholder = null;if(convertView!=null){view = convertView;childholder = (ChildHolder) view.getTag();}else{view = View.inflate(MainActivity.this,R.layout.item, null);childholder = new ChildHolder();childholder.mImage = (ImageView) view.findViewById(R.id.image);childholder.mPrice = (TextView) view.findViewById(R.id.textTwo);childholder.mStateText = (TextView) view.findViewById(R.id.textOne);childholder.mSecondPrice = (TextView) view.findViewById(R.id.textThree);view.setTag(childholder);}childholder.mImage.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this, "第"+groupPosition+"组的第"+childPosition+"圖標被点击了", 0).show();}});childholder.mPrice.setText(group_list.get(groupPosition).getList().get(childPosition).price);int len = group_list.get(groupPosition).getList().size();System.out.println(len+"-----------------");childholder.mStateText.setText(group_list.get(groupPosition).getList().get(childPosition).title);childholder.mSecondPrice.setText(group_list.get(groupPosition).getList().get(childPosition).secondPrice);return view;}//获取子元素数目@Overridepublic int getChildrenCount(int groupPosition) {return group_list.get(groupPosition).childsize;}//获取组元素对象@Overridepublic Object getGroup(int groupPosition) {return group_list.get(groupPosition);}//获取组元素数目@Overridepublic int getGroupCount() {return group_list.size();}//获取组元素Id@Overridepublic long getGroupId(int groupPosition) {return groupPosition;}//加载并显示组元素@Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {View view=null;GroupHolder groupholder = null;if(convertView!=null){view = convertView;groupholder = (GroupHolder) view.getTag();}else{view = View.inflate(MainActivity.this,R.layout.textview, null);groupholder =new GroupHolder();groupholder.mSpaceText = (TextView) view.findViewById(R.id.group_text);view.setTag(groupholder);}groupholder.mSpaceText.setText(group_list.get(groupPosition).spacename);return view;}@Overridepublic boolean hasStableIds() {return true;}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {return false;}}static class GroupHolder{TextView mSpaceText;}static class ChildHolder{ImageView mImage;TextView mStateText;TextView mPrice;TextView mSecondPrice;}
}
 

               效果图

在上面的源码中,总结说来ExpandableListView主要有setOnGroupClickListener, setOnGroupExpandListener,
setOnGroupCollapseListener,setOnChildClickListener这四个监听事件,分别表示组元素点击事件,组元素展开,组收缩,子元素点击事件。我们可按需添加这四个监听事件即可。由此看来ExpandableListView是多么的简单。看似简单,但我们也要注意其中的一些细节。我相信你听过细节决定成败。下面我们就来一起看看细节。

1. 当设置setOnGroupClickListener监听并让其返回true时,所有Group消费点击事件,事件均不能分发传递给child(换言之,设置setOnChildClickListener不起任何作用)。

2.默认设置完Group以及child,Group左边会默认有以上下切换的图标,假如你有强迫症可以通过mListView.setGroupIndicator(null)去除。

3.前面已经简单说明了isChildSelectable(int groupPosition, int childPosition)方法的作用,所以当我们需要child可点击时,必须将setOnGroupClickListener和isChildSelectable对应设置为false和true。

4.说到这里也许读者会问,到底还是没有说出图一的做法。现在对图一进行详细的介绍。图一主要是将Group设置为不能收缩并且使其默认展开(即设置setOnGroupClickListener返回true,并且添加源码中setAdapter后三行代码)。

以上就是本人对ExpandableListView的一些浅薄之见,今天也是鄙人第一次写博客。都说Android技术博大精深,可能我阐述的只是冰山一角,希望大家踊跃的给出批评与建议。本人也希望能与热爱技术的开发人员进行深入的学习与交流。






http://chatgpt.dhexx.cn/article/eMJEbsg6.shtml

相关文章

ExpandableListView实例

先来看效果图&#xff1a; demo中有三个group item和多个child item&#xff0c;group item包括一个指示器&#xff0c;一个标题和一个按钮。child item包括一个图片&#xff0c;一个标题和一个按钮。先来实现布局文件 1 activity_main.xml <?xml version"1.0&qu…

ExpandableListView使用方法详解

一、前言 “好记性不如烂笔头”&#xff0c;再次验证了这句话是真的很有道理啊&#xff0c;一个月前看了一下ExpandableListView的使用&#xff0c;今天再看居然忘了这个是干啥的了&#xff0c;今天就详细讲解一下ExpandableListView的使用方法&#xff0c;感觉对于二级条目显示…

ExpandableList的使用

首先&#xff0c;我们把一二级选择的对应的类写好。 看这些代码&#xff0c;最主要的是我在ParentStrings中写了一个List<ChildrenStrings>的一个方法&#xff0c;以便之后ChildrenStrings的存储和调用 下面是BusAdapter继承BaseExpandableAdapter public class BusAda…

可折叠列表ExpandableList

ExpandableList就是可展开的ListView 首先我们来看一下页面的布局 expandlist_layout.xml文件 <RelativeLayoutxmlns:android"http://schemas.android.com/apk/res/android"xmlns:app"http://schemas.android.com/apk/res-auto"xmlns:tools"htt…

Adapter类控件使用之ExpandableList(可折叠式列表)的基本使用

(一)概述 本节要讲解的Adapter类控件是ExpandableListView,就是可折叠的列表,它是 ListView的子类, 在ListView的基础上它把应用中的列表项分为几组,每组里又可包含多个列表项。至于 样子, 类似于QQ联系人列表,他的用法与ListView非常相似,只是ExpandableListViv…

如果在临汾遇见你

哈 如果在临汾遇见你&#xff0c;那么&#xff0c;我们一定要去平阳南街煤化巷口的宾库西餐厅&#xff0c;坐在只需消费15块钱就可以坐一整天的大厅散座上&#xff0c;聊着我们相见恨晚的话题。 如果在临汾遇见你&#xff0c;那么&#xff0c;我们一定要去传说的尧庙&#xff0…

吃完7家互联网大厂食堂,我回去就把老板开了

本文转载自 超人测评 不会还有人不知道&#xff08;中文&#xff09;字节跳动&#xff08;英文&#xff09;Bytedance的工牌&#xff0c;已经成为年轻人的社交货币了吧。 有人说它是“相亲市场的硬通货”&#xff0c;也有人将它称为“2021年最潮流时尚单品”。每当你在奶茶店…

围剿宏光MINI

NEW 关注Tech逆向思维视频号 最新视频→【做核酸&#xff1f;打疫苗&#xff1f;3分钟假期安全出行攻略】 出品&#xff5c;深途 文&#xff5c;黎明 编辑&#xff5c; 魏佳 有很长一段时间&#xff0c;汽车企业的老板和投资人&#xff0c;都热衷于造“大车”。蔚来、小鹏、威马…

《植物大战僵尸》的12个成功秘诀

口述 / James Gwertzman 整理 / 杨东杰 [caption id"attachment_6675" align"alignleft" width"263" caption"James Gwertzman PopCap亚太区总裁"] [/caption] 4月28日&#xff0c;在由长城会和CSDN联合主办的“开发者星球——TUP大…

只要10分钟,搭建属于个人的炫酷网站,你还在犹豫什么?

&#x1f482; 个人主页: IT学习日记&#x1f91f; 版权: 本文由【IT学习日记】原创、在CSDN首发、需要转载请联系博主&#x1f4ac; 如果文章对你有帮助、欢迎关注、点赞、收藏(一键三连)和订阅专栏哦&#x1f485; 想寻找共同成长的小伙伴&#xff0c;请点击【技术圈子】 文章…

我的股票投资原则:专注业绩好、看得懂的消费品行业

本文概要&#xff1a;文以载道。总结了过往的经验教训&#xff0c;明确了未来的投资方向和股票投资圈。 我是一名Java程序员&#xff0c;今年心情有点烦躁&#xff0c;没怎么有耐心写工作之外的代码。 同时&#xff0c;我也是一名业余投资人&#xff0c;所以今年就又花了大量…

最近回了趟家,随便拍的照片

先贴下家对面的东山。呵呵。看上去光秃秃的。不过我看上去只有很深的亲切。 家里盖被子的东西 我大哥房间里面别人送的&#xff0d;&#xff0d;&#xff0d;堆绣  是我们湟中的一种农民搞的艺术品 家里的酸奶。  呵呵&#xff0c;大家很多人都记得西宁街头的那种勺子挖着…

破解网址_中国目前的破解组织大全

中国目前的破解组织大全&#xff08;2009版&#xff09;——[TFW]find31作品 管理提醒&#xff1a; 本帖被 朕很伤心 从 『 风云AD区 』 移动到本区(2009-06-23) 破解组织前管理员告诉你中国目前的破解组织现状——看了某个N年前的帖子而发&#xff0c;说实话我真的不忍心再看…

推荐 10 个不错的网络监视工具

点击上方“民工哥技术之路”选择“置顶或星标” 每天10点为你分享不一样的干货 有几个网络监视工具可以用于不同的操作系统。在这篇文章中&#xff0c;我们将讨论从 Linux 终端中运行的 10 个网络监视工具。 它对不使用 GUI 而希望通过 SSH 来保持对网络管理的用户来说是非常理…

【Java】Socket网络编程实现内网穿透、端口映射转发、内网穿透上网工具的编写,设置IP白名单防火墙

这里写目录标题 简介更新 一、背景1.1 情景假设1.2 想要达到的目的1.3 局限1.3 解决方案一&#xff08;路由器NAT&#xff09;1.4 解决方案二&#xff08;云服务器转发&#xff09; 二、方案介绍2.1 方案简介2.2 具体流程2.3 编程要点2.4 关于web管理IP白名单的更新2.5 关于soc…

计算机网络--Windows网络测试工具

实验目的 理解上述知识点所涉及的基本概念并学会使用这些工具测试网络的状态及从网上获取信息。 实验环境 安装了TCP/IP协议的Windows系统&#xff0c;包含实用的网络工具。 实验内容 完成下列要求&#xff0c;并记录实验步骤和结果 1、 检测本机的MAC地址 2、 检测本机网…

【小工具的制作】制作一个通过HTTP请求来实现上网认证的自动登录小工具

目录 1. 前言2. 分析3. 编码3.1 Python版3.1.1 编写Python脚本3.1.2 下载Python转EXE程序工具3.1.3 打包成EXE程序 3.2 Java版3.2.1 编写代码 4. 最后一步5. 总结 1. 前言 由于学校机房联网时&#xff0c;总是需要登录个人账号。为实现快速登录&#xff0c;我们就此问题给出了自…

常见的网络抓包工具推荐

因为发现好多人想抓包&#xff0c;但是不知道有哪些工具&#xff0c;今天我给大家推荐几款抓包工具&#xff0c;希望对大家有所帮助。 网络抓包工具的用途 网络抓包工具的主要功能是将网络执行的过程&#xff0c;详细的记录下来。如果你是一个程序员&#xff0c;肯定对网络抓…

【转】Fiddler抓包工具手机添加代理后连不上网解决办法

转载&#xff1a;Fiddler抓包工具手机添加代理后连不上网解决办法_数据结构和算法的博客-CSDN博客 最近&#xff0c;在工作中需要测试一个监控网络请求的SDK&#xff0c;需要校验该SDK记录的耗时数据的准确性。根据网上大神们提供的工作经验&#xff0c;可以利用Fiddler工具给…

超好用的网络小工具

整理了一下一些自己使用的网络链路状态测试工具。 一、RAWCAP-本地回路抓包 本地使用wireshark无法抓回路包&#xff0c;为此百度了一下RawCap可以抓回路包&#xff0c;亲测可用。特记录下来。 缺点&#xff1a;有一些系统需要安装Microsoft .NET Framework 4 https://www.m…