先来看下什么是ExpandableListView

跟列表有点像,这种是可以折叠的列表,下面来看下是如何在代码中实现

一、在布局文件中声明一个ExpandableListView

<LinearLayout 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"
tools:context=".MainActivity" > <ExpandableListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false" >
</ExpandableListView> <TextView
android:id="@id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_data" /> </LinearLayout>

二、为每个Group和Child定义一个布局

group.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/groupTo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="50dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textSize="20sp"
android:text="@string/no_data"/>
</LinearLayout>

child.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/childTo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="60dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textSize="20sp"
android:text="@string/no_data"/>
</LinearLayout>

以上两个文件只有缩进不一样,这样可以明显区分Group和Child

三、在代码中实现

package com.example.expandableList;

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.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.SimpleExpandableListAdapter;
import android.widget.Toast; import com.example.widgetdemo.R; public class expandableList extends ExpandableListActivity {
ExpandableListView expandableList = null; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.expandable); expandableList = getExpandableListView(); // 写入当前组数
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", "深圳市");
Map<String, String> childData3 = new HashMap<String, String>();
childData3.put("child", "东莞市");
child1.add(childData1);
child1.add(childData2);
child1.add(childData3);
// 写入第二组的数据
List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();
Map<String, String> childData4 = new HashMap<String, String>();
childData4.put("child", "赣州市");
Map<String, String> childData5 = new HashMap<String, String>();
childData5.put("child", "南昌市");
Map<String, String> childData6 = new HashMap<String, String>();
childData6.put("child", "九江市");
Map<String, String> childData7 = new HashMap<String, String>();
childData7.put("child", "吉安市");
child2.add(childData4);
child2.add(childData5);
child2.add(childData6);
child2.add(childData7); List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();
childs.add(child1);
childs.add(child2); SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(
expandableList.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 });
setListAdapter(sela);
expandableList.setOnChildClickListener(new expandableListListener());
} //为ExpandableListView编写监听器
class expandableListListener implements OnChildClickListener { @Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
System.out.println("group = " + groupPosition + " child = "
+ childPosition + " id = " + id); ExpandableListAdapter exAdapter = parent.getExpandableListAdapter(); // 获得选中项的HashMap对象
Map<String, String> childMap = (HashMap<String, String>) exAdapter
.getChild(groupPosition, childPosition);
String contentChild = childMap.get("child");
Map<String, String> groupMap = (HashMap<String, String>) exAdapter
.getGroup(groupPosition);
String contentGroup = groupMap.get("group");
Toast.makeText(
getApplicationContext(),
"你选择了第" + groupPosition + " 个Group, 第 " + childPosition
+ " 个Child的值是:" + contentChild + " Group的值是 " + contentGroup,
Toast.LENGTH_LONG).show();
return false;
} }
}

关于如何获取选中的值,我查看文档终于找到ExpandableListAdapter,在这个适配器有获取那个child和group的方法。

下面看下效果

最后还是传上源码

点击打开链接

Android常用控件之ExpandableList的使用的更多相关文章

  1. Android常用控件及对应Robotium API

    最近发现Android控件不熟悉,看Robotium的API都费劲. 常用Android控件: 控件类型 描述 相关类 Button 按钮,可以被用户按下或点击,以执行⼀个动作 Button Text ...

  2. 常用的基本控件 android常用控件

    1.TextView:(文本框):不能编辑    android:textColor="@color/tv_show_color" 字体颜色    android:textSize ...

  3. Android常用控件

     Android 中使用各种控件(View) DatePicker - 日期选择控件 TimePicker - 时间选择控件 ToggleButton - 双状态按钮控件 EditText - 可编辑 ...

  4. Android常用控件之GridView使用BaseAdapter

    我们可以为GridView添加自定义的Adapter,首先看下用自定义Adapter的显示效果 在布局文件main.xml文件中定义一个GridView控件 <RelativeLayout xm ...

  5. Android常用控件之RatingBar的使用

    RatingBar控件比较常见就是用来做评分控件,先上图看看什么是RatingBar 在布局文件中声明 <?xml version="1.0" encoding=" ...

  6. android常用控件的使用方法

    引言 xml很强大 TextView <TextView android:id="@+id/text_view" android:layout_width="mat ...

  7. Android 常用控件的介绍

    http://www.cnblogs.com/linjiqin/category/284058.html 最流行的android组件大全:http://www.cnblogs.com/linjiqin ...

  8. Android常用控件之GridView与ExpandableListView的用法

    概述 1.GridView:与ListView相比,可以显示多列,xml布局时其属性numColumns可以设置显示的列数. 2.ExpandableListView:与ListView相比,可以让每 ...

  9. Android常用控件之FragmentTabHost的使用

    最近在学TabHost时发现TabActivity在API level 13以后不用了,所以就去寻找它的替换类,找到FragmentActivity,可以把每个Fragment作为子tab添加到Fra ...

随机推荐

  1. Javascript 匀速运动——应用案例:网站常用功能分享到

    网站上会经常用到Javascript 中的运动,这次与大家分享一下一些运动的基本应用 . 方便大家在开发中能够直接使用. 代码简单易懂,适用于初学者.最后会一步一步整理出一套自己的运动框架. 应用案例 ...

  2. C++中的引用到底是什么

    这也算是一个老生常谈的问题,写这个其实就是想趁着暑假把博客丰富一下. 咱随便在谷哥.度娘.病软引擎上搜搜都可以得到各种关于引用的解释,无非就是"引用不同于指针,引用是一个变量的别名" ...

  3. linux搭建php

    http://www.cnblogs.com/wubaiqing/archive/2011/11/08/2241195.html 四,启动php-fpm & Nginx /usr/local/ ...

  4. BZOJ 1143 [CTSC2008]祭祀river(二分图匹配)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1143 [题目大意] 给出一张有向图,问最大不连通点集,连通具有传递性 [题解] 我们将 ...

  5. 第三届蓝桥杯预赛真题_第一题_两种微生物 X 和 Y

    /* 假设有两种微生物 X 和 Y X出生后每隔3分钟分裂一次(数目加倍),Y出生后每隔2分钟分裂一次(数目加倍). 一个新出生的X,半分钟之后吃掉1个Y,并且,从此开始,每隔1分钟吃1个Y. 现在已 ...

  6. uestc 10 In Galgame We Trust

    题意:求最长的合法括号序列 解:栈+分类讨论 now表示已经算出的序列,且此序列与现在扫描的序列可能能够连接,tmp表示现在扫描到的序列长度 左括号入栈 右括号:1.栈空时:统计当前总长 并且将栈,n ...

  7. [LeetCode][Python]Median of Two Sorted Arrays

    # -*- coding: utf8 -*-'''https://oj.leetcode.com/problems/median-of-two-sorted-arrays/ There are two ...

  8. 去英国Savile Row 做件私人定制手工西装_GQ男士网

    去英国Savile Row 做件私人定制手工西装_GQ男士网 去英国Savile Row 做件私人定制手工西装

  9. 编写可维护的JS 03

    3.语句和表达式 所有语句都应当使用花括号 if else语句 for 循环 while 循环 do...while try...catch...finally 花括号对齐方式 左括号在第一行语句末尾 ...

  10. 自学JavaScript的几个例子

    学习了广泛使用的浏览器脚本JavaScript和HTML的DOM模型(也是用JavaScript实现),下面给出两个自己学习时的例子,具体JavaScript语法请参考W3C相关网页(http://w ...