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. STL用法总结

    stringstream用法:对已有的运算符赋予多重含义,使同一个运算符作用于不同类型的数据导致不同类型的行为. stream << i   将i输入流中 stream >> ...

  2. 01 LabVIEW的类中各个Scope的范围

    范例地址: D:\Program Files (x86)\National Instruments\LabVIEW 2015\examples\Object-Oriented Programming\ ...

  3. ps 文字处理篇

    ps文字处理篇 1.对文字镂空处理并且移除到新图像上: 首先创建图层-文字编辑-横排编辑 其次 魔棒工具选择通过颜色来选择选区 右击图层-栅格化图层 删除键将选择的颜色删除留下选区- 复制粘贴到另一个 ...

  4. jQuery基础,定时器,工厂函数

    这个星期刚刚学的JQuery,下面我来说说我学的这几个例子 jQuery是JavaScript的一个程序库. Jquery的工厂函数$(): 在Jquery中 $符号等价于jquery,作用是将DOM ...

  5. 关于iPhone设备不同显示尺寸适配的一些方法

    关于iPhone设备不同显示尺寸适配的一些方法   ------关于适配的理解------ 1.什么是适配? 适配是对不同硬件和系统软件的适应,硬件包括屏幕显示,处理器,内存等等(目前主要是屏幕适配, ...

  6. Ant和Maven的作用是什么?两者之间功能、特点有哪些区别?

    Ant和Maven都是基于Java的构建(build)工具.理论上来说,有些类似于(Unix)C中的make ,但没有make的缺陷. Ant是软件构建工具,Maven的定位是软件项目管理和理解工具. ...

  7. [Visual Studio]项目属性中继承的值怎么删除

    遇到一个问题,莫名奇妙编译,却出错"找不到包含文件<winapifamily.h>",之前从没出过问题啊!百思不得其解. 研究包含winapifamily的位置,发现有 ...

  8. 基于fab自动化部署

    fab是一个python库,强大好使,可以做很多帮助你减轻工作量的事情,比如在多台服务器上部署web项目,这里就讲讲使用它简单的方法来执行部署的过程. 关于fab的安装的基本使用,网上一搜一大把,内容 ...

  9. Springboot数据库连接池报错的解决办法

    Springboot数据库连接池报错的解决办法 这个异常通常在Linux服务器上会发生,原因是Linux系统会主动断开一个长时间没有通信的连接 那么我们的问题就是:数据库连接池长时间处于间歇状态,导致 ...

  10. 对Git的理解

    GIT属于计算机软件,功能是分布式版本控制系统,字面意思还挺有意思的:愚蠢或不开心的人,首先呢,这款软件是免费的.这款软件是免费的.这款软件是免费的.(重要的地方要说三遍)即使它不是免费的,在我大天朝 ...