Android ExpandableListActivity的简单介绍及小例子
Android中常常要用到ListView,但也经常要用到ExpandableListView,ListView是显示列表,而ExpandableListView显示的是分类的列表;
下面用一个例子来说明:



还可以点击触发事件;


代码如下:
activity_main.xml
<RelativeLayout 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="com.xiaozhang.listactivitytest.MainActivity" >
<ExpandableListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false" />
</RelativeLayout>
第一层列表group.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="4dp"
android:paddingLeft="12dp"
android:paddingRight="12dp" >
<TextView
android:id="@+id/groupTo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:paddingLeft="30dp"
android:paddingTop="10dp"
android:shadowColor="#40000000"
android:shadowDx="0"
android:shadowDy="8"
android:shadowRadius="1"
android:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>
第二层列表child.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="4dp"
android:paddingLeft="12dp"
android:paddingRight="12dp" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="@drawable/icon"
android:paddingTop="12dp" />
<TextView
android:id="@+id/childTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingLeft="60dp"
android:paddingTop="10dp"
android:textSize="16sp" />
</RelativeLayout>
MainActivity.java
package com.xiaozhang.listactivitytest;
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;
import android.widget.Toast;
public class MainActivity extends ExpandableListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Map<String, String>> groups = new ArrayList<Map<String, String>>();
// 第一层列表数据
Map<String, String> group1 = new HashMap<String, String>();
Map<String, String> group2 = new HashMap<String, String>();
Map<String, String> group3 = new HashMap<String, String>();
Map<String, String> group4 = new HashMap<String, String>();
group1.put("group", "湘北高中");
group2.put("group", "岭南高中");
group3.put("group", "翔阳高中");
group4.put("group", "海南高中");
groups.add(group1);
groups.add(group2);
groups.add(group3);
groups.add(group4);
// 第二层列表数据
List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();
// 第二层列表的第一个子列表数据
List<Map<String, String>> child1 = new ArrayList<Map<String, String>>();
Map<String, String> child1Data1 = new HashMap<String, String>();
child1Data1.put("child", "樱木花道");
Map<String, String> child1Data2 = new HashMap<String, String>();
child1Data2.put("child", "流川枫");
Map<String, String> child1Data3 = new HashMap<String, String>();
child1Data3.put("child", "宫城良田");
Map<String, String> child1Data4 = new HashMap<String, String>();
child1Data4.put("child", "三井寿");
child1.add(child1Data1);
child1.add(child1Data2);
child1.add(child1Data3);
child1.add(child1Data4);
// 第二层列表的第二个子列表数据
List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();
Map<String, String> child2Data1 = new HashMap<String, String>();
child2Data1.put("child", "仙道彰");
Map<String, String> child2Data2 = new HashMap<String, String>();
child2Data2.put("child", "鱼住纯");
Map<String, String> child2Data3 = new HashMap<String, String>();
child2Data3.put("child", "福田吉兆");
child2.add(child2Data1);
child2.add(child2Data2);
child2.add(child2Data3);
// 第二层列表的第三个子列表数据
List<Map<String, String>> child3 = new ArrayList<Map<String, String>>();
Map<String, String> child3Data1 = new HashMap<String, String>();
child3Data1.put("child", "藤真健司");
Map<String, String> child3Data2 = new HashMap<String, String>();
child3Data2.put("child", "花形透");
Map<String, String> child3Data3 = new HashMap<String, String>();
child3Data3.put("child", "长谷川一志");
child3.add(child3Data1);
child3.add(child3Data2);
child3.add(child3Data3);
// 第二层列表的第四个子列表数据
List<Map<String, String>> child4 = new ArrayList<Map<String, String>>();
Map<String, String> child4Data1 = new HashMap<String, String>();
child4Data1.put("child", "牧绅一");
Map<String, String> child4Data2 = new HashMap<String, String>();
child4Data2.put("child", "神宗一郎");
Map<String, String> child4Data3 = new HashMap<String, String>();
child4Data3.put("child", "清田信长");
Map<String, String> child4Data4 = new HashMap<String, String>();
child4Data4.put("child", "高砂一马");
child4.add(child4Data1);
child4.add(child4Data2);
child4.add(child4Data3);
child4.add(child4Data4);
// 把子列表数据放入第二层列表中
childs.add(child1);
childs.add(child2);
childs.add(child3);
childs.add(child4);
SimpleExpandableListAdapter listAdapter = 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 });
setListAdapter(listAdapter);
}
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(
this,
parent.getItemAtPosition(groupPosition)
+ " "
+ parent.getItemAtPosition(childPosition
+ groupPosition + 1), Toast.LENGTH_LONG).show();
return super.onChildClick(parent, v, groupPosition, childPosition, id);
}
}
注意:
(1)@id/android:list,是系统自带的ID,如果要使用ListActivity或我们使用的ExpandableListActivity,就必须要使用@id/android:list;
而ListActivity会根据id自动查找ListView的引用;如在 Activity 中使用 setListAdapter(adapter) 时就默认设置到了这个list上。如果按一般控件的写法 <ListView android:id="@+id/myListView" …… />,则需要 findViewById 先得到控件对像,再调用对像的 setListAdapter(adapter);
Android ExpandableListActivity的简单介绍及小例子的更多相关文章
- jetty 介绍以及小例子
Jetty 是一个开源的servlet容器,它为基于Java的web容器,例如JSP和servlet提供运行环境.Jetty是使用Java语言编写的,它的API以一组JAR包的形式发布.开发人员可以将 ...
- 【unity3d游戏开发之基础篇】unity3d射线的原理用法以及一个利用射线实现简单拾取的小例子
原地址:http://www.cnblogs.com/xuling/archive/2013/03/04/2943154.html 最近开始研究U3D,它的强大就不多说了, 今天研究了研究射线相关东西 ...
- 浅析微信支付:微信支付简单介绍(小程序、公众号、App、H5)
本文是[浅析微信支付]系列文章的第二篇,主要讲解一下普通商户接入的支付方式以及其中的不同之处. 上篇文章讲了本系列的大纲,没有看过的朋友们可以看一下. 浅析微信支付:前篇大纲 微信支付是集成在微信客户 ...
- javascript开发 ios和android app的简单介绍
先看几个名词解释: nodejs ionic,Cordova,phoneGap,anjularjs react-native,reactjs nodeJs 的介绍参见这里,写的很好http://www ...
- Android Service使用简单介绍
作为一个android初学者,经常对service的使用感到困惑.今天结合Google API 对Service这四大组件之一,进行简单使用说明. 希望对和我一样的初学者有帮助,如有不对的地方,也希望 ...
- android MVP模式简单介绍
原文 http://zhengxiaopeng.com/2015/02/06/Android%E4%B8%AD%E7%9A%84MVP/ 前言 MVP作为一种MVC的演化版本在Android开发中受到 ...
- Android 使用 Application 简单介绍
Application 配置全局Context 第一步.写一个全局的单例模式的MyApplication继承自Application 覆盖onCreate ,在这个方法里面实例化Application ...
- android handler机制简单介绍
我们需要了解4个类: handler:处理者,用于发送和接收信息 massage:消息.里面可以存储消息的许多信息 looper:循环泵,用于循环取出消息队列中的消息 MessageQueue(一般不 ...
- android之PackageManager简单介绍
PackageManager相关 本类API是对全部基于载入信息的数据结构的封装,包含下面功能: 安装,卸载应用查询permission相关信息 查询Application相关信息(applicati ...
随机推荐
- java笔记2之算术运算符
1运算符是什么呢 对常量和变量进行操作的运算符 2运算符分为哪些 算术运算符(+,-,*,/), 赋值运算符 比较运算符 逻辑运算符 位运算符 三目运算符 3运算符 A 算术运算符的注意事项 (1)整 ...
- memcached学习——大纲简介 && 安装(基于centos6.5)、启动、关闭memcached(一)
大纲简介 安装前,先简单介绍一下memcached. memcached是一个免费.开源.高性能的分布式缓存.设计memcached的初衷是为了加快web应用程序,减少DB负载. 安装要求:支持大多数 ...
- Python进阶(面向对象编程基础)(一)
鉴于昨天被类和函数折腾得晕头转向,今特把类的知识翻出来温习. 1.定义类并创建实力对象 #!/usr/bin/env python # -*- coding:utf-8 -*- __author__ ...
- 远程连接mysql,mysql如何开启远程连接
很多时候,mysql只需要开本地连接,也就是本机(服务器本身)连接就可以,默认也是这样,默认也不支持远程连接 但有的时候,我们需要将mysql独立出一台主机或数据库,放到另一台机器的时候,这时,就需要 ...
- Lance老师UI系列教程第八课->新浪新闻SlidingMenu界面的实现
UI系列教程第八课:Lance老师UI系列教程第八课->新浪新闻SlidingMenu界面的实现 今天蓝老师要讲的是关于新浪新闻侧滑界面的实现.先看看原图: 如图所示,这种侧滑效果以另一种方式替 ...
- windows下php+apache+mysql环境搭建
在Windows 7下进行PHP环境搭建,首先需要下载PHP代码包和Apache与Mysql的安装软件包. PHP版本:php-5.3.2-Win32-VC6-x86,VC9是专门为IIS定制的,VC ...
- cocos2d-x v3.2 FlappyBird 各个类对象详细代码分析(7)
今天我们介绍最后两个类 GameOverLayer类 GameLayer类 GameLayer类是整个游戏中最重要的类,由于是整个游戏的中央系统,控制着各个类(层)之间的交互,这个类中实现了猪脚小鸟和 ...
- rpm-bin
bin:二进制可执行程序,与windows的exe文件一样,在linux图形界面可直接双击运行,或在终端界面使用该命令执行 ./filename 有的软件是二进制安装程序和源代码一起发布,二进制程序文 ...
- Set Windows IP by Batch
netsh interface ip set address name="Local" static 192.168.1.55 255.255.255.0 192.168.1.1 ...
- 1:环境安装与介绍:canopy
<利用python进行数据分析>这本书推荐用的的环境为EPDFree版本,但实际现在大概已经抛弃它改用Canopy了,下面将介绍Canopy相关: 一:下载:https://store.e ...