ExpandableListView实例

article/2025/11/4 16:01:39

先来看效果图:
这里写图片描述
demo中有三个group item和多个child item,group item包括一个指示器,一个标题和一个按钮。child item包括一个图片,一个标题和一个按钮。先来实现布局文件
1 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><ExpandableListView
        android:id="@+id/expandlist"android:layout_width="match_parent"android:layout_height="match_parent"android:cacheColorHint="#00000000"android:divider="@android:color/white"android:dividerHeight="1dp" /></RelativeLayout>

group布局,groupitem.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="50dp"android:orientation="horizontal"android:gravity="center_vertical" ><RelativeLayout android:layout_width="match_parent"android:layout_height="50dp"><ImageView android:id="@+id/img_indicator"android:layout_width="32dp"android:layout_height="32dp"android:layout_marginLeft="15dp"android:layout_centerVertical="true"/><TextView android:id="@+id/tv_group_text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:layout_centerVertical="true"android:layout_toRightOf="@id/img_indicator"android:text="zhang san"android:textSize="16sp"/><Button
            android:id="@+id/btn_group_function"android:focusable="false"android:clickable="true"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_alignParentRight="true"android:layout_marginRight="20dp"android:gravity="right"android:background="@drawable/btn_bg_menu" /></RelativeLayout></LinearLayout>

child布局文件childitem.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="50dp"    android:paddingStart="8dp"android:gravity="center_vertical"android:orientation="horizontal"><RelativeLayout android:layout_width="match_parent"android:layout_height="50dp"><ImageView android:id="@+id/img_child"android:layout_width="50dp"android:layout_height="50dp"android:layout_marginLeft="20dp"android:layout_centerVertical="true"/><TextView android:id="@+id/tv_child_text"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:layout_centerVertical="true"android:layout_toRightOf="@id/img_child"android:text="xiangjiao"android:textSize="16sp"/><Button
            android:id="@+id/btn_child_function"android:focusable="false"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_alignParentRight="true"android:layout_marginRight="20dp"android:gravity="right"android:background="@drawable/btn_bg_menu" /></RelativeLayout></LinearLayout>

2 由于每一个child子项中的图片和标题都不一样,因此我们要新建一个Java bean类来描述每一个子项内容
新建ChildItem.java

