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

最新文章

  1. 用Go语言做产品半年的一些感觉
  2. 大数据平台架构(flume+kafka+hbase+ELK+storm+redis+mysql)
  3. Java程序设计之正则表达式
  4. Linux上的常用软件
  5. go语言常用函数:make
  6. Ubuntu16.04.1 安装Redis-Cluster
  7. div+css遮罩层
  8. request和response的中文乱码问题
  9. iPhone应用程序间传递数据
  10. [RxJS] Using Observable.create for fine-grained control
  11. springMVC,spring,mybatis全注解搭建框架--第一步,让框架跑起来
  12. LeetCode算法题-Isomorphic Strings(Java实现)
  13. hashMap源码解析(五)
  14. 一、iOS开发环境搭建
  15. Centos PS1
  16. Oracle Data Integrator 12cR1 (12.1.3.0.0)安装过程
  17. 百度云盘下载插进-油猴Tampermonkey
  18. C# 把字符串中间的多个连续的空格转化成一个空格
  19. Fiddler抓包域名过滤
  20. 自定义方法实现strcpy,strlen, strcat, strcmp函数,了解及实现原理

热门文章

  1. (easy)LeetCode 263.Ugly Number
  2. nfs挂在内核出错 T T *** ERROR: Cannot umount
  3. HTML控件-Select
  4. 【caffe-windows】 caffe-master 之 classfication_demo.m 超详细分析
  5. ASP.NET5 中静态文件的各种使用方式
  6. 单链表(c++)
  7. socket 和 SocketServer 模块
  8. Java基础——多线程
  9. 用pxe启动iso光盘里的pe
  10. Windows phone 8 学习笔记(5) 图块与通知(转)