先看效果



demo实现

其他的方法和ListView的方法一样,下面来看看具体demo的实现

首先布局文件很简单,就一个控件为:

<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.duanlian.expendablelistviewdemo.MainActivity">
<ExpandableListView
android:id="@+id/expendablelistview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>

Activity的代码和用ListView的是一样的,创建一个adapter和两个 数据源,然后绑定,具体如下:

package com.duanlian.expendablelistviewdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ExpandableListView; import com.duanlian.expendablelistviewdemo.adapter.MyAdapter; import java.util.ArrayList;
import java.util.List; public class MainActivity extends AppCompatActivity {
private ExpandableListView expandableListView;
private MyAdapter myAdapter;
private List<String> groupList;
private List<List<String>> childList; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
} private void initView() {
expandableListView = (ExpandableListView) findViewById(R.id.expendablelistview);
groupList = new ArrayList<>();
childList = new ArrayList<>();
addData("幼稚园同学",new String[]{"周杰伦","江一燕 ","佟丽娅","高圆圆","刘诗诗","刘亦菲","angleBaby","张静初","张含韵",});
addData("小学同学",new String[]{"光头强","熊大","熊二","妙蛙种子","比卡丘","双蛋瓦斯","贪吃蛇"});
addData("初中同学",new String[]{"苍井空","小泽玛利亚","吉泽明步","波多野结衣","爱川美里菜","小川阿佐美","桃谷绘里香","泷泽萝拉","北原多香子","石川施恩惠","北条麻妃","麻仓优","羽田爱","保坂绘里"});
addData("高中同学",new String[]{"秦始皇","李世民","武则天","曹操","刘备","孙权"});
addData("大学同学",new String[]{"周杰伦","江一燕 ","佟丽娅","高圆圆","刘诗诗","刘亦菲","angleBaby","张静初","张含韵",});
addData("研究生同学",new String[]{"光头强","熊大","熊二","妙蛙种子","比卡丘","双蛋瓦斯","贪吃蛇"});
addData("博士同学",new String[]{"苍井空","小泽玛利亚","吉泽明步","波多野结衣","爱川美里菜","小川阿佐美","桃谷绘里香","泷泽萝拉","北原多香子","石川施恩惠","北条麻妃","麻仓优","羽田爱","保坂绘里"});
addData("教授同事",new String[]{"秦始皇","李世民","武则天","曹操","刘备","孙权"});
addData("众仙家名册",new String[]{"苍井空","小泽玛利亚","吉泽明步","波多野结衣","爱川美里菜","小川阿佐美","桃谷绘里香","泷泽萝拉","北原多香子","石川施恩惠","北条麻妃","麻仓优","羽田爱","保坂绘里","秦始皇","李世民","武则天","曹操","刘备","孙权"});
myAdapter = new MyAdapter(this,groupList,childList);
expandableListView.setAdapter(myAdapter); } /**
* 用来添加数据的方法
*/
private void addData(String group, String[] friend) {
groupList.add(group);
//每一个item打开又是一个不同的list集合
List<String> childitem = new ArrayList<>();
for (int i = 0; i < friend.length; i++) {
childitem.add(friend[i]);
}
childList.add(childitem);
}
}

然后Adapter里面需要2个item布局文件:

外层的item:

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ed88ed"
tools:context="com.duanlian.expendablelistviewdemo.MainActivity"> <TextView
android:id="@+id/group_textview"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:gravity="center_vertical"
android:layout_marginLeft="40dp"
android:textSize="20sp"
android:text="我的好友"/>
<TextView
android:id="@+id/group_number"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:gravity="center_vertical"
android:layout_marginRight="20dp"
android:textSize="15sp"
android:layout_alignParentRight="true"
android:text="20"/>
</RelativeLayout>

里面那层item布局:

<?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:layout_width="match_parent"
android:layout_height="40dp"
tools:context="com.duanlian.expendablelistviewdemo.MainActivity"> <com.duanlian.expendablelistviewdemo.CircleImageView
android:id="@+id/child_img"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
android:src="@mipmap/duanlian"/>
<TextView
android:id="@+id/child_name"
android:layout_height="40dp"
android:layout_width="wrap_content"
android:layout_toRightOf="@+id/child_img"
android:layout_marginLeft="15dp"
android:textSize="16sp"
android:gravity="center_vertical"
android:text="吉泽明步"/>
<TextView
android:id="@+id/child_model"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:gravity="center_vertical"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:textSize="13sp" android:text="4G"/>
</RelativeLayout>

最总要的还是在Adapter里面,他和ListView一样,也有simpleAdapter和ArrayAdapter,和BaseAdapter,我这里用的是BaseAdapter,他的adapter继承的是BaseExpandableListAdapter:

package com.example.wxpandablelistviewdemo;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import java.util.List; /**
* Created by duanlian on 2016/9/12.
*/
public class MyAdapter extends BaseExpandableListAdapter {
private List<String> groupList;//外层的数据源
private List<List<String>> childList;//里层的数据源
private Context context; public MyAdapter(Context context, List<String> groupList,List<List<String>> childList ){
this.context = context;
this.groupList = groupList;
this.childList = childList;
} @Override
public int getGroupCount() {
return groupList.size();
} /**
* 这个返回的一定要是对应外层的item里面的List集合的size
* @param groupPosition
* @return
*/
@Override
public int getChildrenCount(int groupPosition) {
return childList.get(groupPosition).size();
} @Override
public Object getGroup(int groupPosition) {
return groupPosition;
} @Override
public Object getChild(int groupPosition, int childPosition) {
return childList.get(groupPosition).get(childPosition);
} @Override
public long getGroupId(int groupPosition) {
return groupPosition;
} @Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
} @Override
public boolean hasStableIds() {
return true;
} @Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
convertView = View.inflate(context, R.layout.item_group, null);
//分组名字
TextView textView = (TextView) convertView.findViewById(R.id.group_textview);
//子元素的个数
TextView number = (TextView) convertView.findViewById(R.id.group_number);
number.setText(childList.get(groupPosition).size()+"个");
textView.setText(groupList.get(groupPosition));
return convertView;
} @Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup viewGroup) {
view = View.inflate(context, R.layout.item_child, null);
TextView textView = (TextView) view.findViewById(R.id.child_name);
//外层的分组名字
textView.setText(childList.get(groupPosition).get(childPosition));
return view;
} @Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}

