ExpandableView的使用可以绑定到SimpleExpandableListAdapter,主要是看这个Adapter怎么用。 这个类默认的构造函数有9个参数, 很好地解释了什么叫做又臭又长。

public SimpleExpandableListAdapter (Context context, List<? extends Map<String, ?>> groupData, int groupLayout, String[] groupFrom, int[] groupTo, List<? extends List<? extends Map<String, ?>>> childData, int childLayout, String[] childFrom, int[] childTo)

不过不用担心, 只要把Group开头的和Child开头的分成两组来看就好。 具体看下面注释部分。

可以采用对groupData和childData进行操作后再调用 ArrayAdapter.notifyDataSetChanged()方法来通知UI进行更新。

另外,还有一种操作group和child的方法是使用SimpleExpandableListAdapter的 getGroupgetChild方法。


代码:


public class MainActivity extends AppCompatActivity {

    /* 用四个group*数据来表示Group的表现方式,用四个child*的数据来表示Child的表示方式。
groupData,其中的每一个Map代表了一个Group的所有要显示的数据,比如在GroupTab上你想显示name,type,description,那么就在Map里面用键值对放这几项就好了。
groupLayout,只是一个视图,其中要包含有所有用于显示name,type,description等项目的元素。这些元素需要被放在groupTo里面。并且顺序要和groupFrom里面的key顺序对应。
groupFrom,要显示的Group数据的key。
groupTo,要显示的Group数据对应的View的ID。这些ID必须是之前定义的group布局里面的。 Child基本上类似,也是每一个Map对应一个Child的所有数据项。第一层List,分到不同的Group,第二层List,分到不同的ChildItem,第三层就是Map了,每个Child的数据集合。
**/ //定义Group相关变量
ArrayList<Map<String, String>> groupData = new ArrayList<>();
int groupLayout = R.layout.group_view_group;
String[] groupFrom = {"name", "type", "description"};
int[] groupTo = {R.id.txt_view_name, R.id.txt_view_type, R.id.txt_view_description};
//定义Child相关变量
ArrayList<ArrayList<Map<String, String>>> childData = new ArrayList<>();
int childLayout = R.layout.group_view_child;
String[] childFrom = {"ability"};
int[] childTo = {R.id.txt_view_child_ability};
//定义Adapter
SimpleExpandableListAdapter mArrayAdapter; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExpandableListView list = (ExpandableListView) findViewById(R.id.expand_list); for (int i = 0; i < 3; i++) {
//初始化group数据
HashMap<String, String> map = new HashMap<>();
map.put("name", "device " + String.valueOf(i));
map.put("type", "type " + String.valueOf(i));
map.put("description", "Description of device " + String.valueOf(i));
groupData.add(map);
//初始化Child数据
ArrayList<Map<String, String>> childDataList = new ArrayList<>();
for (int j = 0; j < 4; j++) {
HashMap<String, String> tmpMap = new HashMap<String, String>();
tmpMap.put("ability", "ability " + String.valueOf(i) + String.valueOf(j));
childDataList.add(tmpMap);
}
childData.add(childDataList);
}
mArrayAdapter = new SimpleExpandableListAdapter(this, groupData, groupLayout, groupFrom, groupTo, childData, childLayout, childFrom, childTo);
list.setAdapter(mArrayAdapter);
} private void clearData() {
groupData.clear();
childData.clear();
mArrayAdapter.notifyDataSetChanged();
}
}
 


GroupView:


<?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="50dp"
android:background="@android:color/holo_blue_light"
android:orientation="horizontal"> <TextView
android:id="@+id/txt_view_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textSize="20dp" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <TextView
android:id="@+id/txt_view_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp" /> <TextView
android:id="@+id/txt_view_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp" />
</LinearLayout>
</LinearLayout>

 

ChildView:


<?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="30dp"
android:orientation="vertical"> <TextView
android:id="@+id/txt_view_child_ability"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="18dp" />
</LinearLayout>

 

