ExpandableListView的应用

article/2025/11/4 8:33:07

使用ExpandableListView实现下拉菜单栏

xml布局


1.activity_main.xml

<ExpandableListView xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"android:id="@+id/id_expandableListView"android:indicatorLeft="10dp"android:indicatorRight="40dp"></ExpandableListView>

2.item_child_chapter.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/id_tv_child"android:layout_width="match_parent"android:layout_height="40dp"android:gravity="center_vertical"android:textSize="16sp"></TextView>

3.item_parent_chapter.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:background="#44337dd7"android:layout_height="56dp"android:orientation="horizontal">//这里自己可以找个图片代替<ImageViewandroid:id="@+id/id_indicator_group"android:layout_width="24dp"android:layout_height="24dp"android:layout_gravity="center_vertical"android:background="@drawable/indicator_group"/><TextViewandroid:id="@+id/id_tv_parent"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center_vertical"tools:text="Android"android:textSize="24dp"android:textStyle="bold"></TextView></LinearLayout>

工具类


1.Chapter

public class Chapter {//分组条目private int id;private String name;private List<ChapterItem> chapterItems = new ArrayList<>();public void addChild(ChapterItem chapterItem){chapterItem.setPid(getId());chapterItems.add(chapterItem);}public void addChild(int id,String cname){ChapterItem chapterItem = new ChapterItem(id,cname);chapterItem.setPid(getId());chapterItems.add(chapterItem);}public Chapter() {}public Chapter(int id, String name) {this.id = id;this.name = name;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public List<ChapterItem> getChapterItems() {return chapterItems;}public void setChapterItems(List<ChapterItem> chapterItems) {this.chapterItems = chapterItems;}@Overridepublic String toString() {return "Chapter{" +"id=" + id +", name='" + name + '\'' +", chapterItems=" + chapterItems +'}';}
}

2.ChapterItem

public class ChapterItem {private int id;private String name;private int pid;public ChapterItem() {}public ChapterItem(int id, String name) {this.id = id;this.name = name;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getPid() {return pid;}public void setPid(int pid) {this.pid = pid;}@Overridepublic String toString() {return "ChapterItem{" +"id=" + id +", name='" + name + '\'' +", pid=" + pid +'}';}
}

3.ChapteeLab

//这里是自己传入的数据,可以自己编写,这里做一个大概的
public class ChapteeLab {
//模拟数据[可以按照这个样子来继续写]public static List<Chapter> generateDatas() {List<Chapter> chapters = new ArrayList<>();Chapter root1 = new Chapter(1, "Android");//写值root1.addChild(1, "PullToRefresh");root1.addChild(2, "Android 8.0通知栏解决方案");root1.addChild(4, "Android 与WebView的js交互");root1.addChild(8, "Android UiAutomator 2.0 入门实战");root1.addChild(10, "移动端音频视频入门");//添加chapters.add(root1);return chapters;}
}

ChapteAdapter封装类

public class ChapteAdapter extends BaseExpandableListAdapter {private Context mContext;private List<Chapter> mDatas;private LayoutInflater mInflater;public ChapteAdapter(Context context, List<Chapter> chapters) {mContext = context;mDatas = chapters;mInflater = LayoutInflater.from(context);}@Overridepublic int getGroupCount() {return mDatas.size();}@Overridepublic int getChildrenCount(int groupPosition) {return mDatas.get(groupPosition).getChapterItems().size();}@Overridepublic Object getGroup(int groupPosition) {return mDatas.get(groupPosition);}@Overridepublic Object getChild(int groupPosition, int childPosition) {return mDatas.get(groupPosition).getChapterItems().get(childPosition);}@Overridepublic long getGroupId(int groupPosition) {return groupPosition;}@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}// TODO@Overridepublic boolean hasStableIds() {return false;}@Overridepublic View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {ParentViewHolder vh;if (convertView == null) {// 修改item height即可演示,第二个参数作用convertView = mInflater.inflate(R.layout.item_parent_chapter, parent, false);vh = new ParentViewHolder();vh.tv = convertView.findViewById(R.id.id_tv_parent);vh.iv = convertView.findViewById(R.id.id_indicator_group);convertView.setTag(vh);} else {vh = (ParentViewHolder) convertView.getTag();}vh.tv.setText(mDatas.get(groupPosition).getName());vh.iv.setSelected(isExpanded);return convertView;}@Overridepublic View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {ChildViewHolder vh;if (convertView == null) {convertView = mInflater.inflate(R.layout.item_child_chapter, parent, false);vh = new ChildViewHolder();vh.tv = convertView.findViewById(R.id.id_tv_child);convertView.setTag(vh);} else {vh = (ChildViewHolder) convertView.getTag();}vh.tv.setText(mDatas.get(groupPosition).getChapterItems().get(childPosition).getName());return convertView;}// 控制child item不可点击@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {return true;}public static class ParentViewHolder {TextView tv;ImageView iv;}public static class ChildViewHolder {TextView tv;}
}