demo地址:https://download.csdn.net/download/heishuai123/10941739

原博客地址:https://blog.csdn.net/dl10210950/article/details/52525492

可折叠的listview 之ExpandableListView基本使用的更多相关文章

  1. ExpandableListView简单应用及listview模拟ExpandableListView

    首先我们还是来看一些案例,还是拿搜狐新闻客户端,因为我天天上下班没事爱看这个东东,上班又没时间看新闻,上下班路途之余浏览下新闻打发时间嘛.           看这个效果挺棒吧,其实实现起来也不难,我 ...

  2. Android ListView与ExpandableListView设置分割线divider

    listview设置分割线需要以下操作: lv.setDivider(getResources().getDrawable(R.drawable.diyline)); ExpandableListVi ...

  3. 53、listview、expandableListview如何选中时保持高亮?

    一.listView被选中后保持高亮 70down voteaccepted To hold the color of listview item when you press it, include ...

  4. Android 关于ExpandableListView刷新的解决办法

    正文 首先是最基础的 ExpandableListView vList = (ExpandableListView) this.findViewById(R.id.list); EListAdapte ...

  5. Android 使用PullToRefresh实现下拉刷新和上拉加载(ExpandableListView)

    PullToRefresh是一套实现非常好的下拉刷新库,它支持: 1.ListView 2.ExpandableListView 3.GridView 4.WebView 等多种常用的需要刷新的Vie ...

  6. android ListView上拉加载更多 下拉刷新功能实现(采用pull-to-refresh)

    Android实现上拉加载更多功能以及下拉刷新功能, 采用了目前比较火的PullToRefresh,他是目前实现比较好的下拉刷新的类库. 目前他支持的控件有:ListView, ExpandableL ...

  7. Android 开发自己的网络收音机3——电台分类(ExpandableListView)

    上一篇文章说了使用SlidingMenu开源项目实现侧滑栏,今天主要是讲解多级列表ExpandableListView的使用,以及如何使用它实现电台分类管理.ExpandableListView是An ...

  8. android--------ListView和ExpandableListView的侧滑删除操作

    本案例主要实现了ListView和ExpandableListView的侧滑删除操作功能 效果图: ListView的Adapter类 private class SlideAdapter exten ...

  9. android 的 ExpandableListView Example Tutorial

    https://www.journaldev.com/9942/android-expandablelistview-example-tutorial Welcome to Android Expan ...

随机推荐

  1. C++编码规范101

    组织和策略问题 第0条 不要拘泥于小节(又名:了解哪些东西不应该标准化) 第1条 在高警告级别干净利落地进行编译 第2条 使用自动构建系统 第3条 使用版本控制系统 第4条 在代码审查上投入 设计风格 ...

  2. windows服务那些事

    前一段时间由于项目需求,写了一个windows服务.下面总结如下: windows服务其实就是一些后台程序,和其他程序的主要区别是它运行于系统后台.微软公司为了方便我们自己定制我们的服务,提供了很多借 ...

  3. IO调度

    互联网公司不关注真实的文件系统,他们关注VFS层,关注block层,关注IO的管控. queue->make_request_fn ( blk_queue_bio ),其中blk_queue_b ...

  4. 一张图彻底搞懂JavaScript的==运算

    一张图彻底搞懂JavaScript的==运算 来源 https://zhuanlan.zhihu.com/p/21650547 PS:最后,把图改了一下,仅供娱乐 : ) 大家知道,==是JavaSc ...

  5. [BZOJ3473][BZOJ3277]字符串

    [BZOJ3473][BZOJ3277]字符串 试题描述 给定 \(n\) 个字符串,询问每个字符串有多少子串(不包括空串)是所有 \(n\) 个字符串中至少 \(k\) 个字符串的子串? 输入 第一 ...

  6. jsp电子商务 购物车实现之一 设计篇

    购物车的功能实现. 查询的资料,找到三种方法: 1.用cookie实现购物车: 2.用session实现购物车: 3.用cookie和数据库(购物车信息持久化)实现购物车: ============= ...

  7. yaf的安装

    http://kenby.iteye.com/blog/1979899 yaf源码分析学习网站 # wget https://github.com/laruence/php-yaf/archive/m ...

  8. Codeforces Round #524 (Div. 2) D. Olya and magical square

    D. Olya and magical square 题目链接:https://codeforces.com/contest/1080/problem/D 题意: 给出一个边长为2n的正方形,每次可以 ...

  9. POJ 1050 To the Max 二维最大子段和

    To the MaxTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 52281 Accepted: 27633Description ...

  10. mapreduce出现大量task被KILLED_UNCLEAN的3个原因

    Request received to kill task 'attempt_201411191723_2827635_r_000009_0' by user ------- Task has bee ...