ExpandableListView实现简单分组功能

来源:互联网 发布:海鹰数据 wish 编辑:程序博客网 时间:2024/05/17 01:52

第一步,创建分组所需的信息,图解为:

二、设置简单参数

获取ExpandableListView对象

设置参数setGroupIndicator(null);

 加载adapter,注意参数的对应

  设置setAdapter(mExpandableListAdapter);

添加点击事件,点击打开或者关闭分组

setOnGroupClickListener

三、创建一个类,继承SimpleExpandableListAdapter重写六个方法

 重写6个方法
 getGroup:返回对应位置的组的参数
 getGroupCount:返回分了多少组
 getGroupView:显示组
 getChild:返回每组的成员
 getChildCount:返回每组成员的个数
 getChildView : 显示每组成员
在构造方法里面实例化LayoutInflater的对象,下面是两个实例化对象的方法

1、

inflater = LayoutInflater.from(context);

2、

inflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);



下面是该程序的代码

java代码:

package com.xiaoke.expandablelistview;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.content.Context;import android.graphics.Color;import android.os.Bundle;import android.provider.Settings.System;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;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;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);data = new ArrayList<HashMap<String, Object>>();// 创建分组的信息String[] strGroup = { "A", "B", "C", "D", "E" };for (int i = 0; i < strGroup.length; i++) {HashMap<String, Object> map = new HashMap<String, Object>();map.put(GROUP, strGroup[i]);ArrayList<String> temp = new ArrayList<String>();for (int j = 0; j < 5; j++) {temp.add("数据" + j);}map.put(CHILD, temp);data.add(map);}// 获取ExpandableListView对象ExpandableListView elv = (ExpandableListView) findViewById(R.id.expandableListView);elv.setGroupIndicator(null);// 加载adapter,注意参数的对应ExpandableListAdapter mExpandableListAdapter = new MyExpand(this, null,0, 0, null, null, null, 0, 0, null, null);elv.setAdapter(mExpandableListAdapter);// // 演示//// // 展开0组// elv.expandGroup(0);// // 收起1组// elv.collapseGroup(1);// // 展开2组// elv.expandGroup(2);// 添加点击事件,点击打开或者关闭分组elv.setOnGroupClickListener(new OnGroupClickListener() {@Overridepublic boolean onGroupClick(ExpandableListView parent, View v,int groupPosition, long id) {// TODO Auto-generated method stubreturn false;}});}// /*// * 重写6个方法// * getGroup:返回对应位置的组的参数// * getGroupCount:返回分了多少组// * getGroupView:显示组// * getChild:返回每组的成员// * getChildCount:返回每组成员的个数// * getChildView : 显示每组成员// */public class MyExpand extends SimpleExpandableListAdapter {private LayoutInflater inflater = null;// 构造方法中实例化LayoutInflater对象public MyExpand(Context context,List<? extends Map<String, ?>> groupData,int expandedGroupLayout, int collapsedGroupLayout,String[] groupFrom, int[] groupTo,List<? extends List<? extends Map<String, ?>>> childData,int childLayout, int lastChildLayout, String[] childFrom,int[] childTo) {super(context, groupData, expandedGroupLayout,collapsedGroupLayout, groupFrom, groupTo, childData,childLayout, lastChildLayout, childFrom, childTo);// TODO Auto-generated constructor stub// 两个方法实例化LayoutInflater对象inflater = LayoutInflater.from(context);// inflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);}@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;}}}
xml布局文件为:

<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="zhangphil.expandablelistview.MainActivity" >        <ExpandableListView          android:id="@+id/expandableListView"          android:layout_width="match_parent"          android:layout_height="match_parent" />   </RelativeLayout>
该代码大部分根据以下博客改编而来
http://blog.csdn.net/zhangphil/article/details/49356595


0 0
原创粉丝点击