Android中ExpandableListView的使用
ExpandableListView是Android中可以实现下拉list的一个控件,具体的实现方法如下:
首先:在layout的xml文件中定义一个ExpandableListView
- < LinearLayout
 - android:id ="@+id/linearLayout"
 - android:layout_width ="fill_parent"
 - android:layout_height ="fill_parent"
 - androidrientation ="vertical"
 - >
 - < ExpandableListView
 - android:id ="@+id/expandableListView"
 - android:layout_width ="fill_parent"
 - android:layout_height ="wrap_content"
 - />
 - </ LinearLayout >
 
定义两个List,用来存放控件中Group/Child中的String
- private List<String> groupArray;
 - private List<List<String>> childArray;
 
对这两个List进行初始化,并插入一些数据
- groupArray = new ArrayList<String>();
 - childArray = new ArrayList<List<String>>();
 - groupArray.add("第一行" );
 - groupArray.add("第二行" );
 - List<String> tempArray = new ArrayList<String>();
 - tempArray.add("第一条" );
 - tempArray.add("第二条" );
 - tempArray.add("第三条" );
 - for (int index = 0 ; index <groupArray.size(); ++index)
 - {
 - childArray.add(tempArray);
 - }
 
定义ExpandableListView的Adapter
- //ExpandableListView的Adapter
 - public class ExpandableAdapter extends BaseExpandableListAdapter
 - {
 - Activity activity;
 - public ExpandableAdapter(Activity a)
 - {
 - activity = a;
 - }
 - public Object getChild(int groupPosition, int childPosition)
 - {
 - return childArray.get(groupPosition).get(childPosition);
 - }
 - public long getChildId(int groupPosition, int childPosition)
 - {
 - return childPosition;
 - }
 - public int getChildrenCount(int groupPosition)
 - {
 - return childArray.get(groupPosition).size();
 - }
 - public View getChildView(int groupPosition, int childPosition,
 - boolean isLastChild, View convertView, ViewGroup parent)
 - {
 - String string = childArray.get(groupPosition).get(childPosition);
 - return getGenericView(string);
 - }
 - // group method stub
 - public Object getGroup(int groupPosition)
 - {
 - return groupArray.get(groupPosition);
 - }
 - public int getGroupCount()
 - {
 - return groupArray.size();
 - }
 - public long getGroupId(int groupPosition)
 - {
 - return groupPosition;
 - }
 - public View getGroupView(int groupPosition, boolean isExpanded,
 - View convertView, ViewGroup parent)
 - {
 - String string = groupArray.get(groupPosition);
 - return getGenericView(string);
 - }
 - // View stub to create Group/Children 's View
 - public TextView getGenericView(String string)
 - {
 - // Layout parameters for the ExpandableListView
 - AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
 - ViewGroup.LayoutParams.FILL_PARENT, 64 );
 - TextView text = new TextView(activity);
 - text.setLayoutParams(layoutParams);
 - // Center the text vertically
 - text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
 - // Set the text starting position
 - text.setPadding(36 , 0 , 0 , 0 );
 - text.setText(string);
 - return text;
 - }
 - public boolean hasStableIds()
 - {
 - return false ;
 - }
 - public boolean isChildSelectable(int groupPosition, int childPosition)
 - {
 - return true ;
 - }
 - }
 
最后,给定义好的ExpandableListView添加上Adapter
- ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.expandableListView);
 - expandableListView.setAdapter(new ExpandableAdapter(Main.this ));
 
运行即可见效果~~~
----------------------------------------------------------------------------------------------------------------
Android版手风琴(ExpandableListView)
先看效果,过瘾一番。




源码下载:http://files.cnblogs.com/salam/WidgetDemo.rar
ExpandableListView是Android中的手风琴,本人感觉效果相当棒。
一、ExpandableListView介绍
一个垂直滚动的显示两个级别(Child,Group)列表项的视图,列表项来自ExpandableListAdapter 。组可以单独展开。
1.重要方法
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) :判断此组是否展开
2.代码:
ExpandableListContextMenuInfo menuInfo=(ExpandableListContextMenuInfo)item.getMenuInfo();
  String title=((TextView)menuInfo.targetView).getText().toString();
  int type=ExpandableListView.getPackedPositionType(menuInfo.packedPosition);
  
  if (type==ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
  int groupPos =ExpandableListView.getPackedPositionGroup(menuInfo.packedPosition);
  int childPos =ExpandableListView.getPackedPositionChild(menuInfo.packedPosition);
二、ExpandableListAdapter
一个接口,将基础数据链接到一个ExpandableListView。 此接口的实施将提供访问Child的数据(由组分类),并实例化的Child和Group。
1.重要方法
getChildId (int groupPosition, int childPosition) 获取与在给定组给予孩子相关的数据。
getChildrenCount (int groupPosition) 返回在指定Group的Child数目。
2.代码
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
         // Sample data set.  children[i] contains the children (String[]) for groups[i].
         public String[] groups = { "我的好友", "新疆同学", "亲戚", "同事" };
         public String[][] children = {
                 { "胡算林", "张俊峰", "王志军", "二人" },
                 { "李秀婷", "蔡乔", "别高", "余音" },
                 { "摊派新", "张爱明" },
                 { "马超", "司道光" }
         };
         
         public Object getChild(int groupPosition, int childPosition) {
             return children[groupPosition][childPosition];
         }
public long getChildId(int groupPosition, int childPosition) {
             return childPosition;
         }
public int getChildrenCount(int groupPosition) {
             return children[groupPosition].length;
         }
public TextView getGenericView() {
             // Layout parameters for the ExpandableListView
             AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                     ViewGroup.LayoutParams.MATCH_PARENT, 64);
TextView textView = new TextView(ExpandableListDemo.this);
             textView.setLayoutParams(lp);
             // Center the text vertically
             textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
             // Set the text starting position
             textView.setPadding(36, 0, 0, 0);
             return textView;
         }
         
         public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                 View convertView, ViewGroup parent) {
             TextView textView = getGenericView();
             textView.setText(getChild(groupPosition, childPosition).toString());
             return textView;
         }
