ExpandableListView实现简单分组功能

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

二、 设置简单参数

获取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;

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

			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
				// TODO Auto-generated method stub
				return 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);
		}

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

	}

}
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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值