ExpandableListView的简单例子

article/2025/11/4 8:26:08

最近一段时间参考网上的例子,做了一下简单的 ExpandableListView,现在和大家共享一下:

1:main.xml的内容

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout   
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"  
android:layout_width="fill_parent"   
android:layout_height="fill_parent"  
androidrientation="vertical"  
>  
<ExpandableListView  
android:id="@+id/expandableListView"  
android:layout_width="fill_parent"  
android:layout_height="wrap_content"  
/>  
</LinearLayout> 

显示一个ExpandableListView


2:ExpandableListViewDemo 类的内容

package com.chama.expandablelistviewdemo;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
public class ExpandableListViewDemo extends Activity {
//定义两个List,用来存放控件中Group/Child中的String
private List<String> groupArray;        
private List<List<String>> childArray;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//对这两个List进行初始化,并插入一些数据
groupArray = new ArrayList<String>();  
childArray = new ArrayList<List<String>>();  
groupArray.add("第一行");  
groupArray.add("第二行");  
List<String> tempArray = new ArrayList<String>();  
tempArray.add("第一条");  
tempArray.add("第二条");  
tempArray.add("第三条");  
for(int index = 0; index <groupArray.size(); ++index)  
{  
childArray.add(tempArray);  
}  
//给定义好的ExpandableListView添加上Adapter
ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.expandableListView);
ExpandableAdapter adapter = new ExpandableAdapter(this);
expandableListView.setAdapter(adapter);
}
//定义ExpandableListView的Adapter
public class ExpandableAdapter extends BaseExpandableListAdapter  
{  
Activity activity;  
public ExpandableAdapter(Activity a)  
{  
activity = a;  
}  
public Object getChild(int groupPosition, int childPosition)  
{  
return childArray.get(groupPosition).get(childPosition);  
} 
public long getChildId(int groupPosition, int childPosition)  
{  
return childPosition;  
} 
public int getChildrenCount(int groupPosition)  
{  
return childArray.get(groupPosition).size();  
} 
public View getChildView(int groupPosition, int childPosition,  
boolean isLastChild, View convertView, ViewGroup parent)  
{  
String string = childArray.get(groupPosition).get(childPosition);  
return getGenericView(string);  
}  
// group method stub  
public Object getGroup(int groupPosition)  
{  
return groupArray.get(groupPosition);  
}  
public int getGroupCount()  
{  
return groupArray.size();  
} 
public long getGroupId(int groupPosition)  
{  
return groupPosition;  
}  
public View getGroupView(int groupPosition, boolean isExpanded,  
View convertView, ViewGroup parent)  
{  
String string = groupArray.get(groupPosition);  
return getGenericView(string);  
}  
// View stub to create Group/Children 's View  
public TextView getGenericView(String string)  
{  
// Layout parameters for the ExpandableListView  
AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(  
ViewGroup.LayoutParams.FILL_PARENT, 64);  
TextView text = new TextView(activity);  
text.setLayoutParams(layoutParams);  
// Center the text vertically  
text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);  
// Set the text starting position  
text.setPadding(36, 0, 0, 0);  
text.setText(string);  
return text;  
}  
public boolean hasStableIds()  
{  
return false;  
}  
public boolean isChildSelectable(int groupPosition, int childPosition)  
{  
return true;  
}  
}  
}

需要注意的内容:

  1:groupArray 和childArray的内容,分别定义父对象和子对象显示的内容

  2:ExpandableAdapter继承了BaseExpandableListAdapter,并重载了其中的一些方法, 注意public TextView getGenericView(String string)的作用,主要用形成显示父对象和子对象视图的类


3:程序效果

 


http://chatgpt.dhexx.cn/article/1kJNItr1.shtml

相关文章

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…

吃完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大…