ExpandableListView继承ListView,具有LIstVIew的基本功能。此外具有group/child,由组与子元素组成。

1.布局主要有是三个。

a.主布局:

<ExpandableListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/expandlistview"
android:dividerHeight="5dp"
android:background="#ffffff"
android:divider="@drawable/expandchilddivide"
android:childDivider="#000000"
/>

b.Group布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/group_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:gravity="center_horizontal"
android:text="122"
/> </LinearLayout>

c.Child布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/textOne"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="1"
/>
<TextView
android:id="@+id/textTwo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="2"
/>
<TextView
android:id="@+id/textThree"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="3"
/>
</LinearLayout>
</LinearLayout>

2.代码生成 类似adapter 创建adapter(ExpandableListAdapter、BaseExpandableListAdapter、SimpleExpandableListAdapter 依次继承关系)

a.创建adapter

//自定义适配器
class Adapter extends BaseExpandableListAdapter {
//获取子元素对象
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
} //获取子元素Id
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
} //加载子元素并显示
@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View view = null;
ChildHolder childholder = null;
if (convertView != null) {
view = convertView;
childholder = (ChildHolder) view.getTag();
} else {
view = View.inflate(ExpandableListViewActi.this, R.layout.expand_child, null);
childholder = new ChildHolder();
//childholder.mImage = (ImageView) view.findViewById(R.id.image);
childholder.mPrice = (TextView) view.findViewById(R.id.textTwo);
childholder.mStateText = (TextView) view.findViewById(R.id.textOne);
childholder.mSecondPrice = (TextView) view.findViewById(R.id.textThree);
view.setTag(childholder);
}
// childholder.mImage.setOnClickListener(new OnClickListener() {
// @Override
// public void onClick(View v) {
// Toast.makeText(MainActivity.this, "第"+groupPosition+"组的第"+childPosition+"圖標被点击了", 0).show();
// }
// });
childholder.mPrice.setText(child_list.get(groupPosition));
int len = group_list.size();
System.out.println(len + "-----------------");
childholder.mStateText.setText(child_list.get(groupPosition));
childholder.mSecondPrice.setText(child_list.get(groupPosition));
return view;
} //获取子元素数目
@Override
public int getChildrenCount(int groupPosition) {
return child_list.size();
} //获取组元素对象
@Override
public Object getGroup(int groupPosition) {
return group_list.get(groupPosition);
} //获取组元素数目
@Override
public int getGroupCount() {
return group_list.size();
} //获取组元素Id
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
} //加载并显示组元素
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View view = null;
GroupHolder groupholder = null;
if (convertView != null) {
view = convertView;
groupholder = (GroupHolder) view.getTag();
} else {
view = View.inflate(ExpandableListViewActi.this, R.layout.expand_group, null);
groupholder = new GroupHolder();
groupholder.mSpaceText = (TextView) view.findViewById(R.id.group_text);
view.setTag(groupholder);
}
groupholder.mSpaceText.setText(group_list.get(groupPosition));
return view;
} @Override
public boolean hasStableIds() { return true;
} @Override
public boolean isChildSelectable(int groupPosition, int childPosition) { return true;
} }

b.生成主界面代码,设置adapter .expandable四中点击相应事件。

private void initView() {
mListView = (ExpandableListView) findViewById(R.id.expandlistview);
mInflater = LayoutInflater.from(ExpandableListViewActi.this);
group_list = new ArrayList<String>();
for (int i = 0; i < 5; i++) {
group_list.add("zcx");
child_list.add("child");
}
Adapter adapter = new Adapter();
//mListView.setGroupIndicator(null); /**
* ExpandableListView的组监听事件
*/
// mListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
//
// @Override
// public boolean onGroupClick(ExpandableListView parent, View v,
// int groupPosition, long id) {
// Toast.makeText(ExpandableListViewActi.this, "第" + groupPosition + "组被点击了", 0).show();
// return true;
// }
// });
/**
* ExpandableListView的组展开监听
*/
mListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { @Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(ExpandableListViewActi.this, "第" + groupPosition + "组展开", 0).show();
}
});
/**
* ExpandableListView的组合拢监听
*/
mListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() { @Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(ExpandableListViewActi.this, "第" + groupPosition + "组合拢", 0).show();
}
});
/**
* ExpandableListView的子元素点击监听
*/
mListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(ExpandableListViewActi.this, "第" + groupPosition + "组的第" + childPosition + "被点击了", 0).show();
return true;
}
}); mListView.setAdapter(adapter);
// int groupCount = mListView.getCount();
// for(int i=0;i<groupCount;i++){
// mListView.expandGroup(i);
// }
}

3.效果图

