Android控件之ExpandableListView

本文介绍了一个使用Android的ExpandableListView组件展示分组数据的具体实例。通过创建不同层级的数据结构,并利用SimpleExpandableListAdapter适配器进行绑定,实现了带有分组和子项的数据展示。文章详细展示了如何设置布局文件及实现数据的加载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

layout/main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ExpandableListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:drawSelectorOnTop="false"> </ExpandableListView> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@id/android:empty" android:text="No Data" /> </LinearLayout>


layout/childs.xml

<?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"> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/child" android:textSize="15sp" android:paddingLeft="25px" android:paddingTop="10px" android:paddingRight="5px" android:paddingBottom="10px" android:text="No Data" /> </LinearLayout>


layout/groups.xml

<?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"> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/group" android:textSize="25sp" android:paddingLeft="35px" android:paddingTop="10px" android:paddingRight="5px" android:paddingBottom="10px" android:text="No Data" /> </LinearLayout>

ExpandableListViewActivity.java

package com.zeph.android.expandablelistview.example; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.ExpandableListActivity; import android.os.Bundle; import android.view.View; import android.widget.ExpandableListView; import android.widget.SimpleExpandableListAdapter; public class ExpandableListViewActivity extends ExpandableListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); List<Map<String, String>> groups = new ArrayList<Map<String, String>>(); // 创建两个一级条目标题 Map<String, String> group1 = new HashMap<String, String>(); group1.put("group", "音乐"); Map<String, String> group2 = new HashMap<String, String>(); group2.put("group", "歌词"); groups.add(group1); groups.add(group2); // 创建一级条目下的的二级条目 List<Map<String, String>> child1 = new ArrayList<Map<String, String>>(); // 同样是在一级条目目录下创建两个对应的二级条目目录 Map<String, String> childdata1 = new HashMap<String, String>(); childdata1.put("child", "青花瓷"); Map<String, String> childdata2 = new HashMap<String, String>(); childdata2.put("child", "东风破"); child1.add(childdata1); child1.add(childdata2); // 同上 List<Map<String, String>> child2 = new ArrayList<Map<String, String>>(); Map<String, String> childdata3 = new HashMap<String, String>(); childdata3.put("child", "青花瓷.lrc"); Map<String, String> childdata4 = new HashMap<String, String>(); childdata4.put("child", "东风破.lrc"); child2.add(childdata3); child2.add(childdata4); // 将二级条目放在一个集合里,供显示时使用 List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>(); childs.add(child1); childs.add(child2); /** * 使用SimpleExpandableListAdapter显示ExpandableListView 参数1.上下文对象Context * 参数2.一级条目目录集合 参数3.一级条目对应的布局文件 参数4.fromto,就是map中的key,指定要显示的对象 * 参数5.与参数4对应,指定要显示在groups中的id 参数6.二级条目目录集合 参数7.二级条目对应的布局文件 * 参数8.fromto,就是map中的key,指定要显示的对象 参数9.与参数8对应,指定要显示在childs中的id */ SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter( this, groups, R.layout.groups, new String[] { "group" }, new int[] { R.id.group }, childs, R.layout.childs, new String[] { "child" }, new int[] { R.id.child }); setListAdapter(adapter); } /** * 设置哪个二级目录被默认选中 */ @Override public boolean setSelectedChild(int groupPosition, int childPosition, boolean shouldExpandGroup) { // do something return super.setSelectedChild(groupPosition, childPosition, shouldExpandGroup); } /** * 设置哪个一级目录被默认选中 */ @Override public void setSelectedGroup(int groupPosition) { // do something super.setSelectedGroup(groupPosition); } /** * 当二级条目被点击时响应 */ @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { // do something return super.onChildClick(parent, v, groupPosition, childPosition, id); } }

转载自:http://android.tgbus.com/Android/tutorial/201110/373238.shtml

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值