我的场景:
我在可扩展列表的 groupView 和 childView 中有一个列表。GroupView 和 ChildView 有复选框。我设置复选框没有问题。当 GroupView 中的复选框被选中时,我想检查 ChilView 的所有复选框。
我经历了这两个解决方案:
Android ExpandableListView CheckBox Select Only Clicked CheckBox
和
选中 ExpandableListView (Android) 中的所有复选框
我选中 groupPosition 2 中的复选框,然后自动选中 groupPosition 5 中的复选框。再次,当我选中 groupPosition 1 中的复选框时,say groupPosition 6 中的复选框会自动被选中。我已经在哈希图中保存了复选框的状态。任何人都可以解释为什么会这样。
我的 childrenView 代码如下
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ChildHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.group_item, null);
holder = new ChildHolder();
holder.cb = (CheckBox) convertView.findViewById(R.id.cb);
holder.title = (TextView) convertView.findViewById(R.id.title);
convertView.setTag(holder);
} else {
holder = (ChildHolder) convertView.getTag();
}
Item item = getChild(groupPosition, childPosition);
holder.title.setText(item.name);
if (DataHolder.selectedGroupMembers.containsKey(groupPosition) == true)
holder.cb.setChecked(true);
else {
holder.cb.setChecked(false);
}
return convertView;
}
我的 groupView 代码如下
public View getGroupView(final int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
final GroupHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.group_list, null);
holder = new GroupHolder();
holder.cb = (CheckBox) convertView.findViewById(R.id.cb);
holder.imageView = (ImageView) convertView
.findViewById(R.id.label_indicator);
holder.title = (TextView) convertView.findViewById(R.id.title);
convertView.setTag(holder);
} else {
holder = (GroupHolder) convertView.getTag();
}
holder.imageView
.setImageResource(groupStatus[groupPosition] == 0 ? R.drawable.group_down
: R.drawable.group_up);
Item groupItem = getGroup(groupPosition);
holder.title.setText(groupItem.name);
if (DataHolder.selectedGroupMembers.containsKey(groupPosition) == true) {
holder.cb.setChecked(true);
} else {
holder.cb.setChecked(false);
}
holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked == true) {
DataHolder.selectedGroupMembers.put(groupPosition,
new Boolean(true));
Log.i("Prativa Clicked", "group" + groupPosition);
notifyDataSetChanged();
} else if (DataHolder.selectedGroupMembers
.containsKey(groupPosition)) {
DataHolder.selectedGroupMembers.remove(groupPosition);
Log.i("Prativa Removed ", "group" + groupPosition);
notifyDataSetChanged();
}
}
});
return convertView;
}
我保存了哈希映射的状态如下
public static HashMap<Integer,Boolean> selectedGroupMembers = new HashMap<Integer, Boolean>();