ExpandableListView二級列表購物車部分功能實現思路
阿新 • • 發佈:2018-12-03
//全選計算價格
//全選 checkAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (b){ setCheckAll(1); }else { setCheckAll(0); } } });
public void setCheckAll(int s){ int groupCount = expanAdapter.getGroupCount(); for (int i = 0; i <groupCount ; i++) { ShopCarBean.DataBean groupData = (ShopCarBean.DataBean) expanAdapter.getGroup(i); if (s == 1){ groupData.setChecked(true); }else { groupData.setChecked(false); } List<ShopCarBean.DataBean.ListBean> childData = groupData.getList(); for (int j = 0; j < childData.size(); j++) { ShopCarBean.DataBean.ListBean childList = childData.get(j); childList.setSelected(s); } } expanAdapter.notifyDataSetChanged(); getTotal(); }
//計算總價 public void getTotal(){ double total =0; int sum = 0; int groupCount = expanAdapter.getGroupCount(); for (int i = 0; i < groupCount; i++) { ShopCarBean.DataBean groupData = (ShopCarBean.DataBean) expanAdapter.getGroup(i); List<ShopCarBean.DataBean.ListBean> childData = groupData.getList(); for (int j = 0; j < childData.size(); j++) { ShopCarBean.DataBean.ListBean childList = childData.get(j); if(childList.isChecked()){ double price = childList.getPrice(); total = total + price * childList.getNum(); sum = sum + childList.getNum(); } } } //設定總價內容 tv_total.setText("合計:"+total); tv_buy.setText("去結算("+sum+")"); }
//父條目與子條目的點選事件(在xml中設定CheckBox失去焦點)
//父條目點選事件 expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) { ShopCarBean.DataBean groupData = (ShopCarBean.DataBean) expanAdapter.getGroup(i); //判斷自身反選 groupData.setChecked(!groupData.isChecked()); int c = 0; if(groupData.isChecked()){ c = 1; } List<ShopCarBean.DataBean.ListBean> childData = groupData.getList(); for (int j = 0; j <childData.size() ; j++) { ShopCarBean.DataBean.ListBean childList = childData.get(j); childList.setSelected(c); } expanAdapter.notifyDataSetChanged(); getTotal(); return true; } }); //子條目點選事件 expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) { ShopCarBean.DataBean.ListBean childData = (ShopCarBean.DataBean.ListBean) expanAdapter.getChild(i, i1); //判斷自身反選 boolean checked = childData.isChecked(); if(checked){ childData.setSelected(0); }else { childData.setSelected(1); } expanAdapter.notifyDataSetChanged(); getTotal(); return true; } });