在项目开发中,也许我们遇到过ListView中嵌套ListView,但谷歌建议我们最好别这样做,因此他们写好了一个ExpandableListView类,他继承ListView,可以实现ListView中嵌套ListView的效果,好了,废话不多说,先上效果图:

点击下载源码:仿QQ好友分组源代码


主代码:

[java] view plaincopy
  1. public class ExListView extends Activity {
  2. private static final String GROUP_TEXT = "group_text";//大组成员Map的key
  3. private static final String CHILD_TEXT1 = "child_text1";//小组成员Map的第一个key
  4. private static final String CHILD_TEXT2 = "child_text2";//小组成员Map的第二个key
  5. List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();//大组成员
  6. List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();//小组成员
  7. ExAdapter adapter;
  8. ExpandableListView exList;//可扩展的ListView
  9. @Override
  10. public void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.main);
  13. //为大小组中添加数据
  14. for (int i = 1; i < 6; i++) {
  15. Map<String, String> curGroupMap = new HashMap<String, String>();
  16. groupData.add(curGroupMap);
  17. curGroupMap.put(GROUP_TEXT, "第" + i + "大组");
  18. List<Map<String, String>> children = new ArrayList<Map<String, String>>();
  19. for (int j = 1; j < 6; j++) {
  20. Map<String, String> curChildMap = new HashMap<String, String>();
  21. children.add(curChildMap);
  22. curChildMap.put(CHILD_TEXT1, "第" + j + "小组");
  23. curChildMap.put(CHILD_TEXT2, "第" + j + "小组签名");
  24. }
  25. childData.add(children);
  26. }
  27. adapter = new ExAdapter(ExListView.this);
  28. exList = (ExpandableListView) findViewById(R.id.list);
  29. exList.setAdapter(adapter);
  30. exList.setGroupIndicator(null);//不设置大组指示器图标,因为我们自定义设置了
  31. exList.setDivider(null);//设置图片可拉伸的
  32. }
  33. //关键代码是这个可扩展的listView适配器
  34. class ExAdapter extends BaseExpandableListAdapter {
  35. Context context;
  36. public ExAdapter(Context context) {
  37. super();
  38. this.context = context;
  39. }
  40. //得到大组成员的view
  41. public View getGroupView(int groupPosition, boolean isExpanded,
  42. View convertView, ViewGroup parent) {
  43. View view = convertView;
  44. if (view == null) {
  45. LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  46. view = inflater.inflate(R.layout.member_listview, null);
  47. }
  48. TextView title = (TextView) view.findViewById(R.id.content_001);
  49. title.setText(getGroup(groupPosition).toString());//设置大组成员名称
  50. ImageView image = (ImageView) view.findViewById(R.id.tubiao);//是否展开大组的箭头图标
  51. if (isExpanded)//大组展开时
  52. image.setBackgroundResource(R.drawable.btn_browser2);
  53. else//大组合并时
  54. image.setBackgroundResource(R.drawable.btn_browser);
  55. return view;
  56. }
  57. //得到大组成员的id
  58. public long getGroupId(int groupPosition) {
  59. return groupPosition;
  60. }
  61. //得到大组成员名称
  62. public Object getGroup(int groupPosition) {
  63. return groupData.get(groupPosition).get(GROUP_TEXT).toString();
  64. }
  65. //得到大组成员总数
  66. public int getGroupCount() {
  67. return groupData.size();
  68. }
  69. // 得到小组成员的view
  70. public View getChildView(int groupPosition, int childPosition,
  71. boolean isLastChild, View convertView, ViewGroup parent) {
  72. View view = convertView;
  73. if (view == null) {
  74. LayoutInflater inflater = LayoutInflater.from(context);
  75. view = inflater.inflate(R.layout.member_childitem, null);
  76. }
  77. final TextView title = (TextView) view
  78. .findViewById(R.id.child_text);
  79. title.setText(childData.get(groupPosition).get(childPosition)
  80. .get(CHILD_TEXT1).toString());//大标题
  81. final TextView title2 = (TextView) view
  82. .findViewById(R.id.child_text2);
  83. title2.setText(childData.get(groupPosition).get(childPosition)
  84. .get(CHILD_TEXT2).toString());//小标题
  85. return view;
  86. }
  87. //得到小组成员id
  88. public long getChildId(int groupPosition, int childPosition) {
  89. return childPosition;
  90. }
  91. //得到小组成员的名称
  92. public Object getChild(int groupPosition, int childPosition) {
  93. return childData.get(groupPosition).get(childPosition).get(CHILD_TEXT1)
  94. .toString();
  95. }
  96. //得到小组成员的数量
  97. public int getChildrenCount(int groupPosition) {
  98. return childData.get(groupPosition).size();
  99. }
  100. /**
  101. * Indicates whether the child and group IDs are stable across changes to the
  102. * underlying data.
  103. * 表明大組和小组id是否稳定的更改底层数据。
  104. * @return whether or not the same ID always refers to the same object
  105. * @see Adapter#hasStableIds()
  106. */
  107. public boolean hasStableIds() {
  108. return true;
  109. }
  110. //得到小组成员是否被选择
  111. public boolean isChildSelectable(int groupPosition, int childPosition) {
  112. return true;
  113. }
  114. }
  115. }

