ExpandableList就是可展开的ListView
首先我们来看一下页面的布局

expandlist_layout.xml文件

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><ExpandableListView
        android:id="@+id/expendlist"android:layout_width="match_parent"android:layout_height="match_parent" />
</RelativeLayout>

expendlist_group.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center_vertical"android:padding="10dp"><ImageView
        android:id="@+id/group_img"android:layout_width="36dp"android:layout_height="36dp"android:layout_centerVertical="true"android:layout_marginLeft="20dp"android:src="@drawable/ic_next" /><TextView
        android:id="@+id/groupname_tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_toRightOf="@+id/group_img"android:layout_marginLeft="10dp"android:text="张三"android:textSize="18sp" />
</RelativeLayout>

expendlist_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="70dp"android:orientation="horizontal"android:gravity="center_vertical"android:layout_marginBottom="1dp"android:padding="10dp"><ImageView
        android:id="@+id/icon_img"android:layout_width="48dp"android:layout_height="48dp"android:layout_marginLeft="20dp"android:src="@drawable/android" /><LinearLayout
        android:layout_marginLeft="50dp"android:layout_width="match_parent"android:layout_height="60dp"android:gravity="center_vertical"android:orientation="vertical" ><TextView
            android:id="@+id/itemname_tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="22sp"android:textStyle="bold"android:text="Android" /><TextView
            android:id="@+id/info_tv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="编程学习" /></LinearLayout>
</LinearLayout>

创建适配器MyExpandableListViewAdapter

package com.example.administrator.expandablelistview;import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;import java.util.List;public class MyExpandableListViewAdapter extends BaseExpandableListAdapter {private Context mContext = null;private List<String> mGroupList = null;private List<List<String>> mItemList = null;public MyExpandableListViewAdapter(Context context, List<String> groupList,List<List<String>> itemList) {this.mContext = context;this.mGroupList = groupList;this.mItemList = itemList;}
// 获取组的个数@Overridepublic int getGroupCount() {return mGroupList.size();}
// 获取指定组中的子元素个数@Overridepublic int getChildrenCount(int groupPosition) {return mItemList.get(groupPosition).size();}
//获取指定组中的数据@Overridepublic String getGroup(int groupPosition) {return mGroupList.get(groupPosition);}//获取指定组中的指定子元素数据。@Overridepublic String getChild(int groupPosition, int childPosition) {return mItemList.get(groupPosition).get(childPosition);}// 获取指定组的ID,这个组ID必须是唯一的@Overridepublic long getGroupId(int groupPosition) {return groupPosition;}//获取指定组中的指定子元素ID@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}
// 获取显示指定组的视图对象//groupPosition 组位置
//isExpanded    该组是展开状态还是伸缩状态
//convertView   重用已有的视图对象
//parent        返回的视图对象始终依附于的视图组@Overridepublic View getGroupView(int groupPosition, boolean isExpanded, View convertView,ViewGroup parent) {GroupHolder groupHolder = null;if (convertView == null) {convertView = LayoutInflater.from(mContext).inflate(R.layout.expendlist_group, null);groupHolder = new GroupHolder();groupHolder.groupNameTv = (TextView) convertView.findViewById(R.id.groupname_tv);groupHolder.groupImg = (ImageView) convertView.findViewById(R.id.group_img);convertView.setTag(groupHolder);} else {groupHolder = (GroupHolder) convertView.getTag();}if (isExpanded) {groupHolder.groupImg.setImageResource(R.drawable.ic_up);//展开} else {groupHolder.groupImg.setImageResource(R.drawable.ic_next);//未展开}groupHolder.groupNameTv.setText(mGroupList.get(groupPosition));return convertView;}//获取一个视图对象,显示指定组中的指定子元素数据。@Overridepublic View getChildView(int groupPosition, int childPosition, boolean isLastChild,View convertView, ViewGroup parent) {ItemHolder itemHolder = null;if (convertView == null) {convertView = LayoutInflater.from(mContext).inflate(R.layout.expendlist_item, null);itemHolder = new ItemHolder();itemHolder.nameTv = (TextView) convertView.findViewById(R.id.itemname_tv);itemHolder.iconImg = (ImageView) convertView.findViewById(R.id.icon_img);convertView.setTag(itemHolder);} else {itemHolder = (ItemHolder) convertView.getTag();}itemHolder.nameTv.setText(mItemList.get(groupPosition).get(childPosition));itemHolder.iconImg.setBackgroundResource(R.drawable.head_img_1);return convertView;}// 组和子元素是否持有稳定的ID,也就是底层数据的改变不会影响到它们。@Overridepublic boolean hasStableIds() {return true;}//是否选中指定位置上的子元素。@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {return true;}class GroupHolder {public TextView groupNameTv;public ImageView groupImg;}class ItemHolder {public ImageView iconImg;public TextView nameTv;}
}

ExpandableListActivity.java

