QQ 添加分组 添加好友
2015-11-17 16:26
483 查看
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>
相关文章推荐
- MongoDB小结14 - find【查询条件$lt $lte $gt $gte】
- jQuery $(document).ready()和JavaScript window.onload()事件的区别
- 移动端web开发技巧
- POJ 2676 dfs
- Office 365 Code Snippets for Windows
- ListView显示与底下的控件重合了,如何解决
- Look! It’s your data!
- 计算机经典书籍
- iOS开发页面传值方案
- 添加信任公钥
- javamail发送与接收邮件实例
- Adapter实现多种布局
- 数组排序
- Windows下将ImageMagick移植到Android平台
- ExtJS4.2学习(18)时间控件(转)
- Android AlertDialog淡入淡出动画
- SpringMVC使用@ResponseBody处理Ajax请求
- 融资
- svn删除文件及所属文件夹
- 使用单例模式实习string类