ExpandListView总结

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

from 花葬_小吃一口
小白一位,本文仅一些自己在学习路上的一些总结,请各路大佬觉得不是,或有可补充的地方,请帮忙修正。学生感激不尽(耐心看完哦,代码有几百行,这是我对于可折叠列表学习所有的理解,希望可以帮助到有需要的人。请看本篇文章的伙伴,对于adapter有一定的理解)

在这里插入图片描述

可折叠列表

总有一些客户需求列表可以折叠。一开始能想到的就是定义一个type然后来区分哪一块来显示什么。这里要介绍一下ExpandListView的方便之处啦!!!
如果对可折叠列表有存在疑惑的伙伴可以参考一个QQ的好友列表。可折叠列表允许有父级标签,外加自己子级的内容。其实它就相当于两个listview的嵌套,说白了就是listview扩展来的。

添加内容

认识到可扩展列表后,我们很自然可以想到添加内容进去就需要父级集合和子级集合。

//首先我们需要定义两个存放数据的容器,一个用来存放父级内容,还有一个存放子级内容。这里的父级内容可以参考QQ好友列表的分组,子级内容可以参考分组里面的每一个好友。
private List<String> groupArray;//定义父级列表private List<List<String>> itemArray;//定义子列表private 

认识BaseExpandableListAdapter

想要进行个性化定制页面,那我们就需要先来认识ExpandableListView的adapter 里面有哪些方法和属性。

  @Overridepublic int getGroupCount() {  //这个方法可以得到父级元素的数量return groupArray.;}@Overridepublic int getChildrenCount(int groupPosition) {  //这个方法可以得到子级元素的数量return 0;}@Overridepublic Object getGroup(int groupPosition) {   //这个方法得到第(groupPosition)个父级的内容return groupArray.get(groupPosition);}@Overridepublic Object getChild(int groupPosition, int childPosition) {  //这个方法得到第(groupPosition)父级元素的第(childPosition)个的子级元素。return itemArray.get(groupPosition).get(childPosition);}@Overridepublic long getGroupId(int groupPosition) {     //获取第(groupPostion)个组return groupPostion;}@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;					//获取第(childPosition)个子级元素}@Overridepublic boolean hasStableIds() {		return false;}@Overridepublic View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {  //这个方法就和listview的getview一样,不过这里多了个group,所以这里是进行父级标签的初始化和赋值。GroupHolder groupHolder  = new GroupHolder();if(convertView == null) {convertView = inflater.inflate(R.layout.group_item,null);groupHolder.tvGroup = (TextView) convertView.findViewById(R.id.tvGroup);convertView.setTag(groupHolder);}else{groupHolder = (GroupHolder) convertView.getTag();}Data02 data = (Data02) groupArray.get(groupPosition);groupHolder.tvGroup.setText(data.group_tvgroup+"");return convertView;}@Overridepublic View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {// 这个方法是进行子级标签的初始化和赋值。 ChildHolder childHolder = new ChildHolder();if (convertView == null){convertView = inflater.inflate(R.layout.child_item,null);childHolder.tvItem = (TextView) convertView.findViewById(R.id.tvItem);convertView.setTag(childHolder);}else {childHolder = (ChildHolder) convertView.getTag();}Data03 data = (Data03) itemArray.get(groupPosition).get(childPosition);childHolder.tvItem.setText(data.child_tvItem);return convertView;}@Override    //当选择子节点的时候被调用public boolean isChildSelectable(int groupPosition, int childPosition) {return false;}

这个控件属性基本已经介绍完毕了。

下面这个是我的两个数据类

	public class Data02 {public int group_tvgroup;}class Data03{public  String child_tvItem;}

下面这个是我的两个Holder类

class GroupHolder{   //存放父级元素的Holder类public TextView tvGroup;}class ChildHolder{   //存放子级元素的Holder类public TextView tvItem;}
}
下面的这个是group_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/tvGroup"android:layout_width="200dp"android:layout_height="wrap_content"android:textSize="30dp"/></LinearLayout>
下面的这个是child_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"><ExpandableListViewandroid:id="@+id/elv"android:layout_width="wrap_content"android:layout_height="wrap_content"></ExpandableListView></LinearLayout>
下面的这个是expandablelist_item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"><ExpandableListViewandroid:id="@+id/elv"android:layout_width="wrap_content"android:layout_height="wrap_content"></ExpandableListView></LinearLayout>

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

相关文章

ExpandableListView详细教程

既然大家能到这里来&#xff0c;相信已经对ExpandableListView用了初步的认识&#xff0c;废话不多说&#xff0c;直接给大家上干货。 源程序找不到了&#xff0c;下图是将ExpandableListView放在DrawerLayout(侧边栏)中的效果。 ExpandableListView就是大家平时在PC端见到的二…

ExpandableListActivity(一)

相关文章 ExpandableListView&#xff08;二&#xff09; 参考资料&#xff1a; 1.sakurakider的文章安卓的ExpandableListView的使用和优化 2. sunny.day的文章ExpandableListView(可扩展的listView) 效果图 首先是组布局和组子项布局 group_layout.xml <?xml versio…

ExpandableListView的简单例子

最近一段时间参考网上的例子&#xff0c;做了一下简单的 ExpandableListView&#xff0c;现在和大家共享一下&#xff1a; 1&#xff1a;main.xml的内容 <?xml version"1.0" encoding"utf-8"?><LinearLayout xmlns:android"http://sch…

android-ExpandableList可展开的list

ExpandableList可展开的list 老规矩。右键取得显示不出来的图片地址&#xff0c;利用下载工具下载这个图片。后缀改为rar即可得到源代码项目。 package zhang.EspandableListView;import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.…

android 二级折叠列表,Android折叠列表 ExpandableList

ExpandableList 是折叠列表,通过继承ExpandableListActivity 类就可以非常简单的实现折叠列表。 效果图: 代码实现 package com.zhou.activity; import android.app.ExpandableListActivity; import android.os.Bundle; import android.view.ContextMenu; import android.vie…

ExpandableListView的使用

ExpandableListView的使用 效果图 布局 <ExpandableListViewandroid:id"id/expandableListView"android:layout_width"match_parent"android:layout_height"match_parent" /> 初始化 ExpandableListView expandableListView (Expanda…

ExpandableListView简介

学习笔记&#xff0c;欢迎指导。 最近做了一个项目&#xff0c;需要一个层级列表&#xff0c;完成之后&#xff0c;就顺便来做个博客简介一下。 △基本简介 →“ExpandableListView”是对“ListView”做的扩展&#xff0c;“ListView”是个列表&#xff0c;而“ExpandableLis…

浅析——ExpandableListView的使用

ExpandableListView&#xff08;可扩展的ListView&#xff09; ExpandableListVivew是ListView的子类&#xff0c;它在普通ListView的基础上进行了扩展&#xff0c;它把应用中的列表项分为几组&#xff0c;每组里又可包含多个列表项。ExpandableListVivew的用法与普通ListView的…

ExpandableListView的应用

使用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.…

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…