Android之ExpandableList扩展用法(基于BaseExpandableListAdapter)
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("Item21");
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构建一样),以便优化性能和防止内存泄漏。需要的朋友可以自己构建。
Android之ExpandableList扩展用法(基于BaseExpandableListAdapter)的更多相关文章
- Android 实现QQ扩展listview(expandlistview)
Android 实现QQ扩展listview(expandlistview) <?xml version="1.0" encoding="utf-8"?& ...
- android的logcat详细用法
Android日志系统提供了记录和查看系统调试信息的功能.日志都是从各种软件和一些系统的缓冲区中记录下来的,缓冲区可以通过 logcat 命 令来查看和使用. 使用logcat命令 你可以用 logc ...
- android的四层体系结构,基于mvc三层结构浅析
从多方面理解Android体系结构 1.以分层的方式来看Android 安卓体系结构分为四层. 首先看一下官方关于Android体系结构的图: 1).Linux Kernel:负责硬件的驱动程序.网络 ...
- 怎么通过activity里面的一个按钮跳转到另一个fragment(android FragmentTransaction.replace的用法介绍)
即:android FragmentTransaction.replace的用法介绍 Fragment的生命周期和它的宿主Activity密切相关,几乎和宿主Activity的生命周期一致,他们之间最 ...
- Android开发中Bundle用法包裹数据(转)
Android开发中Bundle用法包裹数据 Bundle的经典用法,包裹数据放入Intent中,目的在于传输数据. SDK 里是这样描述: A mapping from String values ...
- 【转】Android各种Adapter的用法
转自:http://my.oschina.net/u/658933/blog/372151 Android各种Adapter的用法 发表于5个月前(2015-01-27 10:56) 阅读(143 ...
- 高扩展的基于NIO的服务器架构(二)
接上文高扩展的基于NIO的服务器架构 Reactor模式 如下图所示,将不同事件的检测分离开,当一种事件发生时一个事件处理器EventHandler将通知与该事件处理相对应的专用工作线程 采用这种架构 ...
- 高扩展的基于NIO的服务器架构
当你考虑写一个扩展性良好的基于Java的服务器时,相信你会毫不犹豫地使用Java的NIO包.为了确保你的服务器能够健壮.稳定地运行,你可能会花大量的时间阅读博客和教程来了解线程同步的NIO selec ...
- android application类的用法
android application类的用法 Application是android系统Framework提供的一个组件,它是单例模式(singleton),即每个应用只有一个实例,用来存储系统的一 ...
随机推荐
- python 写一个类似于top的监控脚本
最近老板给提出一个需要,项目需求大致如下: 1.用树莓派作为网关,底层接多个ZigBee传感节点,网关把ZigBee传感节点采集到的信息通过串口接收汇总,并且发送给上层的HTTP Serve ...
- java.util包下面的类---------01---示意图
一直在使用util包下面的这些类,甚至有些没用过的,想要都去认识认识他们!也许在未来的一天可以用到! 图太大不好截图!部分没有截全!
- iOS 开发之RunLoop
概念 RunLoop 就像她的名字一样,就是跑环,就是一个死循环.是一个可以随时休眠,随时唤醒的死循环. 那么一个手机App为什么会一直运行?而且在接受到用户点击的时候,会做出反应?这些都离不开Run ...
- 使用JSTL在页面前的空行怎么去除?
解决的方法是:在每个JSP的头上加上一段代码 <%@ page trimDirectiveWhitespaces="true" %>
- CentOS6安装DaoCloud加速器
天朝的网,你又不是不懂.我最爱的红杏最近也用不了了.FUCK GFW. 在这,我们使用DaoCloud的加速器,打开网址 https://dashboard.daocloud.io/mirror 找到 ...
- vim 真是上瘾啊
再次更新 .vimrc " leaderlet mapleader = ","nnoremap <leader>ev :vsplit ~/.vimrc< ...
- Data Structure Binary Tree: Iterative Postorder Traversal
http://www.geeksforgeeks.org/iterative-postorder-traversal-using-stack/ #include <iostream> #i ...
- android OTA升级包制作【转】
本文转载自:http://www.thinksaas.cn/topics/0/445/445670.html 0.签名 java -Xmx2048m -jar out/host/linux-x86/f ...
- MyCat:开源分布式数据库中间件
mycat 的主要配置文件 schema.xml rule.xml server.xml 客户端连接mycat mysql -h192.168.1.1 -P8806 -uroot -pwangxiao ...
- castle windsor学习-----Registering components by conventions
注册多个组件 1.one-by-one注册组件可能是一项非常重复的工作,可以通过Classes或Types注册一组组件(你可以指定一些特定的特征) 三个步骤 注册多个类型通常采取以下结构 contai ...