public Object getGroup(int groupPosition) {
             return groups[groupPosition];
         }
public int getGroupCount() {
             return groups.length;
         }
public long getGroupId(int groupPosition) {
             return groupPosition;
         }
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
                 ViewGroup parent) {
             TextView textView = getGenericView();
             textView.setText(getGroup(groupPosition).toString());
             return textView;
         }
public boolean isChildSelectable(int groupPosition, int childPosition) {
             return true;
         }
public boolean hasStableIds() {
             return true;
         }
}
Android中ExpandableListView的使用的更多相关文章
- Android中ExpandableListView控件基本使用
		
本文採用一个Demo来展示Android中ExpandableListView控件的使用,如怎样在组/子ListView中绑定数据源.直接上代码例如以下: 程序结构图: layout文件夹下的 mai ...
 - Android中ExpandableListView,每次只展示一个分组
		
// 只允许打开一个分组 expandListView.setOnGroupExpandListener(new OnGroupExpandListener() { @Override public ...
 - Android之ExpandableListView
		
ExpandableListView可以用来表现多层级的listView,本文主要是ExpandableListView的一个简单实现 布局文件 <LinearLayout xmlns:andr ...
 - Android中使用ExpandableListView实现微信通讯录界面(完善仿微信APP)
		
之前的博文<Android中使用ExpandableListView实现好友分组>我简单介绍了使用ExpandableListView实现简单的好友分组功能,今天我们针对之前的所做的仿微信 ...
 - Android中使用ExpandableListView实现好友分组
		
一个视图显示垂直滚动两级列表中的条目.这不同于列表视图,允许两个层次,类似于QQ的好友分组.要实现这个效果的整体思路为: 1.要给ExpandableListView 设置适配器,那么必须先设置数据源 ...
 - android中与Adapter相关的控件----ExpandableListView
		
ExpandableListView(可折叠的列表) 一.ExpandableListView(可折叠的列表)和ListView有很多地方差不多的,使用也差不多,只是他们使用适配器不一样的,Expan ...
 - Android 中常见控件的介绍和使用
		
1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.la ...
 - 22.Android之ExpandableListView树形列表学习
		
Android经常用到树形菜单,一般ExpandableListView可以满足这个需要,今天学习下. XML代码: <?xml version="1.0" encoding ...
 - Android 之 ExpandableListView 的使用
		
喜欢显示好友QQ那样的列表,可以展开,可以收起,在android中,以往用的比较多的是listview,虽然可以实现列表的展示,但在某些情况下,我们还是希望用到可以分组并实现收缩的列表,那就要用到an ...
 
随机推荐
- Bootstrap <基础三十>Well
			
Well 是一种会引起内容凹陷显示或插图效果的容器 <div>.为了创建 Well,只需要简单地把内容放在带有 class .well 的 <div> 中即可.下面的实例演示了 ...
 - robots笔记以免忘记
			
html头部标签写法: <meta name="robots" content="index,follow" /> content中的值决定允许抓取 ...
 - 观点:BPM已经过时了?
			
在这个信息爆炸的世界,业务转型通常是以数字系统和流程的方式发生. 根据Forrester的报告“软件必须丰富你的品牌”,人们要依靠这些技术实现期望的商业成果,比如品牌联系.客户服务创新.更好的产品和提 ...
 - vi技巧合集
			
VIM 技巧 match & replace match the whole word(eg: match printf but not snprintf/fprintf)You can us ...
 - 10款html5开发工具,实用+好用
			
利用HTML5工具不仅可以帮助设计师和开发者创建更具吸引力的网站,还能增加网站的可用性和可访问性.本文收集了10款HTML5开发工具让你在网页中搭建特效.动画.视频.音频等诸多功能,为你节省更多开发时 ...
 - 学习中的错误——ubuntu 14.04 LTS 启动eclipse报错
			
在ubuntu中启动eclipse报错:(Eclipse:15978): GLib-GIO-CRITICAL **: g_dbus_connection_get_unique_name: assert ...
 - 3、Android Intent Flag的介绍
			
介绍:http://blog.csdn.net/luckily01/article/details/7737499
 - 对于java.lang.NoSuchMethodError: antlr.collections.AST.getLine()I错误解决
			
在J2EE框架下开发web网站,这种问题经常遇到,只要我们网上搜一下,就可以看到很多版本的,我整理一下: 第一种可能性解决:看看我的项目:主要 是里面的Structs 1.3 (structs 2)和 ...
 - Number of 1 Bits(Difficulty: Easy)
			
题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...
 - mac OS X Yosemite (10.10.5) 下 安装vim 7.4笔记
			
摘要 前言 需求与mac OS X 自带vim版本的冲突 默认Python解释器问题 并非Mac自带python的 homebrew 1. 前言 本文为自己作死折腾的问题记录 2. 需求与mac OS ...