源码:
clear = (LinearLayout) findViewById(R.id.clear); clear.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { data.clear(); adapter.notifyDataSetChanged(); integral_sum.setText(0 + ""); checkBox_select_all.setChecked(false); // checkBox_add.setClickable(false); } }); checkBox_select_all = (CheckBox) findViewById(R.id.checkbox_add); checkBox_select_all.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { HashMap<Integer, Boolean> isSelected = ShoppingCartAdapter .getIsSelected(); Iterator iterator = isSelected.entrySet().iterator(); List<Boolean> array = new ArrayList<Boolean>();//列表中checkbox选中状态 List<Integer> nums = new ArrayList<Integer>();//列表中商品数量 while (iterator.hasNext()) { HashMap.Entry entry = (HashMap.Entry) iterator.next(); Integer key = (Integer) entry.getKey(); Boolean val = (Boolean) entry.getValue(); array.add(val); } for (int i = 0; i < data.size(); i++) { //int num = data.get(i).getNum(); int integral = Integer.valueOf(data.get(i).getIntegral()); // nums.add(num); } if (checkBox_select_all.isChecked()) { for (int i = 0; i < data.size(); i++) { ShoppingCartAdapter.getIsSelected().put(i, true); } //checkBox_add.setChecked(true); adapter.notifyDataSetChanged(); } else { for (int i = 0; i < data.size(); i++) { ShoppingCartAdapter.getIsSelected().put(i, false); } //checkBox_add.setChecked(false); adapter.notifyDataSetChanged(); integral_sum.setText(0 + ""); } } }); mListView= (ListView) findViewById(R.id.finance_list); mListView.setAdapter(adapter); mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { int pos = position - 1; // RecyclerView.ViewHolder viewHolder = (RecyclerView.ViewHolder) view.getTag(); // int num = data.get(pos).getNum(); int num = data.get(pos).getNum(); if (num == 0) { Toast.makeText(context, "请选择商品数量", Toast.LENGTH_LONG) .show(); } else { boolean cu = !ShoppingCartAdapter.getIsSelected().get(pos); ShoppingCartAdapter.getIsSelected().put(pos, cu); adapter.notifyDataSetChanged(); //遍历获取列表中checkbox的选中状态 HashMap<Integer, Boolean> isSelected = ShoppingCartAdapter .getIsSelected(); Iterator iterator = isSelected.entrySet().iterator(); List<Boolean> array = new ArrayList<Boolean>(); while (iterator.hasNext()) { HashMap.Entry entry = (HashMap.Entry) iterator.next(); Integer key = (Integer) entry.getKey(); Boolean val = (Boolean) entry.getValue(); array.add(val); } if (!isAllCheck(array)) { checkBox_select_all.setChecked(false); //checkBox_add.setChecked(false); } if (isAllCheck(array)) { checkBox_select_all.setChecked(true); // checkBox_add.setChecked(true); } // if (falsecount==1) { // checkBox_select_all.setChecked(false); // } // if (truecount==1) { // checkBox_add.setChecked(true); // } } } }); } private boolean isAllCheck( List<Boolean> array) {//是否全选 for (int i=0;i<array.size();i++) { if (array.get(i)==false) return false; } return true; } /** * 全选与反选 */ @SuppressLint("HandlerLeak") private Handler handler = new Handler(){ @SuppressWarnings("unchecked") @Override public void handleMessage(Message msg) { super.handleMessage(msg); if(msg.what == 10){ //更改选中商品的总价格 float price = (Float)msg.obj; if(price > 0){ integral_sum.setText(price+""); }else{ integral_sum.setText("0"); } } else if(msg.what == 11){ //列表选中状态 List<Boolean> array = (List<Boolean>) msg.obj; if (!isAllCheck(array)) { checkBox_select_all.setChecked(false); //checkBox_add.setChecked(false); } if (isAllCheck(array)) { checkBox_select_all.setChecked(true); // checkBox_add.setChecked(true); } } } };