QQ 添加分组 添加好友

来源:互联网 发布:php 上传中文文件名 编辑:程序博客网 时间:2024/04/30 07:10


package com.example.qq;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.ViewGroup;import android.widget.Button;import android.widget.ExpandableListAdapter;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 ArrayList<HashMap<String, Object>> data;private MyExpandableListAdapter mExpandableListAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 分组的标签String[] g = { "时光机", "好久不见", "黑色幽默", "甜甜的", "说好的幸福呢" };data = new ArrayList<HashMap<String, Object>>();// 子数据的计数//int COUNT = 0;//Random rand = new Random();for (int i = 0; i < g.length; i++) {HashMap<String, Object> map = new HashMap<String, Object>();map.put(GROUP, g[i]);ArrayList<String> child = new ArrayList<String>();//int k = rand.nextInt(10);// 为每个子List随机生成k个测试数据for (int j = 0; j < 5; j++) {child.add("朋友" + j);}map.put(CHILD, child);data.add(map);}ExpandableListView elv = (ExpandableListView) findViewById(R.id.expandableListView);elv.setGroupIndicator(null);// 这是一个参数为空或者null的ExpandableListAdapter// 构造在子类中完成mExpandableListAdapter = new MyExpandableListAdapter(this, null, 0, null, null, null, 0, null, null);elv.setAdapter(mExpandableListAdapter);// 展开0组//elv.expandGroup(0);// 展开1组//elv.expandGroup(1);// 展开2组//elv.expandGroup(2);elv.setOnGroupClickListener(new OnGroupClickListener() {@Overridepublic boolean onGroupClick(ExpandableListView arg0, View arg1,int arg2, long arg3) {// Android默认是返回false。// 如果返回true,那么,不管是点击已展开的分组还是未展开的分组,都不会相应展开或者收缩的,也就是说这个ExpandableListView将成为一个‘死’的ListViewreturn false;}});Button button1 = (Button) findViewById(R.id.button1);button1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {addGroupData(2);}});Button button2 = (Button) findViewById(R.id.button2);button2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {addChildData(1,"3");}});}private void addChildData(int pos,String str) {HashMap<String,Object> map = data.get(pos);ArrayList<String> list = (ArrayList<String>)map.get(CHILD);list.add(2,str);mExpandableListAdapter.notifyDataSetChanged();}private void addGroupData(int pos) {HashMap<String,Object> map = new HashMap<String,Object>();map.put(GROUP,"听妈妈的话");ArrayList<String> child = new ArrayList<String>();for(int j=0;j<5;j++){child.add("朋友"+j);}map.put(CHILD,child);//data.add(map);data.add(pos,map);mExpandableListAdapter.notifyDataSetChanged();}private class MyExpandableListAdapter extends SimpleExpandableListAdapter {private LayoutInflater inflater;public MyExpandableListAdapter(Context context,List<? extends Map<String, ?>> groupData, int groupLayout,String[] groupFrom, int[] groupTo,List<? extends List<? extends Map<String, ?>>> childData,int childLayout, String[] childFrom, int[] childTo) {super(context, groupData, groupLayout, groupFrom, groupTo,childData, childLayout, childFrom, childTo);inflater = LayoutInflater.from(context);}@Overridepublic Object getChild(int groupPosition, int childPosition) {ArrayList<String> items = (ArrayList<String>) data.get(groupPosition).get(CHILD);return items.get(childPosition);}@Overridepublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {View view = inflater.inflate(android.R.layout.simple_list_item_1,null);TextView text = (TextView) view.findViewById(android.R.id.text1);text.setText(getChild(groupPosition, childPosition) + "");return view;}@Overridepublic int getChildrenCount(int groupPosition) {ArrayList<String> items = (ArrayList<String>) data.get(groupPosition).get(CHILD);return items.size();}@Overridepublic Object getGroup(int groupPosition) {return data.get(groupPosition).get(GROUP);}@Overridepublic int getGroupCount() {return data.size();}@Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {View view = inflater.inflate(android.R.layout.simple_list_item_1,null);TextView text = (TextView) view.findViewById(android.R.id.text1);text.setText(getGroup(groupPosition) + "");view.setBackgroundColor(Color.RED);return view;}}}


<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="match_parent"     android:orientation="vertical"    tools:context="com.example.qq.MainActivity" >  <LinearLayout      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:orientation="horizontal" >      <Button          android:id="@+id/button1"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_weight="1"          android:text="添加分组" />    <Button      android:id="@+id/button2"      android:layout_width="wrap_content"        android:layout_height="wrap_content"      android:layout_weight="1"      android:text="添加朋友"      /></LinearLayout>  <ExpandableListView          android:id="@+id/expandableListView"          android:layout_width="match_parent"          android:layout_height="match_parent" />         </LinearLayout>


0 0
原创粉丝点击