package com.example.administrator.expandablelistview;import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;import java.util.ArrayList;
import java.util.List;public class ExpandableListActivity extends AppCompatActivity {private ExpandableListView mExpandableListView = null;// 列表数据private List<String> mGroupNameList = null;private List<List<String>> mItemNameList = null;// 适配器private MyExpandableListViewAdapter mAdapter = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.expandlist_layout);// 获取组件mExpandableListView = (ExpandableListView) findViewById(R.id.expendlist);mExpandableListView.setGroupIndicator(null);// 初始化数据initData();// 为ExpandableListView设置AdaptermAdapter = new MyExpandableListViewAdapter(this, mGroupNameList, mItemNameList);mExpandableListView.setAdapter(mAdapter);// 监听组点击mExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {@Overridepublic boolean onGroupClick(ExpandableListView parent, View v,int groupPosition, long id) {if (mGroupNameList.get(groupPosition).isEmpty()) {return true;}return false;}});// 监听每个分组里子控件的点击事件mExpandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {@Overridepublic boolean onChildClick(ExpandableListView parent, View v, int groupPosition,int childPosition, long id) {Toast.makeText(ExpandableListActivity.this,mAdapter.getGroup(groupPosition) + ":"+  mAdapter.getChild(groupPosition, childPosition) ,Toast.LENGTH_SHORT).show();return false;}});}// 初始化数据private void initData(){// 组名mGroupNameList = new ArrayList<String>();mGroupNameList.add("后端开发");mGroupNameList.add("前端开发");mGroupNameList.add("移动开发");mItemNameList = new  ArrayList<List<String>>();// 后端组List<String> itemList = new ArrayList<String>();itemList.add("Java");itemList.add("SpringBoot");itemList.add("Python");itemList.add("Go");itemList.add("PHP");itemList.add("C");itemList.add("C++");mItemNameList.add(itemList);// 前端组itemList = new ArrayList<String>();itemList.add("HTML/CSS ");itemList.add("JavaScript");itemList.add("Vue.js");itemList.add("jQuery");itemList.add("Bootstrap");itemList.add("Angular");mItemNameList.add(itemList);// 移动组itemList = new ArrayList<String>();itemList.add("Android");itemList.add("iOS");itemList.add("React native");itemList.add("WEEX");mItemNameList.add(itemList);}
}

最终实现的效果图

可折叠列表ExpandableList相关推荐

  1. Android基础入门教程——2.4.12 ExpandableListView(可折叠列表)的基本使用

    Android基础入门教程--2.4.12 ExpandableListView(可折叠列表)的基本使用 标签(空格分隔): Android基础入门教程 本节引言: 本节要讲解的Adapter类控件是 ...

  2. 【Android 】零基础到飞升 | ExpandableListView(可折叠列表)的基本使用

    2.5.5 ExpandableListView(可折叠列表)的基本使用 本节引言: 本节要讲解的Adapter类控件是ExpandableListView,就是可折叠的列表,它是ListView的子 ...

  3. Amdroid ExpandableListView(可折叠列表)的基本使用

    1.ExpandableListView(可折叠列表)的使用: <?xml version="1.0" encoding="utf-8"?>< ...

  4. [Android]BaseExpandableListAdapter实现可折叠列表

    使用BaseExpandableListAdapter 以实现的可折叠的所谓列表,例如QQ朋友们在分组功能. 基于BaseExpandableListAdapter扩大ExpandableList说明 ...

  5. android 二级折叠列表,Android折叠列表 ExpandableList

    ExpandableList 是折叠列表,通过继承ExpandableListActivity 类就可以非常简单的实现折叠列表. 效果图: 代码实现 package com.zhou.activity ...

  6. android 展开菜单,Android之可收缩展开列表ExpandableList

    在Android的app包中,有这么一个类,这个类继承自Activity,它叫ExpandableListActivity.顾名思义,从它的名字可以看出该类是一种可扩展性的列表List,我们这里理解成 ...

  7. java 下拉列表 可折叠 qq分组_2.5.5 ExpandableListView(可折叠列表)的基本使用

    本节引言: 本节要讲解的Adapter类控件是ExpandableListView,就是可折叠的列表,它是ListView的子类, 在ListView的基础上它把应用中的列表项分为几组,每组里又可包含 ...

  8. Android入门教程四十五之ExpandableListView(可折叠列表)的基本使用

    本节要讲解的Adapter类控件是ExpandableListView,就是可折叠的列表,它是ListView的子类, 在ListView的基础上它把应用中的列表项分为几组,每组里又可包含多个列表项. ...

  9. ExpandableListView(可折叠列表)的基本使用

    本节引言: 本节要讲解的Adapter类控件是ExpandableListView,就是可折叠的列表,它是ListView的子类,在ListView的基础上它把应用中的列表项分为几组,每组里又可包含多 ...

最新文章

  1. Android Studio 3.5 Canary 12 发布
  2. 【阿里云 Linux 服务器】mysql 遇到以下问题Expression #1 of SELECT list is not in GROUP BY clause and contains nonag
  3. 多线程 python layer_在Caffe中加Python Layer的方法
  4. 浙江理工大学2019年1月赛
  5. 关于构造与析构过程中调用虚函数的问题
  6. JAVA 成员访问权限修饰符
  7. 服务器双cpu性能强不,双CPU的电脑用起来,性能和功耗都是原来的两倍?
  8. php mysql 持久化_PHP: mysqli 扩展和持久化连接 - Manual
  9. python的软件环境是什么意思_python的虚拟环境详解
  10. openGL天空盒实现-立方体贴图
  11. 2022年6月大学英语六级作文
  12. js生成java uuid_javascript 生成UUID
  13. C语言链表详解附实例
  14. 一元二次方程组求根问题
  15. C51最小系统板红外遥控控制小车
  16. HTML span 标签 的详细用法
  17. 【python】模拟淘宝的客服自动回复系统-socket,json,time模块的应用
  18. supper 关键字
  19. 2015 电子科大校园招聘名单(更新中)
  20. MySQL varchar(255) 和 varchar(256) 区别

热门文章

  1. 七星彩长奖表图_够力七星彩奖表长条图
  2. 安装opencv_java 时 brew 安装报错不支持pre-release version
  3. C++体育俱乐部积分管理
  4. Unity--升级Android api level 28踩坑记录
  5. 2020年山东省土地利用数据(矢量)
  6. C#判断ip是否可用
  7. java编辑器IDEA常用快捷键
  8. (C语言)八大排序之:基数排序
  9. 快来看,手把手教学 满意度调查如何抽样
  10. css加载更多动画效果,css3实现loading动画效果