【问题标题】:How to uncheck all the checkboxes in expandable listview?如何取消选中可扩展列表视图中的所有复选框?
【发布时间】:2015-12-09 08:51:49
【问题描述】:

我想取消选中可扩展列表视图子视图中的所有复选框,但问题是它只取消选中视图组中的可见复选框。不可见的复选框不会被取消选中。请帮帮我。

public void uncheckAllChildrenCascade(ViewGroup vg) {
    for (int i = 0; i < vg.getChildCount(); i++) {
        View v = vg.getChildAt(i);
        if (v instanceof CheckBox) {
            ((CheckBox) v).setChecked(false);
        } else if (v instanceof ViewGroup) {
            uncheckAllChildrenCascade((ViewGroup) v);
        }
    }
}

【问题讨论】:

    标签: android checkbox expandablelistview


    【解决方案1】:

    您只取消选中可见复选框,因为您使用 ViewHolder。 您没有列表或其他东西来存储您的数据吗? 更改您的列表并致电notifyDataSetChanged()

    【讨论】:

    • 我有 Hash Map 来存储选中的复选框
    【解决方案2】:
    1. 为父复选框维护一个 ArrayList。
    2. 为 getGroupView 中的父复选框添加侦听器。
    3. 如果选中,则将位置的 ArrayList 值设置为 true,否则将其设置为 false。
    4. 现在在 getGroupView 内的此复选框侦听器中调用 notifydatasetchanged。
    5. 在 getChildView 中,如果 ArrayList 在当前 groupPosition 为 true,则选中子复选框,否则取消选中。 我希望这是你需要的。

      @Override
      public View getGroupView(final int groupPosition, boolean isExpanded,View convertView, final ViewGroup parent) {
         if (convertView == null) {
           LayoutInflater infalInflater = (LayoutInflater)this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           convertView = infalInflater.inflate(R.layout.row_list_header,null);
       }
      CheckBox chkGroup = (CheckBox) convertView.findViewById(R.id.chkGroup);
      chkGroup.setOnClickListener(new OnClickListener() {
           public void onClick(View v) {
              CheckBox cb = (CheckBox) v.findViewById(R.id.chkGroup);
              if (cb.isChecked()) {
                 privateGroupChecked.set(groupPosition, true);
              else  {
                 privateGroupChecked.set(groupPosition, false);
               }
               notifyDataSetChanged();
             }
          });
        return convertView;
      }
      
      @Override
      public View getChildView(final int groupPosition,final int childPosition, boolean isLastChild, View convertView,ViewGroup parent) {if (convertView == null) {
          LayoutInflater infalInflater = (LayoutInflater)this._context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          convertView = infalInflater.inflate(R.layout.row_list_child, null);
          }
          boolean isGroupChecked = privateGroupChecked.get(groupPosition);
          if (isGroupChecked) {
          chkDevice.setChecked(isGroupChecked);
           }
           else
           {
             chkDevice.setChecked(!isGroupChecked);
           }
           return convertView;
          }
      

    【讨论】:

    • 什么是 privateGroupChecked?
    【解决方案3】:

    将复选框存储在 arrayList 中:

      final ArrayList<CheckBox> myCheckBoxes = new ArrayList<>();
        final CheckBox checkBox = new CheckBox(this);
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkBox.isSelected()){
                    myCheckBoxes.remove(checkBox);
                }else{
                    myCheckBoxes.add(checkBox);
                }
            }
        });
    

    然后将Checked设置为false:

     for (int i = 0; i < myCheckBoxes.size(); i++) {
            myCheckBoxes.get(i).setSelected(false);
        }
    

    你有没有选中所有的复选框:)

    【讨论】:

    • 实际上我在主要活动的可扩展列表视图的页脚中有一个清除按钮..当我单击该按钮时,会调用此方法 uncheckAllChildrenCascade() 并在其中传递可扩展列表视图..
    • 方法在Adapter类中
    • 只需在您的适配器上制作一个复选框 onClickListener,当您单击复选框时将其添加到 arrayList。当您单击 clean 按钮时,选中 ArrayList 的复选框并将选中的设置为 false。
    • 我正在使用 setOnCheckedChangeListener() 并存储在 arrayList 中,但我在复选框中得到垃圾值...
    • “复选框中的垃圾值”是什么意思?
    【解决方案4】:
    private final Set<Pair<Long, Long>> mCheckedItems = new HashSet<Pair<Long, Long>>();
    final CheckBox cb = (CheckBox) convertView.findViewById(R.id.cb_checkbox);
        RelativeLayout relativelayout=(RelativeLayout) convertView.findViewById(R.id.rl_selected);
        txtListChild.setText(childText);
        final Pair<Long, Long> tag = new Pair<Long, Long>(getGroupId(groupPosition), getChildId(groupPosition, childPosition));
        cb.setTag(tag);
            cb.setChecked(mCheckedItems.contains(tag));
    relativelayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cb.isChecked()) {
                    cb.setChecked(false);
                    mCheckedItems.remove(tag);
                    Shop.iv_shopbyCategory.setVisibility(View.GONE);
                    Shop.tv_rl_selectedshopByCategory.setText(" ");
                }
                else
                    {
                        mCheckedItems.clear();
                        cb.setChecked(true);
                        mCheckedItems.add(tag);
                        final String SelectedGrouposition=(String) getGroup(groupPosition);
                        final String SelectedChildposition=(String) getChild(groupPosition, childPosition);
                       
                        listAdapter.notifyDataSetChanged();
                }
            }
        });
    

    我有一个可展开的列表视图,当我点击相对布局时,它会更改复选框选中或未选中,当我选择新检查时,它将删除所有选中的检查..

    对我来说它的工作,并希望它对你也有用......

    试试这个

    【讨论】:

      猜你喜欢
      • 2012-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多