关闭

QQ 添加分组 添加好友

标签: QQandroid分组
229人阅读 评论(0) 收藏 举报
分类:


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
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:23014次
    • 积分:1136
    • 等级:
    • 排名:千里之外
    • 原创:91篇
    • 转载:20篇
    • 译文:0篇
    • 评论:1条
    文章分类
    文章存档
    最新评论