您的位置:首页 > 其它

QQ分组实现,可收缩---ExpandableListView

2015-11-17 16:22 337 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: