Android中ListView的用法基本上学的时候都会使用,其中可以使用ArrayAdapter,SimpleAdapter,BaseAdapter去实现,这次主要使用的ExpandableListView展示一种两层的效果,ExpandableListView是android中可以实现下拉list的一个控件类似于QQ那种我好友之后就是一排自己的好友,就是两层效果,实现的话使用SimpleExpandableListAdapter即可。

布局文件

先看下效果:

main中xml代码:

  <Button
android:onClick="test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="FlyElephant" /> <ExpandableListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false" />

定义一个省份的province.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/list_provinceText"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="8px"
android:paddingLeft="30px"
android:paddingRight="5px"
android:paddingTop="8px"
android:textSize="20sp" /> </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/child_text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="8px"
android:paddingLeft="30px"
android:paddingRight="5px"
android:paddingTop="8px"
android:textSize="20sp" /> </LinearLayout>

Demo实现

主要实现代码,代码中都已经注释,其中最主要的SimpleExpandableListAdapter中的参数,这个参数太多,很容易弄错,可以看下注释或者API文档:

       // 创建一级条目
List<Map<String, String>> provinces = new ArrayList<Map<String, String>>();
//创建两个省份一级条目
Map<String, String> firstProvince= new HashMap<String, String>();
firstProvince.put("province", "河南");
Map<String, String> secondProvince= new HashMap<String, String>();
secondProvince.put("province", "北京");
provinces.add(firstProvince);
provinces.add(secondProvince);
// 创建一级条目下的的二级地区条目
List<Map<String, String>> childList1= new ArrayList<Map<String, String>>();
//同样是在一级条目目录下创建两个对应的二级条目目录
Map<String, String> child1= new HashMap<String, String>();
child1.put("child", "郑州");
Map<String, String> child2 = new HashMap<String, String>();
child2.put("child", "开封");
childList1.add(child1);
childList1.add(child2);
//同上
List<Map<String, String>> childList2 = new ArrayList<Map<String, String>>();
Map<String, String> child3 = new HashMap<String, String>();
child3.put("child", "海淀");
Map<String, String> child4 = new HashMap<String, String>();
child4.put("child", "昌平");
childList2.add(child3);
childList2.add(child4);
// 将二级条目放在一个集合里,供显示时使用
List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();
childs.add(childList1);
childs.add(childList2);
/**
* 使用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, provinces, R.layout.list_group, new String[] { "province" },
new int[] { R.id.list_groupText }, childs, R.layout.child,
new String[] { "child" }, new int[] { R.id.child_text });
setListAdapter(adapter);

这个mainActivity需要继承ExpandableListActivity,当然你可以设置其中的点击事件,只要重写一下方法即可:

    /**
* 设置哪个二级目录被默认选中
*/
@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);
}

效果如下:

上面这个例子写的有点单调,其实第二个你子的布局直接是空的也行,例如定义一个images.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" > <ImageView
android:src="@drawable/open"
android:layout_width="20dp"
android:layout_height="20dp" /> <TextView
android:id="@+id/txtName"
android:paddingLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout>

然后定义一个items.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/items"
android:layout_width="wrap_content"
android:layout_height="wrap_content" > </TextView>

 代码调用:

public class MyExpandleActivity extends Activity {

	/**
* 实现可扩展展开列ExpandableListView的三种方式
* 一是使用SimpleExpandableListAdpater将两个List集合包装成ExpandableListView 二是
* 扩展BaseExpandableListAdpter
* 三是使用simpleCursorTreeAdapter将Cursor中的数据包装成SimpleCuroTreeAdapter
*/
private String[] names = { "腾讯", "百度", "阿里巴巴" }; private String[][] childnames = { { "马化腾", "张小龙","社交"},
{ "李彦宏", "马东敏","搜索" }, { "马云", "陆兆禧","电商" } };
private ExpandableListView ep; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_expandle); // 定义父列表项List数据集合
List<Map<String, String>> group = new ArrayList<Map<String, String>>();
// 定义子列表项List数据集合
List<List<Map<String, String>>> ss = new ArrayList<List<Map<String, String>>>();
for (int i = 0; i < names.length; i++) {
// 提供父列表的数据
Map<String, String> maps = new HashMap<String, String>();
maps.put("names", names[i]);
group.add(maps);
// 提供当前父列的子列数据
List<Map<String, String>> child = new ArrayList<Map<String, String>>();
for (int j = 0; j < names.length; j++) {
Map<String, String> mapsj = new HashMap<String, String>();
mapsj.put("map", childnames[i][j]);
child.add(mapsj);
}
ss.add(child);
}
/**
* 第一个参数 应用程序接口 this 第二个父列List<?extends Map<String,Object>>集合 为父列提供数据
* 第三个参数 父列显示的组件资源文件 第四个参数 键值列表 父列Map字典的key 第五个要显示的父列组件id 第六个 子列的显示资源文件
* 第七个参数 键值列表的子列Map字典的key 第八个要显示子列的组件id
*/
SimpleExpandableListAdapter expand = new SimpleExpandableListAdapter(
this, group, R.layout.images, new String[] { "names" },
new int[] { R.id.txtName }, ss, R.layout.items,
new String[] { "map" }, new int[] { R.id.items });
ep = (ExpandableListView) findViewById(R.id.expanable_mylist);
ep.setAdapter(expand); } }

  效果跟上面相同:

