我在ExpandableListView
中的所有子视图元素中使用复选框。要一次清除所有复选框,我使用的是clearFilters按钮,Adapter类的代码和按钮单击事件如下所示。
点击按钮根本不做任何事情。我想在点击按钮时清除所有复选框。非常感谢任何帮助。
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类:
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();
}
});
}
}
答案 0 :(得分:0)
如果动机只是取消选中所有复选框,我们可以使用此技术迭代Hashmap并将所有选中状态设置为false。它将清除所有childView中的所有复选框。
public void clearChecks(){
for (List<ChildViewModel> value:expandableListDetail.values()) {
for(ChildViewModel sample:value){
sample.setCheckStatus(false);
}
}
notifyDataSetChanged();