ExpandableListView继承于ListView,但是它不同于ListView,它可以有多个Group,每一个Group里都可以有多个Child。
比如可以实现QQ好友栏里类似的功能。
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
/**
* @author wing
* @date 2011/8/15
*/
public class OwnExpandableListAdapter extends BaseExpandableListAdapter {
private String[] group;
private String[][] child;
private Context context;
public OwnExpandableListAdapter(String[] group,String[][] child,Context context)
{
this.group=group;
this.child=child;
this.context=context;
}
/**
* 获取Group中的一个Child的值
*/
@Override
public Object getChild(int groupPosition, int childPosition)
{
return child[groupPosition][childPosition];
}
/**
* 获取Group中的一个Child的ID,本人没有写
*/
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
/**
* Child的视图
*/
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
//每个Group的Child,用TextView表示,当然也可以用其他的例如button之类的控件
TextView textView=new TextView(context);
textView.setText(child[groupPosition][childPosition]);
textView.setPadding(36, 0, 0, 0);
return textView;
}
/**
* 某个Group的Child的数量
*/
@Override
public int getChildrenCount(int groupPosition) {
return child[groupPosition].length;
}
@Override
public Object getGroup(int arg0) {
return null;
}
/**
* Group的数目
*/
@Override
public int getGroupCount() {
return group.length;
}
@Override
public long getGroupId(int arg0) {
return 0;
}
/**
* Group的视图
*/
@Override
public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {
//每个Group也用TextView表示,当然也可以用其他的例如button之类的控件
TextView textView=new TextView(context);
textView.setText(group[groupPosition]);
textView.setPadding(36, 0, 0, 0);
return textView;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int arg0, int arg1) {
// TODO Auto-generated method stub
return false;
}
}
上面是实现的一个ExpandableListAdapter,用于给ExpandableListView填充数据。
可以看见每一个Group和Child都是用的TextView。当然也可以用Button。
然后再使用。
String[]group= {"我的好友","同事"};
String[][]child= {{"张三","李四","王五"},{"赵六","杨七","嘿嘿","哈哈"}};
expandableListView.setAdapter(new OwnExpandableListAdapter(group, child, this));
另外,其实ExpandableListView也可以实现HTML中下拉框的功能。
首先介绍几个方法:
expandableListView.setDivider();这个是设定每个Group之间的分割线。
expandableListView.setGroupIndicator();这个是设定每个Group之前的那个图标。
expandableListView.collapseGroup(int group); 将第group组收起
expandableListView.expandGroup(int group); 将第group组展开
那么如何实现HTML中下拉框类似的功能呢?
由于我做的时候使用的是项目素材。不方便截图。所以就给大家一点提示。
每个Group和每个Child都使用Button,然后设定自己的样式(button selector)。
然后,每个Button都添加自己的事件,Group Button点击时,展开列表。Child Button点击时,收缩列表,并且改变Group Button的内容即可。
这样就可以实现下拉框的功能了。而且根据Button selector,基本可以定义出自己的样式。
PS:
刚毕业工作,由于项目正在测试。自己终于肯花一点时间来写写博客了。要学习的东西很多,加油!
分享到:
相关推荐
android ExpandableListView三级菜单的使用,多级菜单下拉与隐藏,ExpandableListView的简单使用
本demo实现功能为:Expandablelistview子项checkBox先获取已选择状态,在次过程中可修改子项选择,最终提交选择子类的项,并打印相关数据
android Expandablelistview基本控件的使用,关注博客http://himici.com/
android ExpandableListView简单例子
Android ExpandableListView双层嵌套实现三级树形菜单
android ExpandableListView 四级树形目录 可无限延伸。
android ExpandableListView的group和child都有checkbox,而且group默认展开,点击后不收起child。
android中ExpandableListView嵌套GridView的使用
Android之ExpandableListView控件的使用
前几篇文章介绍了Listview,但在实际开发中也经常会用到多层的Listview来展示数据,比如qq中的好友展示,所以这张来了解一下ExpandableListview,基本思想与Listview大致是相同的,所以用起来会比较方便。
然而,最近我的项目需要在屏幕方面使用这两种类型的列表。新的屏幕是有一个可折叠列表(1组ExpandableListview)的和它下面的一个标准的ListView。复杂的问题,这些名单被要求处理两套不同的数据显示不同的布局。
android学习基础知识,不懂的童鞋看看,一定有收获的~
点击ExpandableListView头部时,子集动态加载。已有子集不重新加载。 ExpandableListView滚动时,头部孤独。 IphoneTreeView功能
最近开发项目遇到了一个分组的列表,我以为是第三方的,在网上一搜原来是原生的,所以写了一个例子,上传到这,和大家交流学习
在android中,以往用的比较多的是listview,虽然可以实现列表的展示,但在某些情况下,我们还是希望用到可以分组并实现收缩的列表,那就要用到android的ExpandableListView
Android ExpandableListView组件与PopupWindow组件的完美结合,数据来源于json。
CommentWithReplyView-master 基于ExpandableListView实现评论和回复的功能。 > 说明 ...处理了NestedScrollView、ExpandableListView和CoordinatorLayout的嵌套问题 点击某条评论,即可@ta进行回复
包括listview ExpandableListView有悬停标示,
基于Android中的ExpandableListView开发示例, 应用list集合类型