QQ 分组界面
2015-11-17 11:25
393 查看
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.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; @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 < k; j++) { child.add("朋友" + COUNT++); } map.put(CHILD, child); data.add(map); } ExpandableListView elv = (ExpandableListView) findViewById(R.id.expandableListView); elv.setGroupIndicator(null); // 这是一个参数为空或者null的ExpandableListAdapter // 构造在子类中完成 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; } }); } 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; } } }
<RelativeLayout 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" tools:context="com.example.qq.MainActivity" > <ExpandableListView android:id="@+id/expandableListView" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
相关文章推荐
- SSH配置:Hibernate的packagesToScan路径解析分析
- css3编写导航栏
- HDOJ 1846 Brave Game
- bootstrap2.0与3.0的区别
- GCTF的re50反汇编分析
- Linux 常用命令
- 浅谈win10TH2更新
- hihocoder1258(水)(2015ACM/ICPC北京站)
- svn up时提示跳过某节点
- 安卓手机电池信息的获取与显示
- 图片旋转UIImageView+RotateImageView
- hihocoder1258(水)(2015ACM/ICPC北京站)
- Spring事务配置的五种方式
- iOS开发宏定义与const修饰的变量的区别
- lua笔记
- 用nodejs搭建一个简单的服务监听程序
- ubuntu14.04下配置使用openCV3.0
- 提取Office以及PDF里的文字
- Java的abstract基本要点
- 华为OJ平台遇到的坑