android ExpandableListView详解
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详解的更多相关文章
- Android Notification 详解(一)——基本操作
Android Notification 详解(一)--基本操作 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/Notification 文中如有纰 ...
- Android Notification 详解——基本操作
Android Notification 详解 版权声明:本文为博主原创文章,未经博主允许不得转载. 前几天项目中有用到 Android 通知相关的内容,索性把 Android Notificatio ...
- Android ActionBar详解
Android ActionBar详解 分类: Android2014-04-30 15:23 1094人阅读 评论(0) 收藏 举报 androidActionBar 目录(?)[+] 第4 ...
- Android 签名详解
Android 签名详解 AndroidOPhoneAnt设计模式Eclipse 在Android 系统中,所有安装 到 系统的应用程序都必有一个数字证书,此数字证书用于标识应用程序的作者和在应用程 ...
- Android编译系统详解(一)
++++++++++++++++++++++++++++++++++++++++++ 本文系本站原创,欢迎转载! 转载请注明出处: http://blog.csdn.net/mr_raptor/art ...
- Android布局详解之一:FrameLayout
原创文章,如有转载,请注明出处:http://blog.csdn.net/yihui823/article/details/6702273 FrameLayout是最简单的布局了.所有放在布局里的 ...
- 【整理修订】Android.mk详解
Android.mk详解 1. Android.mk 的应用范围 Android.mk文件是GNU Makefile的一小部分,它用来对Android程序进行编译. 一个Android.mk文件可以编 ...
- Android菜单详解(四)——使用上下文菜单ContextMenu
之前在<Android菜单详解(二)——创建并响应选项菜单>和<Android菜单详解(三)——SubMenu和IconMenu>中详细讲解了选项菜单,子菜单和图标菜单.今天接 ...
- Android签名详解(debug和release)
Android签名详解(debug和release) 1. 为什么要签名 1) 发送者的身份认证 由于开发商可能通过使用相同的Package Name来混淆替换已经安装的程序,以此保证签名不同的包 ...
随机推荐
- [原] Intellij IDEA开发Android,祝还在使用eclipse的早日脱离苦海
注: 现在推荐使用Android Studio,以后google在Android Studio上个性差异化的东西越来越多, 所以越早使用Android Studio越好,看看更新文档,使我们开发更方便 ...
- JS仿Android Toast提示效果
注:这个需要jquery文件来提示支持,所以需要先调用Jquery. <script type="text/javascript" src="js/jquery.j ...
- PHP的 Mysqli扩展库的多语句执行
$mysqli->multi_query($sqls); 执行多个sql语句,返回true/false 有结果集时,使用 $mysqli->store_result(); 来获取结 ...
- MySQL事物控制
有时候我们需要保证事物的各个步骤都执行成功的前提下才能让每一步骤的事物执行,此时就需要事物控制. 事物控制用于保证数据的一致性,它由一组相关的dml语句组成,该组的dml语句要么全部成功,要么全部失败 ...
- WPF:依赖属性的应用
依赖属性与一般属性相比,提供了对资源引用.样式.动画.数据绑定.属性值继承.元数据重载以及WPF设计器的继承支持功能的支持. 下面的这个Demo来自<葵花宝典--WPF自学手册>. 1.M ...
- servlet和http请求
1.servlet servlet是和平台无关的服务器组件,可以交互式的来浏览和修改数据,生成动态的web内容.它运行于 servlet容器中2.servlet容器 servlet容器负责servle ...
- ajax读取XML文本(如读取城市)
//加载城市 function loadArea_pep() { $.ajax({ url: "/xmlFile/crty.xml", success: function (res ...
- CentOS 6.5 安装nginx 1.6.3
使用epel [root@nginx /]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo ...
- Effective Java 读书笔记之七 通用程序设计
一.将局部变量的作用域最小化 1.在第一次使用变量的地方声明 2.几乎每个变量的声明都应该包含一个初始化表达式:try-catch语句是一个例外 3.使方法小而集中是一个好的策略 二.for-each ...
- [POJ2892]Tunnel Warfare
[POJ2892]Tunnel Warfare 试题描述 During the War of Resistance Against Japan, tunnel warfare was carried ...