Android基础教程之---ExpandableListView基本用法
2012-04-02 14:55
405 查看
什么是ExpandableListView,如下图所示:
![](http://my.csdn.net/uploads/201204/02/1333349664_8526.jpg)
展开后的效果:
![](http://my.csdn.net/uploads/201204/02/1333349792_9285.jpg)
创建ExpandableListView的步骤
1.首先在main.xml布局文件中声明ExpandableListView控件:
2.然后在布局文件夹中创建group.xml文件声明group的样式:
3.在布局文件夹中创建child.xml文件声明子项的样式:
4.创建一个Activity,继承ExpandableListView:
5.为group创建数据,也就是一级菜单项。一级菜单有两个菜单项,分别为group1,group2
为child创建数据,也就是二级菜单项。这个二级菜单项属于一级菜单项group1
创建属于group2的二级菜单项
再定义一个list,用来存储所有二级菜单项的数据
6.生成一个SimpleExpandableListAdapter对象
参数注释:
this, 上下文对象
groups, 一级条目的数据
R.layout.group, 用来设置一级条目样式的布局文件
new String[] {"group"}, 指定一级条目数据的key
new int[] {R.id.groupTo}, 指定一级条目数据显示控件的id
childs, 指定二级条目的数据
R.layout.child, 用来设置二级条目样式的布局文件
new String[] {"child"}, 指定二级条目数据的key
new int[] {R.id.childTo} 指定二级条目数据显示控件的id
程序结构图:
![](http://my.csdn.net/uploads/201204/02/1333353666_9751.png)
源码下载地址
摘自:mars。
![](http://my.csdn.net/uploads/201204/02/1333349664_8526.jpg)
展开后的效果:
![](http://my.csdn.net/uploads/201204/02/1333349792_9285.jpg)
创建ExpandableListView的步骤
1.首先在main.xml布局文件中声明ExpandableListView控件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ExpandableListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:drawSelectorOnTop="false"/> <TextView android:id="@id/android:empty" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="No Data" /> </LinearLayout>
2.然后在布局文件夹中创建group.xml文件声明group的样式:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/groupTo" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="60px" android:paddingTop="10px" android:paddingBottom="10px" android:textSize="26sp" android:text="No Data" /> </LinearLayout>
3.在布局文件夹中创建child.xml文件声明子项的样式:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/childTo" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingLeft="50px" android:paddingTop="5px" android:paddingBottom="5px" android:textSize="20sp" android:text="No Data" /> </LinearLayout>
4.创建一个Activity,继承ExpandableListView:
public class MainActivity extends ExpandableListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
5.为group创建数据,也就是一级菜单项。一级菜单有两个菜单项,分别为group1,group2
//定义一个List,该List对象为一级菜单项提供数据 List<Map<String, String>> groups = new ArrayList<Map<String, String>>(); Map<String, String> group1 = new HashMap<String, String>(); group1.put("group", "group1"); Map<String, String> group2 = new HashMap<String, String>(); group2.put("group", "group2"); groups.add(group1); groups.add(group2);
为child创建数据,也就是二级菜单项。这个二级菜单项属于一级菜单项group1
//定义一个List,该List对象为第一个一级菜单项提供二级菜单项数据 List<Map<String, String>> child1 = new ArrayList<Map<String, String>>(); Map<String, String> child1Data1 = new HashMap<String, String>(); child1Data1.put("child", "child1Data1"); child1.add(child1Data1); Map<String, String> child1Data2 = new HashMap<String, String>(); child1Data2.put("child", "child1Data2"); child1.add(child1Data2);
创建属于group2的二级菜单项
//定义一个List,该List对象为第二个一级菜单项提供二级菜单项数据 List<Map<String, String>> child2 = new ArrayList<Map<String, String>>(); Map<String, String> child2Data1 = new HashMap<String, String>(); child2Data1.put("child", "child2Data1"); child2.add(child2Data1); Map<String, String> child2Data2 = new HashMap<String, String>(); child2Data2.put("child", "child2Data2"); child2.add(child2Data2);
再定义一个list,用来存储所有二级菜单项的数据
List<List<Map<String, String>>> childs = new ArrayList<List<Map<String,String>>>(); childs.add(child1); childs.add(child2);
6.生成一个SimpleExpandableListAdapter对象
SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(this, groups, R.layout.group, new String[] {"group"}, new int[] {R.id.groupTo}, childs, R.layout.child, new String[] {"child"}, new int[] {R.id.childTo});
参数注释:
this, 上下文对象
groups, 一级条目的数据
R.layout.group, 用来设置一级条目样式的布局文件
new String[] {"group"}, 指定一级条目数据的key
new int[] {R.id.groupTo}, 指定一级条目数据显示控件的id
childs, 指定二级条目的数据
R.layout.child, 用来设置二级条目样式的布局文件
new String[] {"child"}, 指定二级条目数据的key
new int[] {R.id.childTo} 指定二级条目数据显示控件的id
程序结构图:
![](http://my.csdn.net/uploads/201204/02/1333353666_9751.png)
源码下载地址
摘自:mars。
相关文章推荐
- Android基础入门教程——2.4.12 ExpandableListView(可折叠列表)的基本使用
- Android UI控件----ExpandableListView的基本用法
- Android基础入门教程——7.5.1 WebView(网页视图)基本用法
- Android基础入门教程——7.5.1 WebView(网页视图)基本用法
- Android实战简易教程<五>(ListView用法研究)
- Android初级教程 - ListView的基础入门
- Android基础入门教程——2.4.2 ListView简单使用
- Android中okhttp的基本用法1的基础上进行封装
- Java基础教程16-String的基本用法
- ExpandableListView的一些基本语法以及用法,程序员须知的基本标准
- Android中关于ExpandableListView用法的一个简单小例子
- Android基础入门教程——5.1 Fragment基本概述
- [置顶] Android开发之数据存储——SharedPreferences基础知识详解,饿补学会基本知识,开发者必会它的用法。
- Android ListView控件基本用法 (转载)
- Android ListView控件基本用法
- Android 学习笔记之ExpandableListView UI的简单用法
- android基础学习004_Android listview与adapter用法
- Android开发进阶系列(六) ListView的基本用法
- Android初级教程 - ListView的基础入门
- Android基础学习笔记之-ListView用法(一)