ExpandableListView是android中可以实现下拉list的一个控件,是一个垂直滚动的心事两个级别列表项手风琴试图,列表项是来自ExpandableListViewaAdapter,组可以单独展开。

重要方法:

 expandGroup (int groupPos) ;//在分组列表视图中 展开一组,
setSelectedGroup (int groupPosition) ;//设置选择指定的组。 setSelectedChild (int groupPosition, int childPosition, boolean shouldExpandGroup);//设置选择指定的子项。 getPackedPositionGroup (long packedPosition);//返回所选择的组 getPackedPositionForChild (int groupPosition, int childPosition) ;//返回所选择的子项 getPackedPositionType (long packedPosition);//返回所选择项的类型(Child,Group) isGroupExpanded (int groupPosition);//判断此组是否展开

expandableListView.setDivider();这个是设定每个Group之间的分割线。

expandableListView.setGroupIndicator();这个是设定每个Group之前的那个图标。

expandableListView.collapseGroup(int group); 将第group组收起

ExpandableListAdapter

一个接口,将基础数据链接到一个ExpandableListView。 此接口的实施将提供访问Child的数据(由组分类),并实例化的Child和Group。

1.重要方法

    getChildId (int groupPosition, int childPosition) 获取与在给定组给予孩子相关的数据。

    getChildrenCount (int groupPosition) 返回在指定Group的Child数目。

案例:

首先定义个一个布局文件expandablelistview.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ExpandableListView
android:id ="@+id/expandableListView"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
>
</ExpandableListView>
</LinearLayout>
 package com.test;

 import java.util.ArrayList;
import java.util.List; import javax.security.auth.PrivateCredentialPermission; import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView; public class ExpandableListViewDemo extends Activity {
/** Called when the activity is first created. */ //定义两个List用来控制Group和Child中的String; private List<String> groupArray;//组列表
private List<List<String>> childArray;//子列表
private ExpandableListView expandableListView_one; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE); //设置为无标题
setContentView(R.layout.expandablelistview);
expandableListView_one =(ExpandableListView)findViewById(R.id.expandableListView);
groupArray =new ArrayList<String>();
childArray = new ArrayList<List<String>>(); /*-第一季-*/
initdate();
expandableListView_one.setAdapter(new ExpandableListViewaAdapter(ExpandableListViewDemo.this)); /*-第二季-*/
// groupArray.add("移动开发");
// List<String> arrayList = new ArrayList<String>();
// arrayList.add("Android");
// arrayList.add("IOS");
// arrayList.add("Windows Phone");
// //组循环
// for(int index=0;index<groupArray.size();++index)
// {
// childArray.add(arrayList);
// }
// expandableListView_one.setAdapter(new ExpandableListViewaAdapter(ExpandableListViewDemo.this)); }
class ExpandableListViewaAdapter extends BaseExpandableListAdapter {
Activity activity;
public ExpandableListViewaAdapter(Activity a)
{
activity = a;
}
/*-----------------Child */
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childArray.get(groupPosition).get(childPosition);
} @Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
} @Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) { String string =childArray.get(groupPosition).get(childPosition); return getGenericView(string);
} @Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return childArray.get(groupPosition).size();
}
/* ----------------------------Group */
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return getGroup(groupPosition);
} @Override
public int getGroupCount() {
// TODO Auto-generated method stub
return groupArray.size();
} @Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
} @Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) { String string=groupArray.get(groupPosition);
return getGenericView(string);
} @Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
} @Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
// TODO Auto-generated method stub
return true;
} private TextView getGenericView(String string )
{
AbsListView.LayoutParams layoutParams =new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT); TextView textView =new TextView(activity);
textView.setLayoutParams(layoutParams); textView.setGravity(Gravity.CENTER_VERTICAL |Gravity.LEFT); textView.setPadding(40, 0, 0, 0);
textView.setText(string);
return textView;
}
} private void initdate()
{
addInfo("语言", new String[]{"Oracle","Java","Linux","Jquery"});
addInfo("男人的需求", new String[]{"金钱","事业","权力","女人","房子","车","球"});
}
private void addInfo(String group,String []child) { groupArray.add(group); List<String> childItem =new ArrayList<String>(); for(int index=0;index<child.length;index++)
{
childItem.add(child[index]);
}
childArray.add(childItem);
}
}

