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 ...
随机推荐
- Apahce映射网络路径
要点有两个: 1. 要使用全路径,不要使用映射的网络驱动器.2. 路径之间用斜杠/,不要用反斜杠\. Alias /weili.mobile "//vmware-host/Shared Fo ...
- [HDU 4419] Colourful Rectangle (扫描线 矩形面积并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4419 题目大意:比矩形面积并多了颜色,问染成的每种颜色的面积. 矩形面积并的扫描线维护的是长度,这道题 ...
- ThreadLocal意为变量副本
http://blog.csdn.net/lufeng20/article/details/24314381
- ASP.NET fails to detect Internet Explorer 10
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9"> http://www.han ...
- JNI_Android项目中调用.so动态库实现详解【转】
转自 http://www.cnblogs.com/sevenyuan/p/4202759.html 1. 在Eclipse中创建项目:TestJNI 2. 新创建一个class:TestJNI.ja ...
- Unity3d - RPG项目学习笔记(一)
通过NGUI和工程素材,学习泰课项目——黑暗之光. 现阶段心得整理: 一.开始界面 开始界面显示顺序为:①白幕渐隐:②镜头拉近:③标题渐显:④按键响应. 1.1 白幕渐隐 NGUI是一个非常强大的插件 ...
- webpack基础+webpack配置文件常用配置项介绍+webpack-dev-server
一.webpack基础 1.在项目中生成package.json:在项目根目录中输入npm init,根据提示输入相应信息.(也可以不生成package.json文件,但是package.json是很 ...
- OpenStack-Mitaka 一键安装测试环境脚本
说明:这个脚本是采用Bash Shell编写,这个版本还只能作为测试环境搭建使用. 此脚本原形的发起人是网友:WuYuLiang.这里有他的博客链接: 第一版的链接: http://blog.cs ...
- 【转】最实用的IT类网站及工具大集合
转自:http://www.cnblogs.com/annie00/p/5753507.html 1.聚合数据 大家在开发过程中,可能会用到各种各样的数据,想找一些接口来提供一些数据.比如天气预报查询 ...
- 华为OJ平台——字符串匹配
题目描述: 判断短字符串中的所有字符是否在长字符串中全部出现 输入: 输入两个字符串. 第一个为短字符,第二个为长字符 输出: true - 表示短字符串中所有字符均在长字符串中出现 false - ...