package com.example.model;public class ChildItem {private String title;//子项显示的文字private int markerImgId;//每个子项的图标public ChildItem(String title, int markerImgId){this.title = title;this.markerImgId = markerImgId;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public int getMarkerImgId() {return markerImgId;}public void setMarkerImgId(int markerImgId) {this.markerImgId = markerImgId;}}

3 如果要将自定义的数据在ExpandableListView上显示出来,我们必须定义一个适配器

package com.example.expandablelistdemo;import java.util.List;
import java.util.Map;import com.example.model.ChildItem;import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;/*** ExpandListView的适配器,继承自BaseExpandableListAdapter**/
public class MyBaseExpandableListAdapter extends BaseExpandableListAdapter implements OnClickListener {private Context mContext;private List<String> groupTitle;//子项是一个map,key是group的id,每一个group对应一个ChildItem的listprivate Map<Integer, List<ChildItem>> childMap;private Button groupButton;//group上的按钮public MyBaseExpandableListAdapter(Context context, List<String> groupTitle, Map<Integer, List<ChildItem>> childMap) {this.mContext = context;this.groupTitle = groupTitle;this.childMap = childMap;}/**  Gets the data associated with the given child within the given group*/@Overridepublic Object getChild(int groupPosition, int childPosition) {//我们这里返回一下每个item的名称,以便单击item时显示return childMap.get(groupPosition).get(childPosition).getTitle();}/*  * 取得给定分组中给定子视图的ID. 该组ID必须在组中是唯一的.必须不同于其他所有ID(分组及子项目的ID)*/@Overridepublic long getChildId(int groupPosition, int childPosition) {      return childPosition;}/* *  Gets a View that displays the data for the given child within the given group*/@Overridepublic View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {ChildHolder childHolder = null;if (convertView == null) {convertView = LayoutInflater.from(mContext).inflate(R.layout.childitem, null);childHolder = new ChildHolder();childHolder.childImg = (ImageView) convertView.findViewById(R.id.img_child);childHolder.childText = (TextView) convertView.findViewById(R.id.tv_child_text);convertView.setTag(childHolder);}else {childHolder = (ChildHolder) convertView.getTag();}childHolder.childImg.setBackgroundResource(childMap.get(groupPosition).get(childPosition).getMarkerImgId());childHolder.childText.setText(childMap.get(groupPosition).get(childPosition).getTitle());return convertView;}/* * 取得指定分组的子元素数*/@Overridepublic int getChildrenCount(int groupPosition) {// TODO Auto-generated method stubreturn childMap.get(groupPosition).size();}/*** 取得与给定分组关联的数据*/@Overridepublic Object getGroup(int groupPosition) {return groupTitle.get(groupPosition);}/*** 取得分组数*/@Overridepublic int getGroupCount() {return groupTitle.size();}/*** 取得指定分组的ID.该组ID必须在组中是唯一的.必须不同于其他所有ID(分组及子项目的ID)*/@Overridepublic long getGroupId(int groupPosition) {return groupPosition;}/* *Gets a View that displays the given group*return: the View corresponding to the group at the specified position */@Overridepublic View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {GroupHolder groupHolder = null;if (convertView == null) {convertView = LayoutInflater.from(mContext).inflate(R.layout.groupitem, null);groupHolder = new GroupHolder();groupHolder.groupImg = (ImageView) convertView.findViewById(R.id.img_indicator);groupHolder.groupText = (TextView) convertView.findViewById(R.id.tv_group_text);convertView.setTag(groupHolder);}else {groupHolder = (GroupHolder) convertView.getTag();}if (isExpanded) {groupHolder.groupImg.setBackgroundResource(R.drawable.downarrow);}else {groupHolder.groupImg.setBackgroundResource(R.drawable.rightarrow);}groupHolder.groupText.setText(groupTitle.get(groupPosition));groupButton = (Button) convertView.findViewById(R.id.btn_group_function);groupButton.setOnClickListener(this);return convertView;}@Overridepublic boolean hasStableIds() {// Indicates whether the child and group IDs are stable across changes to the underlying datareturn true;}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {// Whether the child at the specified position is selectablereturn true;}/*** show the text on the child and group item*/ private class GroupHolder{ImageView groupImg;TextView groupText;}private class ChildHolder{ImageView childImg;TextView childText;}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn_group_function:Log.d("MyBaseExpandableListAdapter", "你点击了group button");           default:break;}}   
}

4 MainActivity.java

package com.example.expandablelistdemo;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import com.example.model.ChildItem;import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ExpandableListView.OnChildClickListener;public class MainActivity extends Activity {private ExpandableListView expandList;private List<String> groupData;//group的数据源private Map<Integer, List<ChildItem>> childData;//child的数据源private MyBaseExpandableListAdapter myAdapter;final int CONTEXT_MENU_GROUP_DELETE = 0;//添加上下文菜单时每一个菜单项的item IDfinal int CONTEXT_MENU_GROUP_RENAME = 1;final int CONTEXT_MENU_CHILD_EDIT = 2;final int CONTEXT_MENU_CHILD_DELETE = 3;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initDatas();initView();initEvents();}/*** group和child子项的数据源*/private void initDatas() {groupData = new ArrayList<String>();groupData.add("红色水果");groupData.add("黄色水果");groupData.add("其他水果");List<ChildItem> childItems = new ArrayList<ChildItem>();ChildItem childData1 = new ChildItem("苹果", R.drawable.apple_pic);childItems.add(childData1);ChildItem childData2 = new ChildItem("樱桃", R.drawable.cherry_pic);childItems.add(childData2);ChildItem childData3 = new ChildItem("草莓", R.drawable.strawberry_pic);childItems.add(childData3);List<ChildItem> childItems2 = new ArrayList<ChildItem>();ChildItem childData4 = new ChildItem("香蕉", R.drawable.banana_pic);childItems2.add(childData4);ChildItem childData5 = new ChildItem("芒果", R.drawable.mango_pic);childItems2.add(childData5);ChildItem childData6 = new ChildItem("橘子", R.drawable.orange_pic);childItems2.add(childData6);ChildItem childData7 = new ChildItem("梨子", R.drawable.pear_pic);childItems2.add(childData7);List<ChildItem> childItems3 = new ArrayList<ChildItem>();ChildItem childData8 = new ChildItem("葡萄", R.drawable.grape_pic);childItems3.add(childData8);ChildItem childData9 = new ChildItem("西瓜", R.drawable.watermelon_pic);childItems3.add(childData9);childData = new HashMap<Integer, List<ChildItem>>();childData.put(0, childItems);childData.put(1, childItems2);childData.put(2, childItems3);myAdapter = new MyBaseExpandableListAdapter(this, groupData, childData);}private void initView() {expandList = (ExpandableListView) findViewById(R.id.expandlist);//在drawable文件夹下新建了indicator.xml,下面这个语句也可以实现group伸展收缩时的indicator变化//expandList.setGroupIndicator(this.getResources().getDrawable(R.drawable.indicator));expandList.setGroupIndicator(null);//这里不显示系统默认的group indicatorexpandList.setAdapter(myAdapter);registerForContextMenu(expandList);//给ExpandListView添加上下文菜单 }private void initEvents() {//child子项的单击事件expandList.setOnChildClickListener(new OnChildClickListener() {@Overridepublic boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) {Toast.makeText(MainActivity.this, "你单击了:"  +myAdapter.getChild(groupPosition, childPosition), Toast.LENGTH_SHORT).show();  return true;}}); }/* * 添加上下文菜单*/@Overridepublic void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {super.onCreateContextMenu(menu, v, menuInfo);ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo)menuInfo;int type = ExpandableListView.getPackedPositionType(info.packedPosition);if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {menu.setHeaderTitle("Options");menu.add(0, CONTEXT_MENU_GROUP_DELETE, 0, "删除");menu.add(0, CONTEXT_MENU_GROUP_RENAME, 0, "重命名");}if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {menu.setHeaderTitle("Options");menu.add(1, CONTEXT_MENU_CHILD_EDIT, 0, "编辑");menu.add(1, CONTEXT_MENU_CHILD_DELETE, 0, "删除");}}/* * 每个菜单项的具体点击事件*/@Overridepublic boolean onContextItemSelected(MenuItem item) {ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo)item.getMenuInfo();switch (item.getItemId()) {case CONTEXT_MENU_GROUP_DELETE:Toast.makeText(this, "这是group的删除", Toast.LENGTH_SHORT).show();break;case CONTEXT_MENU_GROUP_RENAME:Toast.makeText(this, "这是group的重命名", Toast.LENGTH_SHORT).show();break;case CONTEXT_MENU_CHILD_EDIT:Toast.makeText(this, "这是child的编辑", Toast.LENGTH_SHORT).show();break;case CONTEXT_MENU_CHILD_DELETE:Toast.makeText(this, "这是child的删除", Toast.LENGTH_SHORT).show();break;default:break;}   return super.onContextItemSelected(item);}
}

在MainActivity.java中,我们给ExpandableListView添加了上下文菜单,长按group或者child的某一项都能弹出上下文菜单,另外,在group和child中,都添加了一个这里写图片描述这样的按钮,本来是打算,点击group中的此按钮弹出和长按group时弹出一样的上下文菜单,点击child上面的此按钮时也弹出相应的上下文菜单,但是此问题没能解决。就先放在这里了。

代码在这里:http://download.csdn.net/detail/hnyzwtf/9397117


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

相关文章

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…

8大轻型网管工具,网络管理好帮手

从设备发现到系统、网络和流量可视性&#xff0c;这些轻型的网管工具非常实用。在网络和服务器世界&#xff0c;重点是可视性、可视性、可视性&#xff0c;如果你不知道你的网络和服务器在每天每秒正在做什么&#xff0c;你很可能会出问题。幸运的是&#xff0c;这里有很多好工…