主界面配置文件main.xml:

[html] view plaincopy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="@drawable/default_bg" >
  6. <ExpandableListView
  7. android:id="@+id/list"
  8. android:layout_width="fill_parent"
  9. android:layout_height="fill_parent"
  10. android:layout_alignParentLeft="true" android:cacheColorHint="#00000000" /><!-- 背景设置为透明,防止滑动时,白屏 -->
  11. </RelativeLayout>

大组成员配置文件member_listview.xml:

[html] view plaincopy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content"
  5. android:layout_gravity="center_horizontal" >
  6. <LinearLayout
  7. android:id="@+id/layout_013"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:orientation="horizontal" >
  11. <ImageView
  12. android:id="@+id/ImageView01"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:gravity="center_vertical"
  16. android:paddingTop="10dp"
  17. android:src="@drawable/user_group" >
  18. </ImageView>
  19. <RelativeLayout
  20. android:id="@+id/layout_013"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content" >
  23. <TextView
  24. android:id="@+id/content_001"
  25. android:layout_width="wrap_content"
  26. android:layout_height="fill_parent"
  27. android:layout_gravity="center_vertical"
  28. android:gravity="center_vertical"
  29. android:paddingLeft="10dp"
  30. android:textColor="#FFFFFF"
  31. android:textSize="30sp" >
  32. </TextView>
  33. <ImageView
  34. android:id="@+id/tubiao"
  35. android:layout_width="wrap_content"
  36. android:layout_height="wrap_content"
  37. android:layout_alignParentRight="true" >
  38. </ImageView>
  39. </RelativeLayout>
  40. </LinearLayout>
  41. <!--
  42. <RelativeLayout android:id="@+id/layout_013"
  43. android:layout_width="fill_parent"
  44. android:layout_height="fill_parent">
  45. <ImageView android:id="@+id/ImageView01"
  46. android:layout_width="wrap_content"
  47. android:layout_height="wrap_content"
  48. android:src="@drawable/icon"></ImageView>
  49. <TextView android:id="@+id/content_001"
  50. android:text="@+id/TextView01"
  51. android:layout_width="wrap_content"
  52. android:layout_toRightOf="@+id/ImageView01"
  53. android:layout_height="wrap_content"></TextView>
  54. <ImageView android:layout_width="wrap_content"
  55. android:layout_toRightOf="@+id/content_001"
  56. android:layout_height="wrap_content"
  57. android:id="@+id/tubiao"></ImageView>
  58. </RelativeLayout>
  59. -->
  60. </LinearLayout>

小组成员配置文件member_childitem.xml:

[html] view plaincopy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/childlayout"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:orientation="horizontal" >
  7. <ImageView
  8. android:id="@+id/child_image"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:layout_marginLeft="40dp"
  12. android:background="@drawable/child_image"
  13. android:paddingTop="10dp" >
  14. </ImageView>
  15. <LinearLayout
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:orientation="vertical" >
  19. <TextView
  20. android:id="@+id/child_text"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:layout_gravity="center_vertical"
  24. android:gravity="center_vertical"
  25. android:text=""
  26. android:textColor="#FFFFFF"
  27. android:textSize="25sp" >
  28. </TextView>
  29. <TextView
  30. android:id="@+id/child_text2"
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:layout_gravity="center_vertical"
  34. android:gravity="center_vertical"
  35. android:text=""
  36. android:textColor="#FFFFFF"
  37. android:textSize="20sp" >
  38. </TextView>
  39. </LinearLayout>
  40. </LinearLayout>
