QQ分组实现,可收缩---ExpandableListView
2015-11-17 16:22
337 查看
activity:
xml:
activity_main.xml
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
相关文章推荐
- 用rabbitMQ实现消息的发布和订阅
- C++面向对象高级开发_极客班_第一节课笔记_section3
- Gradient And Karush-Kuhn-Tucker Conditions
- [kuangbin带你飞]专题二 搜索进阶 A - Eight
- Theme.AppCompat.Light无法找到问题
- linux下修改环境变量
- 我是怎样转IOS应用开发的
- sharepoint 中用自带的download.aspx实现文件的下载,中文文件名编码的问题
- UI设计颜色风格
- spring中bean的作用域
- 关于Git
- QT 编译问题
- sicily 1099. Packing Passengers
- The specified child already has a parent. You must call removeView() on the child's parent first.
- compiler之automatic memory management以及Java GC
- Anorm, simple SQL data access (play 2.4.x)
- MongoDB小结12 - update【多文档更新】
- 自定义圆形控件RoundImageView并认识一下attr
- GlusterFS复制卷修复原理以及脑裂分析
- 创业公司新品如何寻求科技媒体的报道?