android ExpandableListView详解的更多相关文章

  1. Android Notification 详解(一)——基本操作

    Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...

  2. Android Notification 详解——基本操作

    Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...

  3. Android ActionBar详解

    Android ActionBar详解 分类: Android2014-04-30 15:23 1094人阅读 评论(0) 收藏 举报 androidActionBar   目录(?)[+]   第4 ...

  4. Android 签名详解

    Android 签名详解 AndroidOPhoneAnt设计模式Eclipse  在Android 系统中,所有安装 到 系统的应用程序都必有一个数字证书,此数字证书用于标识应用程序的作者和在应用程 ...

  5. Android编译系统详解(一)

    ++++++++++++++++++++++++++++++++++++++++++ 本文系本站原创,欢迎转载! 转载请注明出处: http://blog.csdn.net/mr_raptor/art ...

  6. Android布局详解之一:FrameLayout

      原创文章,如有转载,请注明出处:http://blog.csdn.net/yihui823/article/details/6702273 FrameLayout是最简单的布局了.所有放在布局里的 ...

  7. 【整理修订】Android.mk详解

    Android.mk详解 1. Android.mk 的应用范围 Android.mk文件是GNU Makefile的一小部分,它用来对Android程序进行编译. 一个Android.mk文件可以编 ...

  8. Android菜单详解(四)——使用上下文菜单ContextMenu

    之前在<Android菜单详解(二)——创建并响应选项菜单>和<Android菜单详解(三)——SubMenu和IconMenu>中详细讲解了选项菜单,子菜单和图标菜单.今天接 ...

  9. Android签名详解(debug和release)

    Android签名详解(debug和release)   1. 为什么要签名 1) 发送者的身份认证 由于开发商可能通过使用相同的Package Name来混淆替换已经安装的程序,以此保证签名不同的包 ...

随机推荐

  1. 用jinja做了个E-Letter小项目

    做了一个html E-Letter项目. 邮件模板采用jinja2, html 邮件内容生成简直太爽了. 整个项目开发只用了2个小时, 调试却花了大半天时间, 生成的邮件总是发不出去. 于是, 打开 ...

  2. 【C语言入门教程】2.6 运算符

    运算符是程序中用于数值运算的操作符,C 语言的运算符可分为 算术运算符.关系与逻辑运算符 和 位操作运算符 这 3 类. 2.6.1 算术运算符 算术运算符用来完成基本的数值运算,如 加.减.乘.除, ...

  3. 个人建了一个APPCAN移动前端开发交流QQ群258213194

    QQ群号:258213194,欢迎有兴趣的同志加一下. 二维码如下:

  4. js实现自定义右键菜单--兼容IE、Firefox、Chrome

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  5. 常用的Git Tips

    导读 Git被越来越多的公司使用,因此我们需要了解Git使用过程中的一些技巧. 一.Configuration:配置 列举所有的别名与配置 git config --list Git 别名配置 git ...

  6. Method Swizzling和AOP(面向切面编程)实践

    Method Swizzling和AOP(面向切面编程)实践 参考: http://www.cocoachina.com/ios/20150120/10959.html 上一篇介绍了 Objectiv ...

  7. Leonbao:MapKit学习笔记

    以下仅作了解, 实际使用以百度地图居多, 因为百度地图有动态路径规划等接口 MapKit学习笔记    原帖: http://www.cocoachina.com/bbs/read.php?tid-6 ...

  8. Sqli-LABS通关笔录-18-审计SQL注入2-HTTP头注入

     在此关卡我学习到了 1.只要跟数据库交互的多观察几遍.特别是对于http头这种类型的注入方式. 2. <?php //including the Mysql connect parameter ...

  9. Sound Generator 原理

    Sound Generator 原理 旨在简单的阐述声音如何通过单片机模块来产生. 声音 声音的种类有千千万,但归根到底还是属于波.在对声音进行模拟转数字的时候,采样越高, 声音被还原的越逼真. 声音 ...

  10. eclipse中整合springMvc和velocity

    1.项目所需要的jar包(有些可能多余) 2.在src目录下创建一个bean  一个一个controller ,路径如下 person代码: package com.test.bean; import ...