QQ 添加分组 添加好友


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;
	
	@Override
	protected 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() {

			@Override
			public boolean onGroupClick(ExpandableListView arg0, View arg1,
					int arg2, long arg3) {
				// Android默认是返回false。
				// 如果返回true,那么,不管是点击已展开的分组还是未展开的分组,都不会相应展开或者收缩的,也就是说这个ExpandableListView将成为一个‘死’的ListView
				return false;
			}
		});
	
	
	Button button1 = (Button) findViewById(R.id.button1);
	button1.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View arg0) {
			addGroupData(2);
		}
		
	});
	
	Button button2 = (Button) findViewById(R.id.button2);
	button2.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public 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);
		}

		@Override
		public Object getChild(int groupPosition, int childPosition) {

			ArrayList<String> items = (ArrayList<String>) data.get(groupPosition).get(CHILD);			
			return items.get(childPosition);
		}

		@Override
		public 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;
		}

		@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) {
			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
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值