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. JavaScript Document
  2. Linux简单的http服务器:SimpleHTTPServer
  3. Access中出现改变字段“自己主动编号”类型,不能再改回来!(已解决)
  4. JavaScript面试时候的坑洼沟洄——表达式与运算符
  5. linux-centos7 常用的基本命令--目录管理、基本属性
  6. linux移植简介[MS2]
  7. eclipse console 输出数据量大时不完整问题
  8. javascript 遍历数组的常用方法(迭代、for循环 、for… in、for…of、foreach、map、filter、every、some,findindex)
  9. 【Bug】下载steam游戏的E盘莫名其妙爆满
  10. 好久没更新了,更新一篇,关于ZEC的吧
  11. 华为手机 图标消失_华为手机桌面图标不见了怎么办
  12. python图像锐化_(python 图像锐化教程)C 实现bmp图像锐化后,锐化的效果很差,求大神帮忙啊...
  13. redis读写分离之lettuce
  14. MySQL实战45讲读后感:一条SQL查询语句是如何执行的?
  15. 蓝桥杯——大臣的旅费
  16. 【软件之道】Word模板的制作及使用
  17. 软件黑盒测试心得与经验
  18. 给初学编程的业余爱好者——会堆积木就会编程
  19. QT数据库访问技术简介
  20. oracle 杀掉spid,oracle 存储过程 sid spid 如果sid被杀掉了,spid是不也自动停止了?...

热门文章

  1. 项目管理之周报的好处
  2. 八位可控加法器logisim_八位二进制加法器
  3. wallop:前程似锦还是胎死腹中??
  4. MATLAB中的复杂矩阵输入问题
  5. admob 开屏广告来了!!!
  6. 源码编译freeswitch-1.10.7遇到问题总结
  7. 攻略丨一文速览2018世界机器人大会最全参会指南
  8. 双色球与大乐透号码生成器
  9. android 一键锁屏 开发
  10. 利用Tumblr Automatic Like软件日引英文流量2000IP+