Android数据适配-ExpandableListView的更多相关文章

  1. Android中使用ExpandableListView实现微信通讯录界面(完善仿微信APP)

    之前的博文<Android中使用ExpandableListView实现好友分组>我简单介绍了使用ExpandableListView实现简单的好友分组功能,今天我们针对之前的所做的仿微信 ...

  2. Android中使用ExpandableListView实现好友分组

    一个视图显示垂直滚动两级列表中的条目.这不同于列表视图,允许两个层次,类似于QQ的好友分组.要实现这个效果的整体思路为: 1.要给ExpandableListView 设置适配器,那么必须先设置数据源 ...

  3. 【收藏】Android屏幕适配全攻略(最权威的Google官方适配指导)

    来源:http://blog.csdn.net/zhaokaiqiang1992 更多:Android AutoLayout全新的适配方式, 堪称适配终结者 Android的屏幕适配一直以来都在折磨着 ...

  4. Android屏幕适配全攻略(最权威的官方适配指导)(转),共大家分享。

    Android的屏幕适配一直以来都在折磨着我们这些开发者,本篇文章以Google的官方文档为基础,全面而深入的讲解了Android屏幕适配的原因.重要概念.解决方案及最佳实践,我相信如果你能认真的学习 ...

  5. Android屏幕适配dp、px两套解决办法

    "又是屏幕适配,这类文章网上不是很多了吗?" 我也很遗憾,确实又是老问题.但本文重点对网上的各种方案做一个简短的总结,和具体使用方法. 若想了解具体Android设备适配的前世因果 ...

  6. Android屏幕适配全攻略(最权威的官方适配指导) (转)

    招聘信息: Cocos2d-X 前端主程 [新浪微博]手机客户端iOS研发工程师 20k-40k iOS 开发工程师 iOS高级开发工程师(中国排名第一的企业级移动互联网云计算公司 和创科技 红圈营销 ...

  7. Android多分辨率适配

    前一阶段开发android项目,由于客户要求进行多分辨率适配,能够支持国内主流的分辨率手机.因此经过了几次开发走了很多弯路,目前刚刚领略了android多分辨率适配的一些方法. 先介绍一下所走的弯路, ...

  8. 【转】Android屏幕适配全攻略(最权威的官方适配指导)

    原文网址:http://blog.csdn.net/jdsjlzx/article/details/45891551 Android的屏幕适配一直以来都在折磨着我们这些开发者,本篇文章以Google的 ...

  9. android开发之ExpandableListView的使用,实现类似QQ好友列表

    由于工作需要,今天简单研究了一下ExpandableListView,做了一个类似QQ列表的Demo,和大家分享一下. 效果图如下: 先来看看主布局文件: <RelativeLayout xml ...

随机推荐

  1. Noip模拟题 Matrix [递推,组合数]

    Matrix 时间限制: 1 Sec  内存限制: 512 MB 题目描述 小 z 的女朋友送给小 z 一个 n × n 的矩阵.但是矩阵实在太大了,小 z 的女朋友拿不动,只能带给他两个长度为 n ...

  2. 洛谷P3521 [POI2011]ROT-Tree Rotation [线段树合并]

    题目传送门 Tree Rotation 题目描述 Byteasar the gardener is growing a rare tree called Rotatus Informatikus. I ...

  3. 美团外卖iOS App冷启动治理

    一.背景 冷启动时长是App性能的重要指标,作为用户体验的第一道“门”,直接决定着用户对App的第一印象.美团外卖iOS客户端从2013年11月开始,历经几十个版本的迭代开发,产品形态不断完善,业务功 ...

  4. (一)预定义宏、__func__、_Pragma、变长参数宏定义以及__VA_ARGS__

    作为第一篇,首先要说一下C++11与C99的兼容性. C++11将 对以下这些C99特性的支持 都纳入新标准中: 1) C99中的预定义宏 2) __func__预定义标识符 3) _Pragma操作 ...

  5. Highmaps网页图表教程之数据标签与标签文本

    Highmaps网页图表教程之数据标签与标签文本 Highmaps数据标签 数据标签用于在地图图表上展现节点对应的数据.数据标签展现数据是静态的,只要节点一加载,数据标签就会出现在节点附近.在High ...

  6. bzoj 1027 floyd求有向图最小环

    结合得好巧妙.... 化简后的问题是: 给你两个点集A,B,求B的一个子集BB,使得BB的凸包包含A的凸包,求BB的最小大小. 先特判答案为1,2的情况,答案为3的情况,我们先构造一个有向图: 对于B ...

  7. Java发送HTTP POST请求示例

    概述: http请求在所有的编程语言中几乎都是支持的,我们常用的两种为:GET,POST请求.一般情况下,发送一个GET请求都很简单,因为参数直接放在请求的URL上,所以,对于PHP这种语言,甚至只需 ...

  8. poj 2623 Sequence Median 堆的灵活运用

    I - Sequence Median Time Limit:1000MS     Memory Limit:1024KB     64bit IO Format:%I64d & %I64u ...

  9. Python学习笔记(一):Python基础学习

    总结的内容: 1.变量的命名 2.脚本的注释 3.运算符 4.用户输入语句 一.变量的命名 1.变量用于引用在程序中可能会变化的值.它们被称为变量是因为它们可能引用存储在内存中的不同的值. 2.变量的 ...

  10. 读书笔记_Effective_C++_条款三十六:绝不重新定义继承而来的non-virtual函数

    这个条款的内容很简单,见下面的示例: class BaseClass { public: void NonVirtualFunction() { cout << "BaseCla ...