原文:http://blog.csdn.net/way_ping_li/article/details/7995552

Android之实现QQ好友分组(ExpandableListView)相关推荐

  1. Android 高仿QQ 好友分组列表

    实现的效果如下: 用ExpandableListView实现, 先看Activity的代码: public class BuddyActivity extends Activity {Expandab ...

  2. expandableListview的使用,模仿qq好友分组点击收缩扩展

    我主要讲述的是用listview实现.模仿qq好友分组点击收缩.扩展功能 这个是对listview的拓展,用法比较相似,还是需要一个适配器 MainActivitypublic class MainA ...

  3. android 仿qq好友动态,Android UI仿QQ好友列表分组悬浮效果

    本文实例为大家分享了Android UI仿QQ好友列表分组悬浮效果的具体代码,供大家参考,具体内容如下 楼主是在平板上測试的.图片略微有点大,大家看看效果就好 接下来贴源代码: PinnedHeade ...

  4. IOS 实现QQ好友分组展开关闭功能

    贴出核心代码  主要讲一下思路. - (void)nameBtnClick:(myButton *)sender { //获取当前点击的分组对应的section self.clickIndex = s ...

  5. Qt可拖拽排序表格(类似QQ好友分组排序)

    1,简介 为了最佳体验,一个拖拽行排序的功能研究了几个小时.效果参考的QQ好友分组的排序. 网上查了下好像没有人发布QT版类似的代码,于是自己动手 QQ好友分组排序效果: 2,效果 这是最终效果图,有 ...

  6. QQ好友分组模拟小程序

    QQ好友分组:一个好友组里有多个好友,一个好友只能选择一个组,这样好友组和好友之间就是一个一对多的关系.在此程序中封装一个好友类即Buddy类,一个组类即Group类.在Buddy类有有关好友的最基本 ...

  7. js实现qq好友分组

    qq好友分组 <style>ul,h2 {padding: 0;margin: 0;background-color: wheat;}li {list-style: none;}#list ...

  8. 模仿QQ好友分组风格

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"/> <title> ...

  9. UITableView的折叠收缩和QQ好友分组效果

    可折叠展开的tableView,QQ好友分组列表 demo下载地址https://github.com/zhengwenming/ExpandTableView   原理分析:这个可以折叠的table ...

最新文章

  1. 强化学习大规模应用还远吗?Youtube推荐已强势上线
  2. python 中的static-method (静态函数), classmethod(类函数 ), 成员函数
  3. vim 下web开发html css js插件
  4. c语言贪吃蛇咬到尾巴,【图片】C语言小游戏~贪吃蛇【c语言吧】_百度贴吧
  5. 王炸!Waymo正式官宣无人车出行平台,瑟瑟发抖的不止Uber
  6. python多进程用不了_python 多进程,实际上都没有运行,sleep
  7. php 手机号 去掉86,手机号前面的+86是什么意思
  8. 苹果电脑上不错的svn客户端
  9. UE4 编辑器模块引用,不影响打包处理
  10. 2016年不可错过的21个深度学习视频、教程和课程
  11. 陌生人交友软件有哪些?陌生人社交APP排名|良心推荐
  12. 小智机器人有初中课程吗_张小智
  13. 怪兽融合、神秘药水?《Monster Tamer》深度攻略,来不及解释快上车!
  14. 【工大SCIR】基于动态图交互网络的多意图口语语言理解框架
  15. 收藏|超实用的100个示波器基础知识问答
  16. [笔记分享] [RTC] Alarm内核驱动分析
  17. primo驱动启动顺序
  18. 京东上千页面搭建基石——CMS前后端分离演讲史读后感
  19. python 爬虫 智联招聘
  20. qmainwindow 背景充电_Qt 设置背景图片(背景图片可随意拉伸)

热门文章

  1. 毕业设计-一种基于 MATLAB 的指纹识别方法
  2. 开启win7 Aero效果
  3. java dozer_java开发工具类之Dozer的使用
  4. 眼动数据分析基础知识了解(人眼工作机制)
  5. k8s通过命令批量删除pod
  6. 计算机毕业设计php+mysql仓库管理系统
  7. 实分析斯坦恩中文版_如何评价Stein的实分析以及复分析翻译版本?
  8. CDH大数据平台搭建之JDK安装
  9. 大数据笔记--SparkSQL(第一篇)
  10. HDLBits-Circuits学习小结(六)移位寄存器(shift registers)