需要用ExpandableListView+CheckBox實現一個組選列表,ExpandableListView組列表為部門,子列表為成員。需要選中部門的CheckBox時,選中所有組內成員CheckBox,取消時全部取消。選擇單個組內成員時,未全部選中該部門成員,則部門CheckBox不勾選,如選中了所有成員,則自動勾選部門CheckBox。希望以前做過的朋友可以給個詳細的例子
下面附上我的代碼
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class ReportListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<DepartmentEntity> parentList;
private Map<String, List<EmployeeEntity>> map;
private List<Boolean> parentStatusList = new ArrayList<Boolean>();
private List<Boolean> childStatusList = new ArrayList<Boolean>();
public ReportListAdapter(Context context, List<DepartmentEntity> parentList,
Map<String, List<EmployeeEntity>> map) {
this.context = context;
this.parentList = parentList;
this.map = map;
}
// 得到子item需要關聯的數據
@Override
public Object getChild(int groupPosition, int childPosition) {
String key = parentList.get(groupPosition).getId();
return (map.get(key).get(childPosition));
}
// 得到子item的ID
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
// 設置子item的組件
@Override
public View getChildView(final int groupPosition, final int childPosition,
final boolean isLastChild, View convertView, final ViewGroup parent) {
if (convertView == null) {
convertView = View.inflate(context, R.layout.report_list_child_item,
null);
}
String key = this.parentList.get(groupPosition).getId();
final EmployeeEntity employee = map.get(key).get(childPosition);
String name = employee.getName();
TextView employeeName = BaseViewHolder.get(convertView,
R.id.employee_name);
employeeName.setText(name);
CheckBox childCB = BaseViewHolder.get(convertView, R.id.employee_checkbox);
// if (employee.isChecked()) {
// cb.setChecked(true);
// } else {
// cb.setChecked(false);
// }
childCB.setChecked(employee.isChecked());
// notifyDataSetChanged();
childCB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CheckBox nowCB = (CheckBox) view;
if (nowCB.isChecked()) {
employee.setIsChecked(true);
//
// //獲得子item數量
int childSize = getChildrenCount(groupPosition);
//其他子item狀態
List<Boolean> otherChildStatus = new ArrayList<Boolean>();
otherChildStatus.clear();
for (int i = 0; i < childSize; i++) {
if (i != childPosition) {
boolean isLastChild1;
if (i == childSize - 1) {
isLastChild1 = true;
} else {
isLastChild1 = false;
}
//獲得子item
View view1 = getChildView(groupPosition, i, isLastChild1, null, null);
CheckBox childCB = (CheckBox) view1.findViewById(R.id.employee_checkbox);
boolean cbStatus;
if (childCB.isChecked()) {
cbStatus = true;
} else {
cbStatus = false;
}
otherChildStatus.add(cbStatus);
}
}
//獲得父item
View parentView1 = getGroupView(groupPosition, true, null, null);
//獲取父item的cb
CheckBox parentCB = (CheckBox) parentView1.findViewById(R.id.department_checkbox);
//設置父Item選項框狀態
for (int i = 0; i < otherChildStatus.size(); i++) {
if (!otherChildStatus.get(i)) {
parentList.get(groupPosition).setIsChecked(false);
parentCB.setChecked(false);
System.out.println("設置父cb不選");
break;
} else {
parentList.get(groupPosition).setIsChecked(true);
parentCB.setChecked(true);
notifyDataSetChanged();
System.out.println("設置父cb全選中");
}
}
notifyDataSetChanged();
} else {
//獲得父item
View parentView = getGroupView(groupPosition, true, null, null);
employee.setIsChecked(false);
CheckBox parentCB = (CheckBox) parentView.findViewById(R.id.department_checkbox);
parentList.get(groupPosition).setIsChecked(false);
parentCB.setChecked(false);
System.out.println("設置父cb不選");
notifyDataSetChanged();
}
}
});
// cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
// @Override
// public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
// //獲得父item
// View parentView = getGroupView(groupPosition, true, null, null);
// if (isChecked) {
// employee.setIsChecked(true);
//
// //獲得子item數量
// int childSize = getChildrenCount(groupPosition);
// //其他子item狀態
// List<Boolean> otherChildStatus = new ArrayList<Boolean>();
// for (int i = 0; i < childSize; i++) {
// if (i != childPosition) {
// boolean isLastChild1;
// if (i == childSize - 1) {
// isLastChild1 = true;
// } else {
// isLastChild1 = false;
// }
// //獲得子item
// View view = getChildView(groupPosition, i, isLastChild1, null, null);
// CheckBox childCB = (CheckBox) view.findViewById(R.id.employee_checkbox);
// boolean cbStatus;
// if (childCB.isChecked()) {
// cbStatus = true;
// } else {
// cbStatus = false;
// }
// otherChildStatus.add(cbStatus);
// }
// }
// //設置父Item選項框狀態
// for (int i = 0; i < otherChildStatus.size(); i++) {
// CheckBox parentCB = (CheckBox) parentView.findViewById(R.id.department_checkbox);
// if (otherChildStatus.get(i) == false) {
// parentCB.setChecked(false);
// parentList.get(groupPosition).setIsChecked(false);
// break;
// } else {
// parentCB.setChecked(true);
// parentList.get(groupPosition).setIsChecked(true);
// }
// }
// notifyDataSetChanged();
// } else {
// employee.setIsChecked(false);
// CheckBox parentCB = (CheckBox) parentView.findViewById(R.id.department_checkbox);
// parentCB.setChecked(false);
// parentList.get(groupPosition).setIsChecked(false);
// notifyDataSetChanged();
// }
//
// }
// });
return convertView;
}
// 獲取當前父item下的子item的個數
@Override
public int getChildrenCount(int groupPosition) {
String key = parentList.get(groupPosition).getId();
int size = map.get(key).size();
return size;
}
// 獲取當前父item的數據
@Override
public Object getGroup(int groupPosition) {
return parentList.get(groupPosition);
}
@Override
public int getGroupCount() {
return parentList.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
// 設置父item組件
@Override
public View getGroupView(final int groupPosition, final boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = View.inflate(context, R.layout.report_list_parent_item,
null);
}
ImageView mgroupimage = BaseViewHolder.get(convertView, R.id.arrow);
mgroupimage.setImageResource(R.mipmap.la);
if (!isExpanded) {
mgroupimage.setImageResource(R.mipmap.shou);
}
TextView departmentName = BaseViewHolder.get(convertView, R.id.department_name);
departmentName.setText(parentList.get(groupPosition).getName());
final CheckBox cb = BaseViewHolder.get(convertView, R.id.department_checkbox);
cb.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
System.out.println("點擊了父" + groupPosition);
CheckBox nowCB = (CheckBox) view;
if (nowCB.isChecked()) {
//獲取子item的數量
int childSize = getChildrenCount(groupPosition);
//子item全選中
for (int i = 0; i < childSize; i++) {
//獲取子item
EmployeeEntity child = (EmployeeEntity) getChild(groupPosition, i);
child.setIsChecked(true);
boolean isLastChild;
if (i == childSize - 1) {
isLastChild = true;
} else {
isLastChild = false;
}
//獲得子item
View view1 = getChildView(groupPosition, i, isLastChild, null, null);
CheckBox childCB = (CheckBox) view1.findViewById(R.id.employee_checkbox);
childCB.setChecked(true);
System.out.println("子" + groupPosition + "-" + i + " 已經選中");
}
//設置父item被完全選中
parentList.get(groupPosition).setIsChecked(true);
cb.setChecked(true);
System.out.println("父" + groupPosition + " 已經選中");
notifyDataSetChanged();
} else {
//獲取子item的數量
int childSize = getChildrenCount(groupPosition);
//子Item全都不選中
for (int i = 0; i < childSize; i++) {
//獲取子item數據
EmployeeEntity child = (EmployeeEntity) getChild(groupPosition, i);
child.setIsChecked(false);
boolean isLastChild;
if (i == childSize - 1) {
isLastChild = true;
} else {
isLastChild = false;
}
//獲得子item
View view2 = getChildView(groupPosition, i, isLastChild, null, null);
CheckBox childCB = (CheckBox) view2.findViewById(R.id.employee_checkbox);
childCB.setChecked(false);
System.out.println("子" + groupPosition + "-" + i + " 已經設為沒選中");
}
parentList.get(groupPosition).setIsChecked(false);
notifyDataSetChanged();
}
}
}
);
int childSize = getChildrenCount(groupPosition);
List<Boolean> nowChildStatus = new ArrayList<Boolean>();
nowChildStatus.clear();
// cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
// @Override
// public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
// System.out.println("點擊了父" + groupPosition);
// if (isChecked) {
// //獲取子item的數量
// int childSize = getChildrenCount(groupPosition);
// //子item全選中
// for (int i = 0; i < childSize; i++) {
// //獲取子item
// EmployeeEntity child = (EmployeeEntity) getChild(groupPosition, i);
// child.setIsChecked(true);
// boolean isLastChild;
// if (i == childSize - 1) {
// isLastChild = true;
// } else {
// isLastChild = false;
// }
// //獲得子item
// View view = getChildView(groupPosition, i, isLastChild, null, null);
// CheckBox childCB = (CheckBox) view.findViewById(R.id.employee_checkbox);
// childCB.setChecked(true);
// System.out.println("子" + groupPosition + "-" + i + " 已經選中");
// }
// //設置父item被完全選中
// parentList.get(groupPosition).setIsChecked(true);
// cb.setChecked(true);
// System.out.println("父" + groupPosition + " 已經選中");
//
// notifyDataSetChanged();
// } else {
// //獲取子item的數量
// int childSize = getChildrenCount(groupPosition);
// //子Item全都不選中
// for (int i = 0; i < childSize; i++) {
// //獲取子item數據
// EmployeeEntity child = (EmployeeEntity) getChild(groupPosition, i);
// child.setIsChecked(false);
// boolean isLastChild;
// if (i == childSize - 1) {
// isLastChild = true;
// } else {
// isLastChild = false;
// }
// //獲得子item
// View view = getChildView(groupPosition, i, isLastChild, null, null);
// CheckBox childCB = (CheckBox) view.findViewById(R.id.employee_checkbox);
// childCB.setChecked(false);
// System.out.println("子" + groupPosition + "-" + i + " 已經設為沒選中");
// }
// parentList.get(groupPosition).setIsChecked(false);
//
// notifyDataSetChanged();
// }
//
// }
// });
// if (parentList.get(groupPosition).getIsChecked() == 2) {
// cb.setChecked(true);
// } else {
// cb.setChecked(false);
// }
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
//子集是否可選
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
http://blog.csdn.net/u013020402/article/details/50427232 父接點 groupitem事件實現一個機制 點擊之後 遍歷子節點的setCheck=true
這我自己寫的 單元能對你有用。