关闭

ExpandableListView可手动添加简单分组功能

267人阅读 评论(0) 收藏 举报

该片文章是下面文章的延伸版

效果图为

 

ExpandableListView实现简单分组功能


点击打开链接

http://blog.csdn.net/xiaoke804376088/article/details/49885793

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.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
	private final String GROUP = "group";
	private final String CHILD = "child";
	private ArrayList<HashMap<String, Object>> data;
	private MyExpand mExpandableListAdapter =null;
	private EditText etGroup=null;
	private EditText etChild=null;		
	@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);
		}
		etGroup=(EditText) findViewById(R.id.et_group_id);
		etChild=(EditText) findViewById(R.id.et_child_id);
		Button btnGroup;
		btnGroup=(Button) findViewById(R.id.btn_group_id);
		btnGroup.setOnClickListener(this);
		btnGroup=(Button) findViewById(R.id.btn_child_id);
		btnGroup.setOnClickListener(this);
		
		// 获取ExpandableListView对象
		ExpandableListView elv = (ExpandableListView) findViewById(R.id.expandableListView);
		//设置箭头符号不显示
		elv.setGroupIndicator(null);
		// 加载adapter,注意参数的对应
		 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;
			}
		});

	}
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.btn_group_id:
			createGroup(etGroup.getText().toString());
			//注意,数据更新后要跟新界面,不然显示不变
			mExpandableListAdapter.notifyDataSetChanged();
			break;
		case R.id.btn_child_id:
			if(!etChild.getText().toString().equals("")){
			creatChild(Integer.parseInt(etChild.getText().toString()));
			mExpandableListAdapter.notifyDataSetChanged();
			}
			break;
		}
		
	}
	public void createGroup(String str){
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put(GROUP, str);	
		ArrayList<String> temp = new ArrayList<String>();
		for (int j = 0; j < 5; j++) {
			temp.add("数据" + j);
		}
		map.put(CHILD, temp);
		data.add(map);
	}
	public void creatChild(int tempInt){
		ArrayList<String> listTemp= (ArrayList<String>) data.get(1).get(CHILD);
		listTemp.add(""+tempInt);
	}

	// /*
	// * 重写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布局文件

<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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/et_group_id"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:singleLine="true" />

        <Button
            android:id="@+id/btn_group_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="添加组"
            android:textSize="15sp" />

        <EditText
            android:id="@+id/et_child_id"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:singleLine="true" />

        <Button
            android:id="@+id/btn_child_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="添加成员"
            android:textSize="15sp" />
    </LinearLayout>

    <ExpandableListView
        android:id="@+id/expandableListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>



0
0

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