带有复选框的可扩展列表视图中的问题

时间:2016-07-26 05:15:06

标签: android checkbox expandablelistview

我的要求是当我同时选择父 CheckBox 选中的子 CheckBoxes 时。但是我点击了父复选框它没有被选中儿童checkbox.how这样做?可以提出建议。

这是我的代码

   public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, final ViewGroup parent) {
    final String headerTitle = (String)getGroup(groupPosition);
    ParentViewHolder cv = null;
    if (convertView == null)
    {
        LayoutInflater li = (LayoutInflater)Context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = li.inflate(R.layout.group_list,null);
      cv  = new ParentViewHolder();
       cv.text = (TextView)convertView.findViewById(R.id.groupTitle);
        cv.text.setTypeface(null, Typeface.BOLD);
        cv.text.setText(headerTitle);
        cv.parentCheckbox =       (CheckBox)convertView.findViewById(R.id.groupCheckbox);

        convertView.setTag(cv);
        cv.parentCheckbox.setTag(groupPosition);
        cv.parentCheckbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(((CheckBox)v).isChecked())
                {
                    Log.e("TAG","clicked");
                        groupitem.itemClick(groupPosition, true);
                    Toast.makeText(Context.getApplicationContext(),   headerTitle+"success", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
    else
    {

    }

        return convertView;
} 

1 个答案:

答案 0 :(得分:0)

我已根据您的要求制作了一些逻辑。你可以这样做:

 /*******************
 * Checkbox Checked Change Listener
 ********************/

private final class CheckUpdateListener implements CompoundButton.OnCheckedChangeListener {
    private final ParentModel parent;
    private  final ArrayList<ChildModel> childs;

    private CheckUpdateListener(ParentModel parent,ArrayList<ChildModel> childs) {
        this.parent = parent;
        this.childs=childs;


    }


    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Log.i("onCheckedChanged", "isChecked: " + isChecked);


        parent.setChecked(isChecked);
        for(int i=0;i<childs.size();i++)
        {
            childs.get(i).setChildChecked(isChecked);
        }
        Log.i("TAG", "CHILD SIZE=>" + parent.getChildList().size());


        notifyDataSetChanged();


    }
}


private class ChildCheckUpdateListener implements CompoundButton.OnCheckedChangeListener {
    private final ParentModel parentModel;
    private final ChildModel childModel;

    public ChildCheckUpdateListener(ChildModel child, ParentModel model) {
        this.parentModel = model;
        this.childModel = child;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        if (!parentModel.isChecked()) {
            childModel.setChildChecked(isChecked);
        }
        notifyDataSetChanged();
    }
}

你只需要从getGroupView和getChildview方法调用它,如下所示:

来自群组视图

  groupViewHolder.chkbox.setOnCheckedChangeListener(new CheckUpdateListener(parent,childs));
来自ChildView的

childViewHolder.childChk.setOnCheckedChangeListener(new ChildCheckUpdateListener(child, model));