全部清除按钮不会取消选中 ExpandableListView android 中的所有框?

Clear All button doesn't uncheck all boxes in a ExpandableListView android?

我在 ExpandableListView 的所有子视图元素中使用复选框。要一次性清除所有复选框,我使用按钮 clearFilters 和适配器代码 class,按钮单击事件如下。

点击按钮根本没有任何反应。我想在按钮为 clicked.Any 时清除所有复选框,非常感谢帮助。

ExpandableListAdapterClass:

public class ExpandableListViewAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<String> expandableListTitle;
    private Map<String, List<ChildViewModel>> expandableListDetail;
    private static List<ChildViewHolder> checkedViewHolders=new ArrayList<>();
    static int checkedBoxesCount;

    public ExpandableListViewAdapter(Context context, List<String> expandableListTitle, Map<String,
            List<ChildViewModel>> expandableListDetail) {
        this.context = context;
        this.expandableListTitle = expandableListTitle;
        this.expandableListDetail = expandableListDetail;
    }
    @Override
    public int getGroupCount() {
        return expandableListTitle.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return expandableListDetail.get(expandableListTitle.get(groupPosition)).size();
    }

    @Override
    public String getGroup(int groupPosition) {
        return expandableListTitle.get(groupPosition);
    }

    @Override
    public ChildViewModel getChild(int groupPosition, int childPosition) {
        return expandableListDetail.get(expandableListTitle.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 false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean b, View view, ViewGroup viewGroup) {
        String listTitle=getGroup(groupPosition);
        GroupViewHolder groupViewHolder;
        if(view==null){
            LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view=inflater.inflate(R.layout.expanded_list_group,null);
            groupViewHolder=new GroupViewHolder();
            groupViewHolder.listTitleTextView=(TextView)view.findViewById(R.id.txtExpandedListTitle);
            view.setTag(groupViewHolder);
        }else {
            groupViewHolder=(GroupViewHolder)view.getTag();
        }
        groupViewHolder.listTitleTextView.setText(listTitle);
        return view;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean b, View view, ViewGroup viewGroup) {
        final ChildViewModel expandedListText=getChild(groupPosition,childPosition);
        final ChildViewHolder childViewHolder;
        if(view==null){
            LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view=inflater.inflate(R.layout.expanded_list_item,null);
            childViewHolder=new ChildViewHolder();
            childViewHolder.expandedListTextView=(TextView)view.findViewById(R.id.txtExpandedListItem);
            childViewHolder.checkBox=(CheckBox)view.findViewById(R.id.expandeditem_chkbox);
            view.setTag(childViewHolder);
        }else {
            childViewHolder=(ChildViewHolder)view.getTag();
        }
        childViewHolder.expandedListTextView.setText(expandedListText.getName());
        if(expandedListText.isCheckStatus()){
            childViewHolder.checkBox.setChecked(true);
        }else {
            childViewHolder.checkBox.setChecked(false);
        }

        childViewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ChildViewModel model;
                if(childViewHolder.checkBox.isChecked()){
                    checkedBoxesCount++;
                    model=expandableListDetail.get(expandableListTitle.get(groupPosition)).get(childPosition);
                    model.setCheckStatus(true);
                    expandableListDetail.get(expandableListTitle.get(groupPosition)).set(childPosition,model);
                    checkedViewHolders.add(childViewHolder);
                    notifyDataSetChanged();
                    Toast.makeText(context,"Checked value is"+expandableListDetail.get(expandableListTitle.get(groupPosition)).get(childPosition),Toast.LENGTH_SHORT).show();
                }else {
                    checkedBoxesCount--;
                    if(checkedBoxesCount==0){
                        Toast.makeText(context,"nothing checked",Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(context,"unchecked",Toast.LENGTH_SHORT).show();
                    }
                    model=expandableListDetail.get(expandableListTitle.get(groupPosition)).get(childPosition);
                    model.setCheckStatus(false);
                    checkedViewHolders.remove(childViewHolder);
                    expandableListDetail.get(expandableListTitle.get(groupPosition)).set(childPosition,model);
                    notifyDataSetChanged();
                }
            }
        });
        return view;
    }

    public void clearChecks(){
        for(int i=0;i<checkedViewHolders.size();i++){
            checkedViewHolders.get(i).checkBox.setChecked(false);
            notifyDataSetChanged();
        }
        checkedViewHolders.clear();
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public class GroupViewHolder {
        TextView listTitleTextView;
    }

    public class ChildViewHolder {
        TextView expandedListTextView;
        CheckBox checkBox;
    }
}

这是 MainActivity class:

public class MainActivity extends AppCompatActivity {

    ExpandableListView mExpandableListView;

    ExpandableListViewAdapter mExpandableListAdapter;
    Button clearFilters;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mExpandableListView=(ExpandableListView)findViewById(R.id.expandedListView);
        clearFilters=(Button)findViewById(R.id.btnClearFilter);


        mExpandableListAdapter=new ExpandableListViewAdapter(MainActivity.this,getTitles(),getNames());
        mExpandableListView.setAdapter(mExpandableListAdapter);

        clearFilters.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mExpandableListAdapter.clearChecks();
            }
        });
    }
}

如果动机是简单地取消选中所有复选框,我们可以使用此技术遍历 Hashmap 并将所有选中状态设置为 false。它将清除所有子视图中的所有复选框。

public void clearChecks(){
        for (List<ChildViewModel> value:expandableListDetail.values()) {
            for(ChildViewModel sample:value){
                sample.setCheckStatus(false);
            }
        }
        notifyDataSetChanged();