[英]Android: Expandable ListView with parent checkbox and child checkbox
我想创建一个具有parent checkboxes
和child checkboxes
的expandable listview
。 每当选择parent checkbox
,所有child checkboxes
也都应选中。 同样,无论何时选中/取消选中child checkboxes
, parent checkboxes
都应表现适当。
我在参考此链接并使用组和子复选框创建可扩展的列表视图之后尝试使用以下Adapter
代码。
public class FilterListExpandableListItemAdapter extends BaseExpandableListAdapter {
private Map<ExpListGroupItem, List<ExpListChildItem>> groupChild;
private List<ExpListGroupItem> groups;
private Activity context;
private ExpandableListView elv;
public FilterListExpandableListItemAdapter(Activity context, List<ExpListGroupItem> cities,
Map<ExpListGroupItem, List<ExpListChildItem>> cityArea, ExpandableListView elv) {
this.context = context;
this.groupChild = cityArea;
this.groups = cities;
this.elv = elv;
}
@Override
public int getGroupCount() {
return groups.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return groupChild.get(groups.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return groupChild.get(groups.get(groupPosition)).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ExpListGroupItem group = (ExpListGroupItem) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.filter_drawer_list_parent_item,
null);
}
GroupViewHolder groupHolder = new GroupViewHolder();
groupHolder.parentCheckBox = (CheckBox) convertView.findViewById(R.id.chkbox_filter_drawer_city);
groupHolder.parentCheckBox.setTypeface(null, Typeface.BOLD);
groupHolder.parentCheckBox.setText(group.getGroupName());
groupHolder.parentCheckBox.setSelected(group.isSelected());
GroupCheckChangedListener groupCheckChangedListener = new GroupCheckChangedListener();
groupHolder.parentCheckBox.setOnCheckedChangeListener(groupCheckChangedListener);
convertView.setTag(groupHolder);
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent){
final ExpListChildItem child = (ExpListChildItem) getChild(groupPosition, childPosition);
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.filter_drawer_list_child_item, null);
}
ChildViewHolder childHolder = new ChildViewHolder();
childHolder.childCheckBox =(CheckBox) convertView.findViewById(R.id.chkbox_filter_drawer_area);
childHolder.childCheckBox.setSelected(child.isSelected());
childHolder.childCheckBox.setText(child.getChildName());
convertView.setTag(childHolder);
return convertView;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
static class ChildViewHolder {
CheckBox childCheckBox;
}
static class GroupViewHolder {
CheckBox parentCheckBox;
}
private class GroupCheckChangedListener implements CheckBox.OnCheckedChangeListener{
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
ExpListGroupItem group = new ExpListGroupItem();
CheckBox chk = (CheckBox) compoundButton;
group.setGroupName(chk.getText().toString());
for(ExpListChildItem item: groupChild.get(group)){
item.setIsSelected(true);
}
notifyDataSetChanged();
}
}}
这里的问题是我已经检查了debugger option
。 我的基础数据已更改。 但是没有反映在UI
。 即使我检查父母,也不会选择相应的孩子。
有人可以建议我在这里做错什么吗? 这是处理所需方案的最佳方法吗?
在您的getChildView方法中,您应该检查是否选择了parentcheckbox,如果是,则
childHolder.childCheckBox.setSelected(true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.