【原创】Android ExpandableListView使用的更多相关文章

  1. 【开源项目4】Android ExpandableListView

    如果你对Android提供的Android ExpandableListView并不满意,一心想要实现诸如Spotify应用那般的效果,那么SlideExpandableListView绝对是你最好的 ...

  2. [原创]Android Monkey 在线日志分析工具开发

    [原创]Android Monkey 在线日志分析工具开发 在移动App测试过程中,Monkey测试是我们发现潜在问题的一种非常有效手段,但是Android原生的Monkey有其天然的不足,数据不能有 ...

  3. [原创]Android 常用adb命令总结

    [原创]Android 常用adb命令总结 1 adb介绍 1.1 adb官方网站及下载 官方网站下载安装:http://adbshell.com/downloads 1.2 adb安装是否成功检查? ...

  4. [原创]Android Monkey测试工具使用介绍

    [原创]Android Monkey测试工具使用介绍 1 Android Monkey介绍 Monkey是Android中的一个命令行工具,可以运行在模拟器里或实际设备中.它向系统发送伪随机的用户事件 ...

  5. Android ExpandableListView

    ExpandableListView 结合SimpleExpandableListAdapter用法 最终实现效果: activity_main.xml <?xml version=" ...

  6. android ExpandableListView详解

    ExpandableListView是android中可以实现下拉list的一个控件,是一个垂直滚动的心事两个级别列表项手风琴试图,列表项是来自ExpandableListViewaAdapter,组 ...

  7. 解决android expandablelistview 里面嵌入gridview行数据重复问题

    最近做了一个“csdn专家博客App” 当然了是android版本,在专家浏览页面,我才用了expandablelistview 组件来显示专家分类,每个分类点击之后可以显示专家的头像和名字. 很简单 ...

  8. Android ExpandableListView的下拉刷新实现

    该控件的修改时根据PullToRefreshList的机制修改 下面是对ExpandableListView的扩展 package com.up91.gwy.view.componet; import ...

  9. Android ExpandableListView的技巧和问题

    前言: 最近一个多月在认真的学习Android和做项目,文章内容表达的不好或者理解错了,希望大家评论指出. :-) 本文是总结几个比较常用且使用的技巧,和一个大家都会遇到的问题. 文章中大部分语句摘抄 ...

随机推荐

  1. ZeroMQ接口函数之 :zmq_msg_set - 设置消息的性质

    ZeroMQ 官方地址 :http://api.zeromq.org/4-1:zmq_msg_set zmq_msg_set(3)  ØMQ Manual - ØMQ/3.2.5 Name zmq_m ...

  2. javascript创建跟随鼠标好玩的东西

    不说话,直接上代码. css: #createGoDivBox{ display: none; } #createGoDivBox div{ background-color: #00A6C2; po ...

  3. vim基本命令

    vim基本命令     1.vim#在命令行中输入vim,进入vim编辑器2.i#按一下i键,下端显示 --INSERT--#插入命令,在vim中可能任意字符都有作用3.Esc#退出i(插入)命令进行 ...

  4. NGUI 层级关系控制

    NGUI元素的遮挡情况是不依赖空间关系,所以在NGUI上添加特效有时候特别蛋疼,特别是美术同学还要依赖空间关系来控制特效效果,那先看看看NGUI的层级是怎么处理的,不过下面的描述都是针对单个相机下的P ...

  5. 阿里云服务器Linux CentOS安装配置(六)resin多端口配置、安装、部署

    阿里云服务器Linux CentOS安装配置(六)resin多端口配置.安装.部署 1.下载resin包 http://125.39.66.162/files/2183000003E08525/cau ...

  6. java基础总结——基础语法1

    一.标示符 二.关键字 三.java基本数据类型 3.1 常量 ● 常量表示不能改变的数值. ● java中常量的分类: 1. 整数常量.所有整数 2. 小数常量.所有小时 3. 布尔型常量.较为特有 ...

  7. iOS 10的正确解锁方式

    在iOS 10上,锁屏状态通过按下电源键点亮屏幕之后,用手指轻触Home键,实际上手机是已经解锁了的,不信请看如下截图: 虽然手机已经解锁,但与iOS 9不同的是,此时手机还处在解锁界面而没有进入主屏 ...

  8. New library for Matlab - test

    Thanks  http://www.matlabsky.com/thread-120-1-1.html Install directory $MatlabRoot\toolbox or >&g ...

  9. 使用Python写Windows Service服务程序

    1.背景 如果你想用Python开发Windows程序,并让其开机启动等,就必须写成windows的服务程序Windows Service,用Python来做这个事情必须要借助第三方模块pywin32 ...

  10. visual studio2015使用git管理源代码

    1.注册https://git.oschina.net/ 2.注册好后,创建一个测试项目,如下图: 点击创建,如下: 上面的红框中的地址下面会用到. 3.git初始化设置 在本地电脑要安装git,打开 ...