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来混淆替换已经安装的程序,以此保证签名不同的包 ...
随机推荐
- Java-开启Java之路
.NET还是我的最爱··· 准备学习下底层知识,为Java打打基础, 然后开始正式学习Java, 也算是曲线救国了, Go,Go,Go ... 二〇一六年十一月十日 18:09:54
- cas
cas配置ladp地址:
- R语言 recommenderlab 包
recommend li_volleyball 2016年3月20日 library(recommenderlab) ## Warning: package 'recommenderlab' was ...
- 2015Summer Training #2
暑假集训昨天刚开始,14级的小伙伴快到齐了,hhhhh ,毕竟下学期区域赛,对我们来说还是很困难的. 打算每天写份总结. UVA 11300 C.Spreading the Wealth 题目大意:n ...
- 【C语言入门教程】4.1 一维数组
数组与指针涉及到数据在内存中的存储位置问题,数组由连续的存储单元组成,最低地址对应于数组的第一个单元,最高地址对应于数组的最后一个单元.指针是一种特殊的变量,该变量所存放的是内存地址,通过指针变量可访 ...
- JAVA 如何使JScrollPane中的JTextArea自动滚动到最后一行?
1.要使JTextArea带有滚动条,需将JTextArea对象添加到JScrollPane中. JTextArea logArea = new JTextArea(15, 35); //创建JTex ...
- window 常用软件
参考链接: http://www.aiweibang.com/yuedu/721140.html http://www.aiweibang.com/yuedu/145263218.html 1.wox ...
- nginx反向代理、动静分离
环境:根据http://www.cnblogs.com/zzzhfo/p/6032095.html配置 方法一:根据目录实现动静分离 在web01创建image并上传一张图片作为静态页面 [root@ ...
- Leonbao:MapKit学习笔记
以下仅作了解, 实际使用以百度地图居多, 因为百度地图有动态路径规划等接口 MapKit学习笔记 原帖: http://www.cocoachina.com/bbs/read.php?tid-6 ...
- [codevs1157]2^k进制数
[codevs1157]2k进制数 试题描述 设r是个2k 进制数,并满足以下条件: (1)r至少是个2位的2k 进制数. (2)作为2k 进制数,除最后一位外,r的每一位严格小于它右边相邻的那一位. ...