主类


1.MainActivity

public class MainActivity extends AppCompatActivity {private ExpandableListView mexpandableListView;private BaseExpandableListAdapter madapter;private List<Chapter> mDatas = new ArrayList<>();private static final String TAG = "xw";private ChapteBiz mchapteBiz = new ChapteBiz();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();initEvents();}private void initEvents() {mexpandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {@Overridepublic boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {Log.d(TAG,"1"+i+"2"+i1);return false;}});mexpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {@Overridepublic boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {Log.d(TAG,"1"+i);return false;}});mexpandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {@Overridepublic void onGroupCollapse(int i) {Log.d(TAG,"1"+i);}});mexpandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {@Overridepublic void onGroupExpand(int i) {Log.d(TAG,"1"+i);}});}private void initView() {mexpandableListView = findViewById(R.id.id_expandableListView);mDatas.clear();mDatas.addAll(ChapteeLab.generateDatas());//设置适配器[封装BaseExpandableListAdapter(){......}]madapter = new ChapteAdapter(this,mDatas);mexpandableListView.setAdapter(madapter);//隐藏'>'mexpandableListView.setGroupIndicator(null);}
}

成果


在这里插入图片描述
在这里插入图片描述
谢谢看到这里,Android萌新,有问题望大佬指出,谢谢!


http://chatgpt.dhexx.cn/article/5QeKjHho.shtml

相关文章

ExpandableListView说明及其用法

一&#xff1a;ExpandableListView ExpandableListView&#xff0c;是可展开的列表组件的ExpandableListView的用法和ListView非常像&#xff0c;只是其所显示的列表项应该由ExpandableListAdapter提供&#xff0c;下面是它的xml属性及说明&#xff1a; childDivider&#xff…

关于ExpandableListView用法的一个简单小例子

喜欢显示好友QQ那样的列表&#xff0c;可以展开&#xff0c;可以收起&#xff0c;在android中&#xff0c;以往用的比较多的是listview&#xff0c;虽然可以实现列表的展示&#xff0c;但在某些情况下&#xff0c;我们还是希望用到可以分组并实现收缩的列表&#xff0c;那就要用…

ExpandableListView扩展(BaseExpandableListAdapter的使用)

针对普通的ExpandableListView的使用&#xff0c;即&#xff0c;需要显示的数据全都是使用TextView显示时&#xff0c;我们使用SimpleExpandableListAdapter就可以了。 但是如果是相对复杂的ExpandableListView&#xff0c;那么SimpleExpandableListAdapter就不满足我们的要求…

ExpandableListView控件的使用

目录 一、ExpandableListView的介绍 二、适配器&#xff08;ExpandableAdapter&#xff09; 1、BaseExpandableListAdapter&#xff1a; BaseExpandableListAdapter例子 一、ExpandableListView的介绍 ExpandableListView是ListView的子类。它是ListView的基础上进行了扩展&…

ExpandableListView的使用详解

在Android开发中&#xff0c;我们知道经常会用到ListView来加载一些列表数据&#xff0c;但有时候ListView并不能完全十分满足我们的需求。比如如下图的效果用 ExpandableListView实现起来就更方便点&#xff0c;我们直接用ExpandableListView&#xff0c;设置Group不能点击即可…

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…