【问题标题】:Android: Expandable ListView with parent checkbox and child checkboxAndroid:带有父复选框和子复选框的可扩展 ListView
【发布时间】:2016-02-08 06:59:59
【问题描述】:

我想用parent checkboxeschild checkboxes 创建一个expandable listview。只要选择parent checkbox,也应该选择所有child checkboxes。同样,无论何时选择/取消选择 child checkboxesparent checkboxes 的行为都应该是正确的。

参考此链接后,我尝试使用以下Adapter 代码 Create expandable listview with group and child checkbox.

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 上。 即使我检查父母,也没有选择相应的孩子。

有人可以建议我在这里做错了什么吗? 这是处理所需场景的最佳方式吗?

【问题讨论】:

    标签: android checkbox expandablelistview expandablelistadapter


    【解决方案1】:

    在您的 getChildView 方法中,您应该检查是否选择了 parentcheckbox,如果是则

    childHolder.childCheckBox.setSelected(true); 
    

    【讨论】:

    • 如何在getChildView中检查parentcheckbox是否被选中。
    猜你喜欢
    • 2011-09-15
    • 2012-12-11
    • 1970-01-01
    • 2014-05-28
    • 2016-02-03
    • 1970-01-01
    • 2011-07-22
    • 1970-01-01
    • 2017-07-09
    相关资源
    最近更新 更多