Android之实现QQ好友分组(ExpandableListView)
在项目开发中,也许我们遇到过ListView中嵌套ListView,但谷歌建议我们最好别这样做,因此他们写好了一个ExpandableListView类,他继承ListView,可以实现ListView中嵌套ListView的效果,好了,废话不多说,先上效果图:
点击下载源码:仿QQ好友分组源代码
主代码:
- public class ExListView extends Activity {
- private static final String GROUP_TEXT = "group_text";//大组成员Map的key
- private static final String CHILD_TEXT1 = "child_text1";//小组成员Map的第一个key
- private static final String CHILD_TEXT2 = "child_text2";//小组成员Map的第二个key
- List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();//大组成员
- List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();//小组成员
- ExAdapter adapter;
- ExpandableListView exList;//可扩展的ListView
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //为大小组中添加数据
- for (int i = 1; i < 6; i++) {
- Map<String, String> curGroupMap = new HashMap<String, String>();
- groupData.add(curGroupMap);
- curGroupMap.put(GROUP_TEXT, "第" + i + "大组");
- List<Map<String, String>> children = new ArrayList<Map<String, String>>();
- for (int j = 1; j < 6; j++) {
- Map<String, String> curChildMap = new HashMap<String, String>();
- children.add(curChildMap);
- curChildMap.put(CHILD_TEXT1, "第" + j + "小组");
- curChildMap.put(CHILD_TEXT2, "第" + j + "小组签名");
- }
- childData.add(children);
- }
- adapter = new ExAdapter(ExListView.this);
- exList = (ExpandableListView) findViewById(R.id.list);
- exList.setAdapter(adapter);
- exList.setGroupIndicator(null);//不设置大组指示器图标,因为我们自定义设置了
- exList.setDivider(null);//设置图片可拉伸的
- }
- //关键代码是这个可扩展的listView适配器
- class ExAdapter extends BaseExpandableListAdapter {
- Context context;
- public ExAdapter(Context context) {
- super();
- this.context = context;
- }
- //得到大组成员的view
- public View getGroupView(int groupPosition, boolean isExpanded,
- View convertView, ViewGroup parent) {
- View view = convertView;
- if (view == null) {
- LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- view = inflater.inflate(R.layout.member_listview, null);
- }
- TextView title = (TextView) view.findViewById(R.id.content_001);
- title.setText(getGroup(groupPosition).toString());//设置大组成员名称
- ImageView image = (ImageView) view.findViewById(R.id.tubiao);//是否展开大组的箭头图标
- if (isExpanded)//大组展开时
- image.setBackgroundResource(R.drawable.btn_browser2);
- else//大组合并时
- image.setBackgroundResource(R.drawable.btn_browser);
- return view;
- }
- //得到大组成员的id
- public long getGroupId(int groupPosition) {
- return groupPosition;
- }
- //得到大组成员名称
- public Object getGroup(int groupPosition) {
- return groupData.get(groupPosition).get(GROUP_TEXT).toString();
- }
- //得到大组成员总数
- public int getGroupCount() {
- return groupData.size();
- }
- // 得到小组成员的view
- public View getChildView(int groupPosition, int childPosition,
- boolean isLastChild, View convertView, ViewGroup parent) {
- View view = convertView;
- if (view == null) {
- LayoutInflater inflater = LayoutInflater.from(context);
- view = inflater.inflate(R.layout.member_childitem, null);
- }
- final TextView title = (TextView) view
- .findViewById(R.id.child_text);
- title.setText(childData.get(groupPosition).get(childPosition)
- .get(CHILD_TEXT1).toString());//大标题
- final TextView title2 = (TextView) view
- .findViewById(R.id.child_text2);
- title2.setText(childData.get(groupPosition).get(childPosition)
- .get(CHILD_TEXT2).toString());//小标题
- return view;
- }
- //得到小组成员id
- public long getChildId(int groupPosition, int childPosition) {
- return childPosition;
- }
- //得到小组成员的名称
- public Object getChild(int groupPosition, int childPosition) {
- return childData.get(groupPosition).get(childPosition).get(CHILD_TEXT1)
- .toString();
- }
- //得到小组成员的数量
- public int getChildrenCount(int groupPosition) {
- return childData.get(groupPosition).size();
- }
- /**
- * Indicates whether the child and group IDs are stable across changes to the
- * underlying data.
- * 表明大組和小组id是否稳定的更改底层数据。
- * @return whether or not the same ID always refers to the same object
- * @see Adapter#hasStableIds()
- */
- public boolean hasStableIds() {
- return true;
- }
- //得到小组成员是否被选择
- public boolean isChildSelectable(int groupPosition, int childPosition) {
- return true;
- }
- }
- }
主界面配置文件main.xml:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/default_bg" >
- <ExpandableListView
- android:id="@+id/list"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_alignParentLeft="true" android:cacheColorHint="#00000000" /><!-- 背景设置为透明,防止滑动时,白屏 -->
- </RelativeLayout>
大组成员配置文件member_listview.xml:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal" >
- <LinearLayout
- android:id="@+id/layout_013"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <ImageView
- android:id="@+id/ImageView01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:gravity="center_vertical"
- android:paddingTop="10dp"
- android:src="@drawable/user_group" >
- </ImageView>
- <RelativeLayout
- android:id="@+id/layout_013"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" >
- <TextView
- android:id="@+id/content_001"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_gravity="center_vertical"
- android:gravity="center_vertical"
- android:paddingLeft="10dp"
- android:textColor="#FFFFFF"
- android:textSize="30sp" >
- </TextView>
- <ImageView
- android:id="@+id/tubiao"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true" >
- </ImageView>
- </RelativeLayout>
- </LinearLayout>
- <!--
- <RelativeLayout android:id="@+id/layout_013"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <ImageView android:id="@+id/ImageView01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/icon"></ImageView>
- <TextView android:id="@+id/content_001"
- android:text="@+id/TextView01"
- android:layout_width="wrap_content"
- android:layout_toRightOf="@+id/ImageView01"
- android:layout_height="wrap_content"></TextView>
- <ImageView android:layout_width="wrap_content"
- android:layout_toRightOf="@+id/content_001"
- android:layout_height="wrap_content"
- android:id="@+id/tubiao"></ImageView>
- </RelativeLayout>
- -->
- </LinearLayout>
小组成员配置文件member_childitem.xml:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/childlayout"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal" >
- <ImageView
- android:id="@+id/child_image"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="40dp"
- android:background="@drawable/child_image"
- android:paddingTop="10dp" >
- </ImageView>
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/child_text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:gravity="center_vertical"
- android:text=""
- android:textColor="#FFFFFF"
- android:textSize="25sp" >
- </TextView>
- <TextView
- android:id="@+id/child_text2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:gravity="center_vertical"
- android:text=""
- android:textColor="#FFFFFF"
- android:textSize="20sp" >
- </TextView>
- </LinearLayout>
- </LinearLayout>
Android之实现QQ好友分组(ExpandableListView)相关推荐
- Android 高仿QQ 好友分组列表
实现的效果如下: 用ExpandableListView实现, 先看Activity的代码: public class BuddyActivity extends Activity {Expandab ...
- expandableListview的使用,模仿qq好友分组点击收缩扩展
我主要讲述的是用listview实现.模仿qq好友分组点击收缩.扩展功能 这个是对listview的拓展,用法比较相似,还是需要一个适配器 MainActivitypublic class MainA ...
- android 仿qq好友动态,Android UI仿QQ好友列表分组悬浮效果
本文实例为大家分享了Android UI仿QQ好友列表分组悬浮效果的具体代码,供大家参考,具体内容如下 楼主是在平板上測试的.图片略微有点大,大家看看效果就好 接下来贴源代码: PinnedHeade ...
- IOS 实现QQ好友分组展开关闭功能
贴出核心代码 主要讲一下思路. - (void)nameBtnClick:(myButton *)sender { //获取当前点击的分组对应的section self.clickIndex = s ...
- Qt可拖拽排序表格(类似QQ好友分组排序)
1,简介 为了最佳体验,一个拖拽行排序的功能研究了几个小时.效果参考的QQ好友分组的排序. 网上查了下好像没有人发布QT版类似的代码,于是自己动手 QQ好友分组排序效果: 2,效果 这是最终效果图,有 ...
- QQ好友分组模拟小程序
QQ好友分组:一个好友组里有多个好友,一个好友只能选择一个组,这样好友组和好友之间就是一个一对多的关系.在此程序中封装一个好友类即Buddy类,一个组类即Group类.在Buddy类有有关好友的最基本 ...
- js实现qq好友分组
qq好友分组 <style>ul,h2 {padding: 0;margin: 0;background-color: wheat;}li {list-style: none;}#list ...
- 模仿QQ好友分组风格
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"/> <title> ...
- UITableView的折叠收缩和QQ好友分组效果
可折叠展开的tableView,QQ好友分组列表 demo下载地址https://github.com/zhengwenming/ExpandTableView 原理分析:这个可以折叠的table ...
最新文章
- 强化学习大规模应用还远吗?Youtube推荐已强势上线
- python 中的static-method (静态函数), classmethod(类函数 ), 成员函数
- vim 下web开发html css js插件
- c语言贪吃蛇咬到尾巴,【图片】C语言小游戏~贪吃蛇【c语言吧】_百度贴吧
- 王炸!Waymo正式官宣无人车出行平台,瑟瑟发抖的不止Uber
- python多进程用不了_python 多进程,实际上都没有运行,sleep
- php 手机号 去掉86,手机号前面的+86是什么意思
- 苹果电脑上不错的svn客户端
- UE4 编辑器模块引用,不影响打包处理
- 2016年不可错过的21个深度学习视频、教程和课程
- 陌生人交友软件有哪些?陌生人社交APP排名|良心推荐
- 小智机器人有初中课程吗_张小智
- 怪兽融合、神秘药水?《Monster Tamer》深度攻略,来不及解释快上车!
- 【工大SCIR】基于动态图交互网络的多意图口语语言理解框架
- 收藏|超实用的100个示波器基础知识问答
- [笔记分享] [RTC] Alarm内核驱动分析
- primo驱动启动顺序
- 京东上千页面搭建基石——CMS前后端分离演讲史读后感
- python 爬虫 智联招聘
- qmainwindow 背景充电_Qt 设置背景图片(背景图片可随意拉伸)