package com.cdma;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Toast;
public class ExListView extends Activity {
private static final String G_TEXT = "G_TEXT";
//private static final String G_CB = "G_CB";
private static final String C_TITLE = "C_TITLE";
private static final String C_TEXT = "C_TEXT";
private static final String C_CB = "C_CB";
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
List<Map<String, Boolean>> groupCheckBox = new ArrayList<Map<String,Boolean>>();
List<List<Map<String, Boolean>>> childCheckBox = new ArrayList<List<Map<String,Boolean>>>();
String CheckedItem="";
ExpandBaseAdapter adapter;
ExpandableListView exList;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//初始化数据Group Text,Child Title Text
for (int i = 0; i < 3; i++) {
Map<String, String> curGroupMap = new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(G_TEXT, "Group " + i);
List<Map<String, String>> children = new ArrayList<Map<String, String>>();
for (int j = 0; j < 3; j++) {
Map<String, String> curChildMap = new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(C_TITLE, "Child " + j);
curChildMap.put(C_TEXT, "Child " + j);
}
childData.add(children);
}
//初始化数据checkBox
for ( int i = 0; i < 5; i++) {
//Map<String, Boolean> curGB = new HashMap<String, Boolean> ();
//groupCheckBox.add(curGB);
//curGB.put(G_CB, false);
List<Map<String, Boolean>> childCB = new ArrayList<Map<String,Boolean>>();
for (int j = 0; j < 5; j++) {
Map<String, Boolean> curCB = new HashMap<String, Boolean>();
childCB.add(curCB);
curCB.put(C_CB, false); //全不选
}
childCheckBox.add(childCB);
}
adapter = new ExpandBaseAdapter(ExListView.this,
groupData, childData, groupCheckBox, childCheckBox);
exList = (ExpandableListView) findViewById(R.id.layoutExListView);
exList.setAdapter(adapter);
// 去掉系统自带的按钮
exList.setGroupIndicator(null);
// 去掉系统自带的分隔线
exList.setDivider(null);
// 展开所有二级列表
int groupCount = adapter.getGroupCount();
for (int i = 0; i < groupCount; i++) {
exList.expandGroup(i);
}
// 监听二级列表
exList.setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
CheckBox checkBox = (CheckBox) v.findViewById(R.id.multiple_checkbox);
checkBox.toggle();
if (childCheckBox.get(groupPosition).get(childPosition).get(C_CB)) {
childCheckBox.get(groupPosition).get(childPosition).put(C_CB, false);
}
else {
childCheckBox.get(groupPosition).get(childPosition).put(C_CB, true);
}
//checkBox.setChecked(childCheckBox.get(groupPosition).get(childPosition).get(C_CB));
CheckedItem = "";
for (int i=0; i < adapter.getGroupCount(); i++) {
for (int j=0; j < adapter.getChildrenCount(i); j++) {
if (childCheckBox.get(i).get(j).get(C_CB)) {
CheckedItem = CheckedItem
+ groupData.get(i).get(G_TEXT) + ":"
+ childData.get(i).get(j).get(C_TITLE)+" ";
}
}
}
Toast.makeText(ExListView.this, CheckedItem, Toast.LENGTH_LONG).show();
/*
Toast.makeText(getBaseContext(),String.valueOf(groupPosition) + ":" + String.valueOf(childPosition),
Toast.LENGTH_SHORT).show();
*/
return false;
}
});
}
}
/* static class ViewHolder {
TextView cTitle;
TextView cText;
CheckBox checkBox;
}
class ExAdapter extends BaseExpandableListAdapter {
ExListView exlistview;
ViewHolder holder;
public ExAdapter(ExListView elv) {
super();
exlistview = elv;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.groupitem, null);
}
TextView title = (TextView) view.findViewById(R.id.content_001);
title.setText(getGroup(groupPosition).toString());
ImageView image=(ImageView) view.findViewById(R.id.groupBox);
if(isExpanded)
image.setBackgroundResource(R.drawable.groupdown);
else image.setBackgroundResource(R.drawable.groupleft);
return view;
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public Object getGroup(int groupPosition) {
return groupData.get(groupPosition).get(G_TEXT).toString();
}
public int getGroupCount() {
return groupData.size();
}
//**************************************
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
//View view = convertView;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.childitem, null);
holder = new ViewHolder();
holder.cTitle = (TextView) convertView.findViewById(R.id.child_title);
holder.cText = (TextView) convertView.findViewById(R.id.child_text);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.multiple_checkbox);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.cTitle.setText(childData.get(groupPosition).get(childPosition).get(C_TITLE).toString());
holder.cText.setText(childData.get(groupPosition).get(childPosition).get(C_TEXT).toString());
holder.checkBox.setChecked(childCheckBox.get(groupPosition).get(childPosition).get(C_CB));
return convertView;
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public Object getChild(int groupPosition, int childPosition) {
return childData.get(groupPosition).get(childPosition).get(C_TITLE).toString();
}
public int getChildrenCount(int groupPosition) {
return childData.get(groupPosition).size();
}
//**************************************
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}*/
package com.cdma;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
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.ImageView;
import android.widget.TextView;
public class ExpandBaseAdapter extends BaseExpandableListAdapter {
private static final String G_TEXT = "G_TEXT";
//private static final String G_CB = "G_CB";
private static final String C_TITLE = "C_TITLE";
private static final String C_TEXT = "C_TEXT";
private static final String C_CB = "C_CB";
List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
List<Map<String, Boolean>> groupCheckBox = new ArrayList<Map<String,Boolean>>();
List<List<Map<String, Boolean>>> childCheckBox = new ArrayList<List<Map<String,Boolean>>>();
private Context context;
//ExListView exlistview;
ViewHolder holder;
public ExpandBaseAdapter(Context context,
List<Map<String, String>> groupData, List<List<Map<String, String>>> childData,
List<Map<String, Boolean>> groupCheckBox, List<List<Map<String, Boolean>>> childCheckBox) {
this.groupData = groupData;
this.childData = childData;
this.groupCheckBox = groupCheckBox;
this.childCheckBox = childCheckBox;
this.context = context;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.groupitem, null);
}
TextView title = (TextView) view.findViewById(R.id.groupText);
title.setText(getGroup(groupPosition).toString());
ImageView image = (ImageView) view.findViewById(R.id.groupBox);
if (isExpanded)
image.setBackgroundResource(R.drawable.groupdown);
else
image.setBackgroundResource(R.drawable.groupleft);
return view;
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public Object getGroup(int groupPosition) {
return groupData.get(groupPosition).get(G_TEXT).toString();
}
public int getGroupCount() {
return groupData.size();
}
// **************************************
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// View view = convertView;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.childitem, null);
holder = new ViewHolder();
holder.cTitle = (TextView) convertView
.findViewById(R.id.child_title);
holder.cText = (TextView) convertView.findViewById(R.id.child_text);
holder.checkBox = (CheckBox) convertView
.findViewById(R.id.multiple_checkbox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.cTitle.setText(childData.get(groupPosition).get(childPosition)
.get(C_TITLE).toString());
holder.cText.setText(childData.get(groupPosition).get(childPosition)
.get(C_TEXT).toString());
holder.checkBox.setChecked(childCheckBox.get(groupPosition)
.get(childPosition).get(C_CB));
return convertView;
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public Object getChild(int groupPosition, int childPosition) {
return childData.get(groupPosition).get(childPosition).get(C_TITLE)
.toString();
}
public int getChildrenCount(int groupPosition) {
return childData.get(groupPosition).size();
}
// **************************************
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
static class ViewHolder {
TextView cTitle;
TextView cText;
CheckBox checkBox;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- android:background="@drawable/default_bg"> -->
<ExpandableListView
android:id="@+id/layoutExListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"/>
</RelativeLayout>
<?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="40dip"
android:layout_gravity="center_horizontal">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/layout_01">
<ImageView
android:id="@+id/groupImaView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:src="@drawable/group_icon"/>
<RelativeLayout
android:id="@+id/layout_02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="@+id/groupText"
android:textSize="26px"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFFFFF"
android:gravity="center_vertical"
android:paddingLeft="10px"
android:layout_gravity="center_vertical"/>
<ImageView
android:id="@+id/groupBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
<?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="fill_parent"
android:id="@+id/childlayout"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="fill_parent"
android:id="@+id/relativeLayout1"
android:layout_height="wrap_content">
<ImageView android:id="@+id/childImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/child_icon"
android:paddingTop="10dip"
android:layout_marginLeft="30dip"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_toRightOf="@id/childImageView">
<TextView
android:id="@+id/child_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="20dip"
android:layout_gravity="center_vertical"/>
<TextView
android:id="@+id/child_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dip"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<RatingBar
android:id="@+id/ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:numStars="5"
style="?android:attr/ratingBarStyleSmall"
android:gravity="center_vertical"
android:layout_toLeftOf="@+id/multiple_checkbox"/>
<!--这三个很重要
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
-->
<CheckBox
android:id="@+id/multiple_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:layout_alignParentRight="true" android:checked="true"/>
</RelativeLayout>
</LinearLayout>