ExpandableListView中子选项checkBox被选中的操作

  一些记录,或许以后能用。

上图  

列表效果

选择之后的效果

上代码了。

布局文件activity_expandable_list_view.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_margin="5dip"
            android:text="菜单"
            android:textColor="#808080"
            android:textSize="28.0dip" />

        <Button
            android:id="@+id/shopMsubmit"
            android:layout_width="60dip"
            android:layout_height="30dip"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="5dip"
            android:background="@drawable/order_submit"
            android:text="提交菜单"
            android:textColor="@android:color/white"
            android:textSize="12.0dip" />
    </RelativeLayout>

    <ExpandableListView
        android:id="@+id/expandableListView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:divider="@android:color/white"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:groupIndicator="@drawable/expandable_icon" >
    </ExpandableListView>

</LinearLayout>



foodgroup_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/groupName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dip"
        android:gravity="center_vertical"
        android:text="菜名"
        android:textColor="#008000"
        android:textSize="22.0dip" />

</LinearLayout>



foodchild_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="25dip"
    android:orientation="horizontal"
    android:background="@android:color/white" >

    <CheckBox
        android:id="@+id/childCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/checkbox_icon"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="40dip"
   />

    <TextView
        android:id="@+id/childName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="菜名"
        android:textColor="@android:color/black"
        android:textSize="14.0dip" />

</LinearLayout>



然后是执行代码

ExpandableListViewDemo.java

package com.example.expandablelistview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.Toast;

/**
 * 界面
 * @author Sanpark
 * @since 2013-11-23
 * 
 */
public class ExpandableListViewDemo extends Activity {
	private List<String> groupDatas;// 父列表数据
	private List<List<HashMap<String, String>>> childDatas;// 子列表数据
	private ExpandableListView expandableListView;
	private MyExpandableListViewAdapter ex_adapter;
	private Button order_submit;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_expandable_list_view);
		InitDatas();
		InitView();
	}

	private void InitView() {
		order_submit = (Button) findViewById(R.id.shopMsubmit);
		expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
		ex_adapter = new MyExpandableListViewAdapter(this, groupDatas,
				childDatas);
		expandableListView.setAdapter(ex_adapter);
		// 提交操作
		order_submit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				List<List<HashMap<Integer, Boolean>>> childCheckBoxState = ex_adapter.childCheckBoxState;
				String options = "选择的项是:";
				List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
				//循环childCheckBoxState状态,匹配childDatas的值
				for (int j = 0; j < ex_adapter.getGroupCount(); j++) {
					for (int i = 0; i < ex_adapter.getChildrenCount(j); i++) {
						if (childCheckBoxState.get(j).get(i).get(i) == true) {
							@SuppressWarnings("unchecked")
							HashMap<String, Object> map = (HashMap<String, Object>) ex_adapter
									.getChild(j, i);
							String name = map.get("name").toString();
							String price = (String) map.get("price");
							options += "\n" + name + "." + price;
							list.add(map);
						}
					}
				}
				Toast.makeText(getApplicationContext(), options,
						Toast.LENGTH_SHORT).show();
			}
		});
	}

	// 初始化数据
	private void InitDatas() {
		groupDatas = new ArrayList<String>();
		childDatas = new ArrayList<List<HashMap<String, String>>>();
		addInfo("炒菜类", new String[] { "番茄鸡蛋", "茄子豆角", "清炒苦瓜" }, new String[] {
				"9.00", "10.00", "8.00" });
		addInfo("汤类", new String[] { "番茄蛋汤", "紫菜蛋汤" }, new String[] { "9.00",
				"10.00" });
		addInfo("盖饭类", new String[] { "土豆牛肉盖饭", "红烧茄子盖饭", "青椒肉丝盖饭" },
				new String[] { "15.00", "12.00", "14.00" });
	}

	// 添加数据方法
	private void addInfo(String group, String[] childs, String[] price) {
		groupDatas.add(group);
		List<HashMap<String, String>> childItem = new ArrayList<HashMap<String, String>>();
		for (int i = 0; i < childs.length; i++) {
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("name", childs[i]);
			map.put("price", price[i]);
			childItem.add(map);
		}
		childDatas.add(childItem);
	}
}



适配器 MyExpandableListViewAdapter.java

package com.example.expandablelistview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

/**
 * ExpandableListView适配器
 * 
 * @author Sanpark
 * @since 2013-11-23
 */
public class MyExpandableListViewAdapter extends BaseExpandableListAdapter {
	private Context context;
	private List<String> groupDatas;
	private List<List<HashMap<String, String>>> childDatas;
	// 子列表checkBox的状态记录,数据结构与子列表数据一样
	public List<List<HashMap<Integer, Boolean>>> childCheckBoxState = new ArrayList<List<HashMap<Integer,Boolean>>>();
	public MyExpandableListViewAdapter(Context context,
			List<String> groupDatas,
			List<List<HashMap<String, String>>> childDatas) {
		super();
		this.context = context;
		this.groupDatas = groupDatas;
		this.childDatas = childDatas;
		InitCheckBoxState();
	}

	// 初始化checkBox状态,默认所有checkBox未选中
	public void InitCheckBoxState() {
		for (int i = 0; i < groupDatas.size(); i++) {
			List<HashMap<Integer, Boolean>> item = new ArrayList<HashMap<Integer, Boolean>>();
			for (int j = 0; j < childDatas.get(i).size(); j++) {
				HashMap<Integer, Boolean> map = new HashMap<Integer, Boolean>();
				map.put(j, false);
				item.add(map);
			}
			childCheckBoxState.add(item);
		}
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return childDatas.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(final int groupPosition, final int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.foodchild_item, null);
			holder = new ViewHolder();
			holder.name = (TextView) convertView.findViewById(R.id.childName);
			holder.box = (CheckBox) convertView
					.findViewById(R.id.childCheckBox);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.name.setText(childDatas.get(groupPosition).get(childPosition)
				.get("name").toString());
		holder.box.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
				if (isChecked) {
					childCheckBoxState.get(groupPosition).get(childPosition)
							.put(childPosition, true);
				} else {
					childCheckBoxState.get(groupPosition).get(childPosition)
							.put(childPosition, false);
				}
			}
		});
		// 按状态初始化checkBox,避免父列表收缩打开checkBox混乱
		holder.box.setChecked(childCheckBoxState.get(groupPosition)
				.get(childPosition).get(childPosition) == false ? false : true);
		return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return childDatas.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return groupDatas.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return groupDatas.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.foodgroup_item, null);
			holder = new ViewHolder();
			holder.name = (TextView) convertView.findViewById(R.id.groupName);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.name.setText(groupDatas.get(groupPosition).toString());
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	static class ViewHolder {
		TextView name;
		CheckBox box;
	}
}

转载请注明出处:http://my.oschina.net/u/726360/admin/edit-blog?blog=178352

源码已上传CSDN:http://download.csdn.net/detail/just_sanpark/6598061


转载于:https://my.oschina.net/u/726360/blog/178352

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值