我正在使用 ExpandableListView
。在组和子项中,都有复选框。如何做下面的需求?
当用户选中组的复选框时,选中其子项的所有复选框。
我想我需要覆盖 BaseExpandableListAdapter
,
但我不知道应该填写什么。谢谢。
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
CheckBox cb = (CheckBox)v.findViewById(R.id.checkBox1);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
{
//Fill in what?
}
});
}
有一个类似的问题,但我不明白它的解决方案: How to get all childs view in group with ExpandableListView
最佳答案
我认为链接的答案是说您应该在数组列表中跟踪已检查组的状态。然后,在您的 BaseExpandableListAdapter
的 getChildView
方法中,您将检查 child 的组是否存在。
这是我的实现,我使用的是 HashMap 而不是数组列表:
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
CheckBox check__bookmark = (CheckBox) child_row.findViewById(R.id._checkBox);
if (mapp.group_bookmark_s_checked.containsKey((groupPosition)) == true)
check__bookmark.setChecked(true);
else
check__bookmark.setChecked(false);
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
CheckBox PlayGroupSelected =
(CheckBox) group_row.findViewById(R.id.bookmark_group_checkBox);
if (mapp.group_bookmark_s_checked.containsKey((groupPosition)) == true)
PlayGroupSelected.setChecked(true);
else
PlayGroupSelected.setChecked(false);
PlayGroupSelected.setOnCheckedChangeListener(
new BookmarkGroupCheckedChangeListener(groupPosition, mapp, group_row));
}
现在在您的监听器中,您可以通过删除和添加状态对象来管理状态。我使用了 notifyDataSetChanged()
,因为一旦绘制了检查状态更改,子项就不会刷新。不确定这是否是解决该问题的正确方法,但它确实起到了作用。
class BookmarkGroupCheckedChangeListener implements OnCheckedChangeListener {
private int mPosition;
MyApplication mapp;
RelativeLayout mgroup_row;
BookmarkGroupCheckedChangeListener(int position, MyApplication app, RelativeLayout group_row) {
mapp = app;
mPosition = position;
mgroup_row = group_row;
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked == true) {
mapp.group_bookmark_s_checked.put((mPosition), new Boolean(true));
Log.v("DEBUG:", "Group Bookmark Checked On at " + mPosition);
notifyDataSetChanged();
} else if (mapp.group_bookmark_s_checked.containsKey(mPosition)) {
mapp.group_bookmark_s_checked.remove((mPosition));
Log.v("DEBUG:", "Checked Off at " + mPosition);
notifyDataSetChanged();
}
}
}
关于java - 选中 ExpandableListView (Android) 中的所有复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7215243/