android-基础编程-ExpandableListview的更多相关文章

  1. android: 多线程编程基础

    9.1   服务是什么 服务(Service)是 Android 中实现程序后台运行的解决方案,它非常适合用于去执行那 些不需要和用户交互而且还要求长期运行的任务.服务的运行不依赖于任何用户界面,即使 ...

  2. Android网络编程基础

    Android网络编程只TCP通信 TCP 服务器端工作的主要步骤如下.步骤1 调用ServerSocket(int port)创建一个ServerSocket,并绑定到指定端口上.步骤2 调用acc ...

  3. Qt on Android 核心编程

    Qt on Android 核心编程(最好看的Qt编程书!CSDN博主foruok倾力奉献!) 安晓辉 著   ISBN 978-7-121-24457-5 2015年1月出版 定价:65.00元 4 ...

  4. Android网络编程系列 一 TCP/IP协议族

    在学习和使用Android网路编程时,我们接触的仅仅是上层协议和接口如Apache的httpclient或者Android自带的httpURlconnection等等.对于这些接口的底层实现我们也有必 ...

  5. Android网络编程系列 一 Socket抽象层

     在<Android网络编程>系列文章中,前面已经将Java的通信底层大致的描述了,在我们了解了TCP/IP通信族架构及其原理,接下来我们就开始来了解基于tcp/ip协议层的Socket抽 ...

  6. Android基础总结(8)——服务

    服务(Service)是Android中实现程序后台运行的解决方案,它非常适合用于去执行哪些不需要和用户交互而且还要长期运行的任务.服务的运行不依赖任何用户界面,即使当程序被切换到后台,或者用户打开了 ...

  7. 【Xamarin开发 Android 系列 4】 Android 基础知识

    原文:[Xamarin开发 Android 系列 4] Android 基础知识 什么是Android? Android一词的本义指“机器人”,同时也是Google于2007年11月5日宣布的基于Li ...

  8. 【Android 应用开发】Android 网络编程 API笔记 - java.net 包 权限 地址 套接字 相关类 简介

    Android 网络编程相关的包 : 9 包, 20 接口, 103 类, 6 枚举, 14异常; -- Java包 : java.net 包 (6接口, 34类, 2枚举, 12异常); -- An ...

  9. 【Android 应用开发】Android 网络编程 API笔记 - java.net 包相关 接口 api

    Android 网络编程相关的包 : 9 包, 20 接口, 103 类, 6 枚举, 14异常; -- Java包 : java.net 包 (6接口, 34类, 2枚举, 12异常); -- An ...

  10. Android并发编程 多线程与锁

    该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,如果能给各位看官带来一丝启发或者帮助,那真是极好的. 前言 前一篇Android并发编程开篇呢,主要是简单介绍一下线程以及 ...

随机推荐

  1. win10环境下Android studio安装教程----亲测可用

    这段时间学习了一下Android的基本开发,发现Google已经停止了对eclipse的支持,并开发了自己的Android开发工具--Android Studio,于是想安装一下Android Stu ...

  2. day28元类与异常查找

    元类与异常处理1. 什么是异常处理    异常是错误发生的信号,一旦程序出错就会产生一个异常,如果该异常    没有被应用程序处理,那么该异常就会抛出来,程序的执行也随之终止    异常包含三个部分: ...

  3. leetcode14:最长公共字符串

    编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...

  4. Laravel线上布暑到linux的问题汇总

    1.直接报403错误 ,配置文件中增加: location / { try_files $uri $uri/ /index.php?$query_string; } 同时根目录不是指到项目名,而是指到 ...

  5. java新手抖机灵(java新手技巧)

    java新手抖机灵(java新手技巧) 1.交换两个整数的值 好处是不用定义临时变量,显得代码简洁,提高运行效率 其实也可以用+-*/进行这种运算 比如可以这样: a = a + b; b = a - ...

  6. Adjacent Bit Counts(01组合数)

    Adjacent Bit Counts 4557 Adjacent Bit CountsFor a string of n bits x 1 , x 2 , x 3 ,..., x n , the a ...

  7. LevelDB源码分析-TableBuilder生成sstable

    TableBuilder生成sstable(include/table_builder.h table/table_builder.cc) LevelDB使用TableBuilder来构建sstabl ...

  8. Sql Server数据库之事务,视图,索引

    一.事务的定义 事务是一种机制,包含一组操作指令,并将所有的命令作为一个整体一起向系统提交或撤销操作请求(要么都执行,要么都不执行) 二.事务的分类 显式事务:用Begin TRANSCATION开始 ...

  9. EF连接Mysql 表'TableDetails'中的列'IsPrimaryKey'的值为DBNull

    无法生成模型,因为存在以下异常:'System.Data.StrongTypingException:表'TableDetails'中的列'IsPrimaryKey'的值为DBNull.---> ...

  10. mongodb安装及配置

    下载安装篇 MongoDB 提供了 linux 各发行版本 64 位的安装包,你可以在官网下载安装包. 下载地址:https://www.mongodb.com/download-center#com ...