浅析——ExpandableListView的使用

article/2025/11/4 8:36:28

ExpandableListView(可扩展的ListView)

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

实现ExpandableAdapter的三种方式 

 一是扩展BaseExpandableListAdpter实现ExpandableAdapter。

 二是使用SimpleExpandableListAdpater将两个List集合包装成ExpandableAdapter

 三是使用simpleCursorTreeAdapter将Cursor中的数据包装成SimpleCuroTreeAdapter

下图为ExpandableListVivew支持的xml属性

XML Attributes

属性名

Related Method

描述

 

 

 

 

 

 

 

 

 

android:childDivider

指定各组内子类表项之间的分隔条 

 

 

 

 

 

 

 

 

 

android:childIndicator

显示在子列表旁边的Drawable对象

 

 

 

 

 

 

 

 

 

android:childIndicatorLeft

子列表项指示符的左边约束位置

 

 

 

 

 

 

 

 

 

android:childIndicatorRight

子列表项指示符的右边约束位置

 

 

 

 

 

 

 

 

 

android:groupIndicator

显示在组列表旁边的Drawable对象 

 

 

 

 

 

 

 

 

 

android:indicatorLeft

组列表项指示器的左边约束位置 

 

 

 

 

 

 

 

 

 

android:indicatorRight

组列表项指示器的右边约束位置 

 

 

 

 

 

 

 

 

 

备注:

①   注:图片不会完全显示,分离子列表项的是一条直线

②   注:可以是一个图片

③ 注:即从左端0位置开始计数,比如,假设指示符是一个图标,给定这个属性值为

3dip,则表示从左端起3dip开始显示此图标。

④   注:表示右端到什么位置结束

⑤   注:可以是一个图片。

⑥   注:表示左端从什么位置开始。

⑦ 注:表示右端到什么位置结束。

 

       一般适用于ExpandableListViewAdapter都要继承BaseExpandableListAdapter这个类,并且必须重载getGroupViewgetChildView这两个最为重要的方法。

      当扩展BaseExpandableListAdapter时,关键是实现如下四个方法:

public abstract ViewgetChildView (int groupPosition, intchildPosition, boolean isLastChild, ViewconvertView, ViewGroup parent)

取得显示给定分组给定子位置的数据用的视图.

参数

groupPosition 包含要取得子视图的分组位置.

childPosition   分组中子视图(要返回的视图)的位置.

isLastChild     该视图是否为组中的最后一个视图.

convertView   如果可能,重用旧的视图对象.使用前你应该保证视图对象为非空,并且是否是合适的类型.如果该对象不能转换为可以正确显示数据的视图,该方法就创建新视图.不保证使用先前由getChildView(int, int,boolean, View, ViewGroup)创建的视图.

parent     该视图最终从属的父视图.

返回

指定位置相应的子视图.

public abstract intgetChildrenCount (int groupPosition)

取得指定分组的子元素数.

参数

groupPosition 要取得子元素个数的分组位置.

返回

指定分组的子元素个数.

public abstract ViewgetGroupView (int groupPosition, booleanisExpanded, View convertView, ViewGroupparent)

取得用于显示给定分组的视图.这个方法仅返回分组的视图对象,要想获取子元素的视图对象,就需要调用getChildView(int, int, boolean, View, ViewGroup).

参数

groupPosition 决定返回哪个视图的组位置.

isExpanded     该组是展开状态还是收起状态 .

convertView   如果可能,重用旧的视图对象.使用前你应该保证视图对象为非空,并且是否是合适的类型.如果该对象不能转换为可以正确显示数据的视图,该方法就创建新视图.不保证使用先前由getGroupView(int, boolean,View, ViewGroup)创建的视图.

parent     该视图最终从属的父视图.

返回

指定位置相应的组视图.

public abstract intgetGroupCount ()

取得分组数.

返回

分组数.

BaseExpandableListAdapter的重载的其它方法如下:

public abstract Object getChild(int groupPosition, int childPosition)

取得与指定分组、指定子项目关联的数据.

参数

groupPosition 包含子视图的分组的位置.

childPosition   指定的分组中的子视图的位置.

返回

与子视图关联的数据.

public abstract long getChildId(int groupPosition, intchildPosition)

