先看效果



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. PokeCats开发者日志(七)

      现在是PokeCats游戏开发的第十二天的晚上,很不幸提交到的三个平台(360开放平台,腾讯开放平台,华为应用市场)都没通过,著作权申请也被打回来了.   心中一万只草泥马在奔腾.   得了,看来 ...

  2. 当网卡收到的包的目的地址是主机上另一个网卡的地址.arp总结

    2019/01/13 今天测试发现结果不符合预期呀,发现设置了arp_filter之后,仍然是能ping通主机上的另外一张网卡.但是现在的问题是 -------------------- 内核中是如何 ...

  3. [剑指Offer] 43.左旋转字符串

    题目描述 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果.对于一个给定的字符序列S,请你把其循环左移K位后的序列输出.例如,字符序列S=”abc ...

  4. BZOJ4690 Never Wait for Weights(并查集)

    带权并查集按秩合并即可维护. #include<iostream> #include<cstdio> #include<cmath> #include<cst ...

  5. [Leetcode] 3sum-closest 给定值,最为相近的3数之和

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  6. 洛谷P4591 [TJOI2018]碱基序列 【KMP + dp】

    题目链接 洛谷P4591 题解 设\(f[i][j]\)表示前\(i\)个串匹配到位置\(j\)的方案数,匹配一下第\(i\)个串进行转移即可 本来写了\(hash\),发现没过,又写了一个\(KMP ...

  7. C# new override

    A -> virtual Fun B : A -> override Fun C : B -> override Fun D : C -> new virtual Fun E ...

  8. Spring学习-- SpEL表达式

    Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言. 语法类似于 EL:SpEL 使用 #{...} 作为定界符 , 所有在大括号中的字符都将被认为是 SpE ...

  9. JS表单验证优化

    var validate = (function(){ var messages = { isEmail : '输入正确格式邮箱', isPhoneNum : '输入正确手机号' }; var val ...

  10. Intelij idea新窗口打开项目设置

    1.idea 打开后 file->setting   2.appearance&behave->system setting->open project in new win ...