1.简介
基于BaseExpandableListAdapter扩展的ExpandableList用法,现在网上流行的主要有两种:第一种是向BaseExpandableListAdapter传入两个数组,第一个是表示Group(目录头)信息的一维数组,第二个是表示Child(目录子项)的二维数组数组;第二种是构建两个类,一个是表示目录信息的GroupInfo类,另一个是表示子项信息的ChildInfo类,然后传入BaseExpandableListAdapter。通过对比发现,第一种方法由于数组是固定的,而实际项目中往往需要动态变化的目录和子项,因此用处不大,第二种方法文件太多,实现复杂。这里提供一种方法,传递两个动态的二维数组来实现目录结构。
2.案例

package com.devin;
import java.util.ArrayList;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;public class PadTestActivity extends Activity {protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);ArrayList<String> groupList = new ArrayList<String>();for (int i = 0; i < 3; i++) {groupList.add("title");}ArrayList<String> itemList1 = new ArrayList<String>();itemList1.add("Item1");itemList1.add("Item2");ArrayList<String> itemList2 = new ArrayList<String>();itemList2.add("Item1");itemList2.add("Item2");itemList2.add("Item3");ArrayList<String> itemList3 = new ArrayList<String>();itemList3.add("Item1");itemList3.add("Item2");itemList3.add("Item3");itemList3.add("Item4");ArrayList<ArrayList<String>> childList = new ArrayList<ArrayList<String>>();childList.add(itemList1);childList.add(itemList2);childList.add(itemList3);ExpandableListView list = new ExpandableListView(this);ExpandableListAdapter mAdapter = new MyExpandableListAdapter(groupList, childList);list.setAdapter(mAdapter);list.setCacheColorHint(0x00000000);list.setSelector(new ColorDrawable(Color.TRANSPARENT));list.setGroupIndicator(null);for (int i = 0; i < mAdapter.getGroupCount(); i++) {list.expandGroup(i);}setContentView(list);}private class MyExpandableListAdapter extends BaseExpandableListAdapter {private ArrayList<String> groupList;private ArrayList<ArrayList<String>> childList;MyExpandableListAdapter(ArrayList<String> groupList, ArrayList<ArrayList<String>> childList) {this.groupList = groupList;this.childList = childList;}public Object getChild(int groupPosition, int childPosition) {return childList.get(groupPosition).get(childPosition);}private int selectedGroupPosition = -1;private int selectedChildPosition = -1;public void setSelectedPosition(int selectedGroupPosition, int selectedChildPosition) {this.selectedGroupPosition = selectedGroupPosition;this.selectedChildPosition = selectedChildPosition;}public long getChildId(int groupPosition, int childPosition) {return childPosition;}public int getChildrenCount(int groupPosition) {return childList.get(groupPosition).size();}public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {TextView textView = null;if (convertView == null) {textView = new TextView(PadTestActivity.this);textView.setPadding(32, 10, 0, 10);convertView = textView;} else {textView = (TextView) convertView;}textView.setText(getChild(groupPosition, childPosition).toString());if (groupPosition == selectedGroupPosition) {if (childPosition == selectedChildPosition) {textView.setBackgroundColor(0xffb6ddee);} else {textView.setBackgroundColor(Color.TRANSPARENT);}}textView.setOnClickListener(new OnClickListener() {public void onClick(View v) {setSelectedPosition(groupPosition, childPosition);notifyDataSetChanged();}});return textView;}public Object getGroup(int groupPosition) {return groupList.get(groupPosition);}public int getGroupCount() {return groupList.size();}public long getGroupId(int groupPosition) {return groupPosition;}public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {LinearLayout cotain = new LinearLayout(PadTestActivity.this);cotain.setPadding(0, 10, 0, 10);cotain.setGravity(Gravity.CENTER_VERTICAL);ImageView imgIndicator = new ImageView(PadTestActivity.this);TextView textView = new TextView(PadTestActivity.this);textView.setText(getGroup(groupPosition).toString());textView.setPadding(5, 0, 0, 0);if (isExpanded) {imgIndicator.setBackgroundResource(R.drawable.macro_minus);} else {imgIndicator.setBackgroundResource(R.drawable.macro_plus);}cotain.addView(imgIndicator);cotain.addView(textView);return cotain;}public boolean hasStableIds() {return true;}public boolean isChildSelectable(int groupPosition, int childPosition) {return true;}}
}

上述代码中,过向BaseExpandableListAdapter传递两个动态数组groupList(表示目录头信息)和childList(表示子项信息)来构建目录,一方面能够实现动态的添加数据,另一方面简化了实现,一举两得。另外,重写的BaseExpandableListAdapter,如果应用在实际项目中,需要对getGroupView()和getChildView()方法进行构建缓存(和ListView构建一样),以便优化性能和防止内存泄漏。

转自:http://www.cnblogs.com/devinzhang/archive/2012/07/18/2596998.html

