android开发如何实现折叠菜单类似qq分组?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android开发如何实现折叠菜单类似qq分组?相关的知识,希望对你有一定的参考价值。
用ExpandableListView来实现,可以设置其中的子ListView是展开还是闭合一、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;
参考自:http://blog.csdn.net/gyflyx/article/details/6461242 参考技术A 这个用listview就可以,在adapter的getview中判断显示的级别加载不同的item就可以达到qq的效果,至于刷新就是刷新数据,也就是adapter加载的list集合,让adapter刷新下就可以。
js仿qq分组折叠效果
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <style type="text/css"> ul { list-style-type: none; } .header { background-color: red; cursor: pointer; } .body { background-color: greenyellow; border-style: solid; border-width: 1px; } </style> <script src="../Scripts/jquery-1.8.2.js"></script> <script type="text/javascript"> $(function() { $("#qq li[name=grp]").addClass("header").click(function () { $(this).nextUntil("li[name=grp]").show("slow"); $(this).siblings("li[name=grp]").nextUntil("li[name=grp]").hide("slow"); }); $("#qq li:not([name=grp])").addClass("body"); $("#qq li:frist").click(); }); </script> </head> <body> <ul id="qq"> <li name="grp">我的好友</li> <li >张三</li><li>李四</li> <li name="grp">我的同学</li> <li>赵三</li><li>同学2</li> <li name="grp">陌生人</li> <li>陌生人1</li><li>陌生人2</li><li>陌生人3</li> </ul> </body> </html>
效果图:
以上是关于android开发如何实现折叠菜单类似qq分组?的主要内容,如果未能解决你的问题,请参考以下文章
QQ通讯录2.2 android版 怎么给联系人分组??大神们帮帮忙
winform, ListView Groups 下面的项目如何实现折叠,展开(默认都是展开) 类似treeview的效果