QQ分组实现,可收缩---ExpandableListView
activity:
package com.zzw.qqgroup; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random; import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView; public class MainActivity extends Activity { private final String GROUP = "group";
private final String CHILD = "child"; private EditText editText;
private MyExpandableListAdapter mExpandableListAdapter; private ArrayList<HashMap<String, Object>> data; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); data = new ArrayList<HashMap<String, Object>>(); editText = (EditText) findViewById(R.id.editText); rawData(); ExpandableListView elv = (ExpandableListView) findViewById(R.id.expandableListView);
elv.setGroupIndicator(null);// 使收缩箭头消失 mExpandableListAdapter = new MyExpandableListAdapter(this, null, 0, 0, null, null, null, 0, null, null); elv.setAdapter(mExpandableListAdapter); /*
* 下面是演示
*/
// elv.expandGroup(0);// 展开0组
// elv.collapseGroup(1);// 收起1组 elv.setOnGroupClickListener(new OnGroupClickListener() { @Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
/*
* 安卓默认是返回false 如果返回true,则不管是点击已展开的分组还是未展开的分组都不会相应展开或者收缩的
*/
return false;
}
}); findViewById(R.id.addGroup).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
addGroup(2);
}
}); findViewById(R.id.addChild).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
addChild(2);
}
});
} // 在指定位置添加组
private void addGroup(int pos) {
String str = editText.getText() + "";
if (!str.trim().equals("")) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put(GROUP, str);
ArrayList<String> childs = new ArrayList<String>();
map.put(CHILD, childs);
data.add(pos, map);
editText.setText("");
mExpandableListAdapter.notifyDataSetChanged();
}
} // 在尾部增加组
private void addGroup() {
String str = editText.getText() + "";
if (!str.trim().equals("")) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put(GROUP, str);
ArrayList<String> childs = new ArrayList<String>();
map.put(CHILD, childs);
data.add(map);
editText.setText("");
mExpandableListAdapter.notifyDataSetChanged();
}
} // 指定组中指定位置添加child数据
private void addChild(int groupPos, int childPos) {
String str = editText.getText() + "";
if (!str.trim().equals("") && groupPos < data.size()) {
HashMap<String, Object> map = data.get(groupPos);
ArrayList<String> childs = (ArrayList<String>) map.get(CHILD);
if (childPos < childs.size()) {
childs.add(childPos, str);
editText.setText("");
mExpandableListAdapter.notifyDataSetChanged();
}
}
} // 指定组中尾部位置增加child元素
private void addChild(int groupPos) {
String str = editText.getText() + "";
if (!str.trim().equals("") && groupPos < data.size()) {
HashMap<String, Object> map = data.get(groupPos);
ArrayList<String> childs = (ArrayList<String>) map.get(CHILD);
childs.add(str);
editText.setText("");
mExpandableListAdapter.notifyDataSetChanged(); }
} // 初始化增加数据
private void rawData() {
// 设置分组
String[] g = { "我的好友", "朋友", "同学", "同事" };
String[] c = { "张三", "李四", "王二", "麻子", "钱五" }; Random rand = new Random();
for (int i = 0; i < g.length; i++) {
int count = 0;
HashMap<String, Object> map = new HashMap<String, Object>();
map.put(GROUP, g[i]); ArrayList<String> child = new ArrayList<String>();
int r = rand.nextInt(10);
for (String ch : c) {
child.add("-------" + ch + count++);
}
map.put(CHILD, child); data.add(map);
}
} private class MyExpandableListAdapter extends SimpleExpandableListAdapter {
LayoutInflater inflater; public MyExpandableListAdapter(Context context, List<? extends Map<String, ?>> groupData,
int expandedGroupLayout, int collapsedGroupLayout, String[] groupFrom, int[] groupTo,
List<? extends List<? extends Map<String, ?>>> childData, int childLayout, String[] childFrom,
int[] childTo) {
super(context, groupData, expandedGroupLayout, collapsedGroupLayout, groupFrom, groupTo, childData,
childLayout, childFrom, childTo);
inflater = LayoutInflater.from(context);
} @Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList<String> items = (ArrayList<String>) data.get(groupPosition).get(CHILD); return items.get(childPosition);
} @Override
public int getChildrenCount(int groupPosition) { ArrayList<String> items = (ArrayList<String>) data.get(groupPosition).get(CHILD);
return items.size();
} @Override
public Object getGroup(int groupPosition) { return data.get(groupPosition).get(GROUP);
} @Override
public int getGroupCount() {
return data.size();
} @Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(android.R.layout.simple_list_item_1, null);
}
TextView textView = (TextView) convertView.findViewById(android.R.id.text1);
textView.setText(getGroup(groupPosition) + "");
textView.setTextColor(Color.RED);
return convertView;
} @Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView,
ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.item, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.textView1);
textView.setText(getChild(groupPosition, childPosition) + "");
return convertView;
}
}
}
xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="com.zzw.qqgroup.MainActivity" > <ExpandableListView
android:id="@+id/expandableListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
</ExpandableListView> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" > <Button
android:id="@+id/addGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="增加分组" /> <Button
android:id="@+id/addChild"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="增加联系人" /> <EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/addGroup"
android:layout_alignBottom="@+id/addGroup"
android:layout_toLeftOf="@+id/addChild"
android:layout_toRightOf="@+id/addGroup"
android:ems="10"
android:hint="请输入" > <requestFocus />
</EditText>
</RelativeLayout> </LinearLayout>
activity_main.xml
QQ分组实现,可收缩---ExpandableListView的更多相关文章
- QQ分组显示列表ExpandableListView组件应用源码
ExpandableListView又称为可扩展的ListView组件,他和ListView组件很相似 不过每行的显示有两个xml文件,一个xml文件用于定义分组列表的显示风格, 还有一个xml文件用 ...
- Android ExpandableListView BaseExpandableListAdapter (类似QQ分组列表)
分组列表视图(ExpandableListView) 和ListView不同的是它是一个两级的滚动列表视图,每一个组可以展开,显示一些子项,类似于QQ列表,这些项目来至于ExpandableListA ...
- 分组的listview——ExpandableListView
开发使用到的数据统计时可以用分组的ExpandablelistView 效果:
- iOS 实现类似QQ分组样式的几种方式
思路 思路很简单,对模型数据操作或则控制界面显示 先看下json部分数据 "chapterDtoList": [{ "token": null, "i ...
- js仿qq分组折叠效果
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- 模拟QQ分组(具有伸缩功能) (添加开源框架的光闪烁效果)SimpleExpandableListAdapter 适配器的用法,并且可添加组及其组内数据。
package com.lixu.qqfenzu; import java.util.ArrayList; import java.util.HashMap; import java.util.Lis ...
- CSS+Jquery实现QQ分组列表
实现效果图如下: 说明: 1.css隐藏分组下的好友内容: 2.Jquery实现点击分组项事件,实现好友内容的显示和隐藏: 3.样式1,可展开多个分组:样式2,只能有一个分组展开: 源码: <! ...
- 模拟QQ分组
package com.lixu.fenzu; import java.util.ArrayList; import java.util.HashMap; import android.app.Lis ...
- C#编写一款qq消息群发器
先上软件成品图 功能编写大概分为以下几个部分了: 获取QQ分组 发送消息 先来讲发送消息吧,实现还是比较简单 //这段主要是用来打开会话窗口的(只能列表中的好友进行会话的) System.Diagno ...
随机推荐
- nyoj 92 图像有用区域
点击打开链接 图像有用区域 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 "ACKing"同学以前做一个图像处理的项目时,遇到了一个问题,他需要摘取 ...
- [ACDream 1430]SETI 后缀数组
题目链接:http://acdream.info/problem?pid=1430 题目大意:给你一个长度不超过10000的字符串,问你出现过两次或两次以上的不重叠的子串有多少个. 后缀数组计算出he ...
- 树状数组HDU1166
http://acm.hdu.edu.cn/showproblem.php?pid=1166 #include<stdio.h> #include<string.h> ]; i ...
- (Loadrunner)Abnormal termination, caused by mdrv process termination.(转)
Load generator跑了太多用户导致CPU和内存爆满,进程无法处理请求 确认自定义的代码是否释放内存 合理调整或增加思考时间 关闭extended log 尽量避免使用Load generat ...
- 初探接口测试框架--python系列6
点击标题下「蓝色微信名」可快速关注 坚持的是分享,搬运的是知识,图的是大家的进步,没有收费的培训,没有虚度的吹水,喜欢就关注.转发(免费帮助更多伙伴)等来交流,想了解的知识请留言,给你带来更多价值,是 ...
- 【LeetCode】16. 3Sum Closest
题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...
- VI小技巧
i.a 进入编辑模式 shift+a 到行尾insert o 在光标下一行编辑 shift+o 在光标上一行编辑 yy 复制 p ...
- VS2013 当前不会命中断点还未为文档加载任何符号
情况:在别人那边的项目可以调试,在我这边不行.看来是电脑环境问题了 自己试过 VS2013修复下(用了1个半小时),点评:无效 网上 五花八门的都不适合 1.设置与当前版本不一致取消打勾. 点评 ...
- NOIP2007 守望者的逃离-DP
https://vijos.org/p/1431 描述 恶魔猎手尤迪安野心勃勃,他背叛了暗夜精灵,率领深藏在海底的娜迦族企图叛变.守望者在与尤迪安的交锋中遭遇了围杀,被困在一个荒芜的大岛上.为了杀死守 ...
- my vimrc
runtime! debian.vim "设置编码 ,ucs-bom,shift-jis,gb18030,gbk,gb2312,cp936 ,ucs-bom,chinese "语言 ...