取得给定分组中给定子视图的ID.该组ID必须在组中是唯一的.必须不同于其他所有ID(分组及子项目的ID.

参数

groupPosition 包含子视图的分组的位置.

childPosition   要取得ID的指定的分组中的子视图的位置.

返回

与子视图关联的ID.

public abstract longgetCombinedChildId (long groupId, long childId)

取得一览中可以唯一识别子条目的ID(包括分组ID和子条目ID.可扩展列表要求每个条目(分组条目和子条目)具有一个可以唯一识别列表中子条目和分组条目的ID.该方法根据给定子条目ID和分组条目ID返回唯一识别ID.另外,如果hasStableIds()为真,该函数返回的ID必须是固定不变的.

参数

groupId   包含子条目ID的分组条目ID.

childId    子条目的ID.

返回

可以在所有分组条目和子条目中唯一识别该子条目的ID(可能是固定不变的).

public abstract longgetCombinedGroupId (long groupId)

取得一览中可以唯一识别子条目的ID(包括分组ID和子条目ID.可扩展列表要求每个条目(分组条目和子条目)具有一个可以唯一识别列表中子条目和分组条目的ID.该方法根据给定子条目ID和分组条目ID返回唯一识别ID.另外,如果hasStableIds()为真,该函数返回的ID必须是固定不变的.

参数

groupId   分组条目ID.

返回

可以在所有分组条目和子条目中唯一识别该分组条目的ID(可能是固定不变的).

public abstract Object getGroup(int groupPosition)

取得与给定分组关联的数据.

参数

groupPosition 分组的位置.

返回

指定分组的数据.

public abstract long getGroupId(int groupPosition)

取得指定分组的ID.该组ID必须在组中是唯一的.必须不同于其他所有ID(分组及子项目的ID.

参数

groupPosition 要取得ID的分组位置.

返回

与分组关联的ID.

public abstract booleanhasStableIds ()

是否指定分组视图及其子视图的ID对应的后台数据改变也会保持该ID.

返回

是否相同的ID总是指向同一个对象.

public abstract booleanisChildSelectable (int groupPosition, intchildPosition)

指定位置的子视图是否可选择.

参数

groupPosition 包含要取得子视图的分组位置.

childPosition   分组中子视图的位置.

返回

是否子视图可选择.

注意:

XML布局文件中,如果ExpandableListView上一级视图的大小没有严格定义的话,则不能对ExpandableListViewandroid:layout_height属性使用wrap_content值。(例如,如果上一级视图是ScrollView的话,则不应该指定wrap_content的值,因为它可以是任意的长度。不过,如果ExpandableListView的上一级视图有特定的大小的话,比如100像素,则可以使用wrap_content

应用实例:

运行效果图:


项目结构图:


package com.jph.expandablelistviewdemo;import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
/*** Describe:</br>* 可扩展的ListView</br>* 本实例主要通过扩展BaseExpandableListAdapter来实现ExpandableListAdapter</br>* 并通过ExpandableListAdapter为ExpandableListView设置数据适配器</br>* 另外,本实例为ExpandableListView的子列表单击事件设置监听器* * @author jph* Date:2014.07.14* */
public class ExpandableListViewDemo extends Activity {ExpandableListView list;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_expandable_list_view);list=(ExpandableListView)findViewById(R.id.list);//创建一个BaseExpandableListAdapter对象final ExpandableListAdapter adapter=new BaseExpandableListAdapter() {//设置组视图的图片int[] logos = new int[] { R.drawable.js, R.drawable.mfzw,R.drawable.yczw};//设置组视图的显示文字private String[] category = new String[] { "僵尸	", "魔法植物", "远程植物" };//子视图显示文字private String[][] subcategory = new String[][] {{"旗帜僵尸", "铠甲僵尸", "书生见识", "铁桶僵尸", "尸娃僵尸","舞蹈僵尸" },{ "黄金蘑菇", "贪睡蘑菇", "大头蘑菇", "诱惑植物", "多嘴蘑菇","七彩蘑菇" },{ "满天星", "风车植物", "带刺植物", "贪睡植物","双子植物","胆怯蘑菇" }};//子视图图片public int[][] sublogos = new int[][] {{ R.drawable.js_1,R.drawable.js_2,R.drawable.js_3,R.drawable.js_4,R.drawable.js_5,R.drawable.js_6},{ R.drawable.mfzw_1,R.drawable.mfzw_2,R.drawable.mfzw_3,R.drawable.mfzw_4,R.drawable.mfzw_5,R.drawable.mfzw_6},{ R.drawable.yczw_1,R.drawable.yczw_2,R.drawable.yczw_3,R.drawable.yczw_4,R.drawable.yczw_5,R.drawable.yczw_6 } };//定义一个显示文字信息的方法TextView getTextView(){AbsListView.LayoutParams lp=new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,64);TextView textView=new TextView(ExpandableListViewDemo.this);//设置 textView控件的布局textView.setLayoutParams(lp);//设置该textView中的内容相对于textView的位置textView.setGravity(Gravity.CENTER_VERTICAL);//设置txtView的内边距textView.setPadding(36, 0, 0, 0);//设置文本颜色textView.setTextColor(Color.BLACK);return textView;           	}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn true;}@Overridepublic boolean hasStableIds() {// TODO Auto-generated method stubreturn true;}//取得用于显示给定分组的视图. 这个方法仅返回分组的视图对象@Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {// TODO Auto-generated method stub//定义一个LinearLayout用于存放ImageView、TextViewLinearLayout ll=new LinearLayout(ExpandableListViewDemo.this);//设置子控件的显示方式为水平ll.setOrientation(0);//定义一个ImageView用于显示列表图片ImageView logo=new ImageView(ExpandableListViewDemo.this);logo.setPadding(50, 0, 0, 0);//设置logo的大小(50(padding)+46=96)AbsListView.LayoutParams lparParams=new AbsListView.LayoutParams(96,46);logo.setLayoutParams(lparParams);logo.setImageResource(logos[groupPosition]);ll.addView(logo);TextView textView=getTextView();textView.setTextSize(20);textView.setText(category[groupPosition]);ll.addView(textView);return ll;}//取得指定分组的ID.该组ID必须在组中是唯一的.必须不同于其他所有ID(分组及子项目的ID).@Overridepublic long getGroupId(int groupPosition) {// TODO Auto-generated method stubreturn groupPosition;}//取得分组数@Overridepublic int getGroupCount() {// TODO Auto-generated method stubreturn category.length;}//取得与给定分组关联的数据@Overridepublic Object getGroup(int groupPosition) {// TODO Auto-generated method stubreturn category[groupPosition];}//取得指定分组的子元素数.@Overridepublic int getChildrenCount(int groupPosition) {// TODO Auto-generated method stubreturn subcategory[groupPosition].length;}//取得显示给定分组给定子位置的数据用的视图@Overridepublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {// TODO Auto-generated method stub//定义一个LinearLayout用于存放ImageView、TextViewLinearLayout ll=new LinearLayout(ExpandableListViewDemo.this);//设置子控件的显示方式为水平ll.setOrientation(0);//定义一个ImageView用于显示列表图片ImageView logo=new ImageView(ExpandableListViewDemo.this);logo.setPadding(0, 0, 0, 0);//设置logo的大小LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(40, 40);logo.setLayoutParams(lp);logo.setImageResource(sublogos[groupPosition][childPosition]);ll.addView(logo);TextView textView=getTextView();textView.setText(subcategory[groupPosition][childPosition]);ll.addView(textView);return ll;}//取得给定分组中给定子视图的ID. 该组ID必须在组中是唯一的.必须不同于其他所有ID(分组及子项目的ID).@Overridepublic long getChildId(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn childPosition;}@Overridepublic Object getChild(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn subcategory[groupPosition][childPosition];}};list.setAdapter(adapter);		//为ExpandableListView的子列表单击事件设置监听器list.setOnChildClickListener(new OnChildClickListener() {			@Overridepublic boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) {// TODO Auto-generated method stubToast.makeText(ExpandableListViewDemo.this, "你单击了:"+adapter.getChild(groupPosition, childPosition), Toast.LENGTH_LONG).show();return true;}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.expandable_list_view, menu);return true;}}

布局文件:

<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"><ExpandableListView android:id="@+id/list"android:layout_width="match_parent"android:layout_height="wrap_content"></ExpandableListView>a
</RelativeLayout>


 

 

 


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

相关文章

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…

吃完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 来保持对网络管理的用户来说是非常理…