ExpandableList扩展用法相关推荐

  1. Android ExpandableList扩展用法

    1.简介 基于基于BaseExpandableListAdapter扩展的ExpandableList用法,现在网上流行的主要有两种:第一种是向BaseExpandableListAdapter传入两 ...

  2. Android之ExpandableList扩展用法(基于BaseExpandableListAdapter)

    1.简介 基于基于BaseExpandableListAdapter扩展的ExpandableList用法,现在网上流行的主要有两种:第一种是向BaseExpandableListAdapter传入两 ...

  3. C语言offsetof用法以及其扩展用法

    标题C语言offsetof用法以及其扩展用法 offsetof由于不是标准库的函数,所以得查一下,在stddef.h中,搜索一下编译器的这个头文件位置: 暴力一点,直接在根目录下搜索,find -na ...

  4. 【Kotlin】扩展接收者 与 分发接收者 ( 类内部扩展用法 | 注意事项 | open 修饰扩展 )

    文章目录 I . 类内部扩展其它类 II . 扩展接收者 与 分发接收者 注意事项 III . open 修饰 分发接收者 类型中的扩展 I . 类内部扩展其它类 1 . 扩展函数 / 属性声明的位置 ...

  5. ode45的常用和扩展用法

    写在开头:本文是ode45基础及扩展篇: 1.介绍ode45使用方法.使用技巧: 2.介绍ode45的算法原理,促进理解. 一.简单介绍: 1.先直接引用百度百科上的内容(为了避免重复造轮子) 2.四 ...

  6. [Excel]COUNTIF()函数使用实例以及扩展用法——根据区域是否包含某个字符进行操作

    函数介绍 计算某个区域中满足给定条件的个数 =countif(range,criteria) range是区域,criteria是判断条件 实例 1.计算一个区域包含某个字符的个数 公式:=COUNT ...

  7. 安卓实现ExpandableList中子项不同的布局

    最近学习了ExpandableList的用法,并用之实现子项布局不同 实现目标如下:1.主界面由两个list组成2.这两个list均可以展开,即有自己的子布局3.子布局的布局不同  下面看效果图: 实 ...

  8. python lambda匿名函数 用法

    语法 lambda argument_list: expression argument_list是参数列表 expression是一个关于参数的表达式.表达式中出现的参数需要在argument_li ...

  9. python用变量输出abcd_python中星号变量的几种特殊用法

    一.什么是星号变量 最初,星号变量是用在函数的参数传递上的,在下面的实例中,单个星号代表这个位置接收任意多个非关键字参数,在函数的*b位置上将其转化成元组,而双星号代表这个位置接收任意多个关键字参数, ...

最新文章

  1. 找出一个数组中唯一一个出现2次的数字
  2. PostgreSQL+安装及常见问题
  3. Dollar toolbox 学习笔记(一)
  4. 辗转相除求最大公约数最小公倍数 扩展欧几里得算法
  5. 马踏棋盘python_马踏棋盘python实现
  6. 第2章:Maven的安装/2.1 Window下的安装
  7. 安装更新Lenovo Solution Center更新失败!具体问题看内容!要是等官方技术人员解决,估计要等上好一段时间!...
  8. python基础01day
  9. 使用POI完成excel文件导出
  10. 阿里云 CentOS7.9 搭建 Hexo 个人博客教程
  11. [LED]如何配置LCD背光和LED,调试方法
  12. Python数据结构15:turtle模块制图,画直线,正方形,星星,递归可视化:分形树,谢尔宾斯基三角形
  13. 输入字符串“I am a student”,要求输出字符串“student a am I”
  14. 浏览器缓存知识+JS实现缓存
  15. Vivado与modelsim联合仿真(2018.3---10.6c)
  16. SQLite Expert 5.X 通用注册版-你的SQL好帮手
  17. halcon 相机标定
  18. 诺基亚S40的theme2.0的nth主题
  19. oracle erp闪退,ORACLE ERP系统经常出现的问题及解决办法
  20. Excel通过宏 实现数据的填充和表格的复制

热门文章

  1. Python基础(三)列表、元组、字典、集合
  2. 高斯判别算法GDA(吴恩达机器学习实践总结,四)
  3. wifi卡慢延迟高_卡顿缓冲真头疼 这几招能加速你的WiFi
  4. JDK(1.6、1.7、1.8、10、11) 绿色免安装版 Windows 版
  5. fume fx 汉化_FumeFX中文版下载 流体动力学插件FumeFX 5.x for 3DS MAX 2019 免费版(附安装教程) 下载-脚本之家...
  6. 可能是花了太久写小游戏,脑子被榨干了,今天就不淫诗了——流程控制之if判断、while循环、for循环,第七天
  7. iSpring Suite教程:使用iSpring创建视频讲座只需简单6步(上)
  8. 比经济萧条更可怕:男人都不追女人了!
  9. Commvault+XSKY 推出基于 Object Lock 的防勒索病毒联合方案
  10. 零基础快速搭建私人影音媒体平台