ExpandableListView 结合SimpleExpandableListAdapter用法

最终实现效果:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.zy.expandablelistview.MainActivity"> <ExpandableListView
android:id="@+id/expand_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"> </ExpandableListView>
</RelativeLayout>

父级布局文件group_item.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="50dp"
android:gravity="center_vertical" android:orientation="vertical"> <TextView
android:id="@+id/tv_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> </LinearLayout>

子级布局文件child_item.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="30dp"
android:paddingLeft="30dp"
android:gravity="center_vertical"
android:orientation="vertical"> <TextView
android:id="@+id/tv_child"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

MainActivity.java

package com.zy.expandablelistview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; public class MainActivity extends AppCompatActivity { List<Map<String,?>> groupList = new ArrayList<>();
List<List<Map<String,String>>> itemList = new ArrayList<>();
String[] groupStringArr = {"腾讯","百度","阿里巴巴"};
String[][] itemStringArr = {
{"QQ","腾讯云服务","QQ浏览器"},
{"百度地图","百度搜索","百度外卖"},
{"淘宝","天猫","阿里云"}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.expand_list_view); //父级数组
for (int i = 0; i < groupStringArr.length; i++) {
Map<String,String> gMap = new HashMap<>();
gMap.put("groupName",groupStringArr[i]);
groupList.add(gMap);
//子级数组
List<Map<String,String>> itemFor = new ArrayList<>();
for (int j = 0; j < itemStringArr[i].length; j++) {
Map<String,String> iMap = new HashMap<>();
iMap.put("itemName",itemStringArr[i][j]);
itemFor.add(iMap);
}
itemList.add(itemFor);
} SimpleExpandableListAdapter simpleExpandableListAdapter = new SimpleExpandableListAdapter(this,
groupList,R.layout.group_item,new String[]{"groupName"},new int[]{R.id.tv_group},
itemList,R.layout.child_item,new String[]{"itemName"},new int[]{R.id.tv_child}); expandableListView.setAdapter(simpleExpandableListAdapter);
}
}

Android ExpandableListView的更多相关文章

  1. 【开源项目4】Android ExpandableListView

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

  2. 【原创】Android ExpandableListView使用

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

  3. android ExpandableListView详解

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

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

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

  5. Android ExpandableListView的下拉刷新实现

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

  6. Android ExpandableListView的技巧和问题

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

  7. Android ExpandableListView 带有Checkbox的简单应用

    expandablelistview2_groups.xml <?xml version="1.0" encoding="utf-8"?> < ...

  8. Android ExpandableListView的简单应用

    Expandablelistview1Activity.java package com.wangzhu.demoexpandablelistview; import java.util.ArrayL ...

  9. Android ExpandableListView使用+获取SIM卡状态信息

    ExpandableListView 是一个可以实现下拉列表的控件,大家可能都用过QQ,QQ中的好友列表就是用ExpandableListView实现的,不过它是自定义的适配器.本篇 博客除了要介绍E ...

随机推荐

  1. Checkpoints codeforces 709B

    http://codeforces.com/problemset/problem/709/B 题意:给出一条横向坐标轴,给出Vasya所在的坐标位置及其另外n个坐标.Vasya想要至少访问n-1个位置 ...

  2. Itext Demo

    Tables and fonts /** * Example written by Bruno Lowagie in answer to the following question: * http: ...

  3. [刘阳Java]_什么是MyBatis_第1讲

    1.什么MyBatis,我们先通过百度百科先进行一个简单的了解 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation ...

  4. html5调取手机摄像头或相册

    html5调用手机摄像头或者相册 由于input的type=file 格式的文件的界面并不是我们所希望的界面,所以在此我隐藏input,自定义样式,这个样式就在<a>中自己定义,这里我就不 ...

  5. 使用Axis2 插件 报错"An error occurred while completing process -java.lang.reflect.InvocationTargetException"

    参考 http://blog.csdn.net/sunitjy/article/details/6793654

  6. keepalived高可用

    keepalived介绍 Keepalived是一个基于vrrp协议的高可用方案,vrrp协议的软件实现,原生设计的目的为了高可用ipvs服务. 1. vrrp协议 VRRP是一种容错协议,它通过把几 ...

  7. jQuery管理包装集笔记

    size():返回包装集中元素的个数. get([n]):返回一个DOM元素或DOM元素数组(接受负值). toArray():将包装里的所有元素作为DOM元素数组返回. eq(n):获取包装集中与i ...

  8. 百度地图API 与 jquery 同时使用时报 TypeError $(...) is null错误 失效的原因及解决办法

    在引用百度地图API后,发现jquery 根据id 找不到 form.但是对于别的控件没有问题. 在排除了 html加载的问题后. 上网查找 发现以下解决办法: 原因应该是有冲突的插件. 解决办法将 ...

  9. sublime Text 的安装、破解、汉化、使用

    Sublime Text 3安装与使用   本文是Sublime Text 全程指引 by Lucida (http://www.cnblogs.com/figure9/p/sublime-text- ...

  10. Linux(Ubuntu 14.0)

    开始了Mono的学习.学习了Mono for Android之后,编译一些小的APK,总发现这些APK文件很大,额,真心不知道为什么,那么,就让我们从头开始学期了,Android是基于Linux的,那 ...