public classMainActivityextendsAppCompatActivity {privateExpandableListViewlistview;privateMyAdpateradpater;privateTextViewchecked_shop;privateTextViewprice;@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);
listview= (ExpandableListView) findViewById(R.id.listview);
adpater=newMyAdpater(this);listview.setAdapter(adpater);finalCheckBox checkAll = (CheckBox) findViewById(R.id.checkAll);price= (TextView) findViewById(R.id.price);checked_shop= (TextView) findViewById(R.id.checked_shop);checkAll.setOnClickListener(newView.OnClickListener() {@Overridepublic voidonClick(View v) {//設(shè)置商品全部選中adpater.checkAllShop(checkAll.isChecked());//計算選中的價格和數(shù)量String shopPrice =adpater.getShopPrice();//判斷商品是否全部選中booleanb =adpater.selectAll();String[] split = shopPrice.split(",");price.setText(split[0]);checked_shop.setText(split[1]);checkAll.setChecked(b);}? ? ? ? });adpater.getAdapterData(newMyAdpater.AdapterData() {@Overridepublic voidData(View v,String str, booleanb) {? ? ? ? ? ? ? ? String[] split = str.split(",");price.setText(split[0]);checked_shop.setText(split[1]);checkAll.setChecked(b);}? ? ? ? });checkAll.setChecked(adpater.selectAll());adpater.notifyDataSetChanged();}}
//適配器
public classMyAdpaterextendsBaseExpandableListAdapter {privateContextcontext;privateString[]group;privateString[][]child;privateHashMap
groupHashMap;privateList>childList;privateList>dataList;publicMyAdpater(Context context) {this.context= context;initData();}private voidinitData() {group=newString[5];child=newString[5][];groupHashMap=newHashMap<>();childList=newArrayList<>();dataList=newArrayList<>();for(inti =0;i <5;i++) {group[i] ="商家"+ i;groupHashMap.put(i, false);String[] strings =newString[3];HashMap map =newHashMap<>();ArrayList been =newArrayList<>();for(inty =0;y <3;y++) {? ? ? ? ? ? ? ? strings[y] ="商家"+ i +"商品"+ y;map.put(y, false);Bean bean =newBean("100","1");been.add(bean);}child[i] = strings;childList.add(map);dataList.add(been);}? ? }@Overridepublic intgetGroupCount() {returngroup.length;}@Overridepublic intgetChildrenCount(intgroupPosition) {returnchild[groupPosition].length;}@OverridepublicObjectgetGroup(intgroupPosition) {returngroup[groupPosition];}@OverridepublicObjectgetChild(intgroupPosition, intchildPosition) {returnchild[childPosition];}@Overridepublic longgetGroupId(intgroupPosition) {returngroupPosition;}@Overridepublic longgetChildId(intgroupPosition, intchildPosition) {returnchildPosition;}@Overridepublic booleanhasStableIds() {return false;}@OverridepublicViewgetGroupView(final intgroupPosition, booleanisExpanded,View convertView,ViewGroup parent) {? ? ? ? GroupViewHolder holder =null;if(convertView ==null) {? ? ? ? ? ? convertView = View.inflate(context,R.layout.group_item, null);holder =newGroupViewHolder();holder.tv= (TextView) convertView.findViewById(R.id.group_tv);holder.ck= (CheckBox) convertView.findViewById(R.id.group_ck);convertView.setTag(holder);}else{? ? ? ? ? ? holder = (GroupViewHolder) convertView.getTag();}? ? ? ? holder.tv.setText(group[groupPosition]);holder.ck.setOnClickListener(newView.OnClickListener() {@Overridepublic voidonClick(View v) {groupHashMap.put(groupPosition,!groupHashMap.get(groupPosition));//設(shè)置二級列表的選中狀態(tài),根據(jù)一級列表的狀態(tài)來改變setChildCheckAll();//計算選中的價格和數(shù)量String shopPrice = getShopPrice();//判斷商品是否全部選中booleanb = selectAll();adapterData.Data(v,shopPrice,b);}? ? ? ? });holder.ck.setChecked(groupHashMap.get(groupPosition));returnconvertView;}@OverridepublicViewgetChildView(final intgroupPosition, final intchildPosition,booleanisLastChild,View convertView,ViewGroup parent) {? ? ? ? ChildViewHolder holder =null;if(convertView ==null) {? ? ? ? ? ? convertView = View.inflate(context,R.layout.child_item, null);holder =newChildViewHolder();holder.tv= (TextView) convertView.findViewById(R.id.child_tv);holder.ck= (CheckBox) convertView.findViewById(R.id.child_ck);holder.jianshao= (TextView) convertView.findViewById(R.id.jianshao);holder.zengjia= (TextView) convertView.findViewById(R.id.zengjia);holder.number= (TextView) convertView.findViewById(R.id.number);convertView.setTag(holder);}else{? ? ? ? ? ? holder = (ChildViewHolder) convertView.getTag();}? ? ? ? holder.tv.setText(child[groupPosition][childPosition]);holder.ck.setOnClickListener(newView.OnClickListener() {@Overridepublic voidonClick(View v) {? ? ? ? ? ? ? ? HashMap hashMap =childList.get(groupPosition);hashMap.put(childPosition,!hashMap.get(childPosition));//判斷二級列表是否全部選中ChildisChecked(groupPosition);//計算選中的價格和數(shù)量String shopPrice = getShopPrice();//判斷商品是否全部選中booleanb = selectAll();adapterData.Data(v,shopPrice,b);}? ? ? ? });finalChildViewHolder finalHolder = holder;holder.zengjia.setOnClickListener(newView.OnClickListener() {@Overridepublic voidonClick(View v) {? ? ? ? ? ? ? ? List been =dataList.get(groupPosition);String num =finalHolder.number.getText().toString();inti = Integer.parseInt(num);++i;been.get(childPosition).setNumber(i +"");//計算選中的價格和數(shù)量String shopPrice = getShopPrice();//判斷商品是否全部選中booleanb = selectAll();adapterData.Data(v,shopPrice,b);notifyDataSetChanged();}? ? ? ? });holder.jianshao.setOnClickListener(newView.OnClickListener() {@Overridepublic voidonClick(View v) {? ? ? ? ? ? ? ? List been =dataList.get(groupPosition);String num =finalHolder.number.getText().toString();inti = Integer.parseInt(num);if(i >1) {? ? ? ? ? ? ? ? ? ? --i;}? ? ? ? ? ? ? ? been.get(childPosition).setNumber(i +"");//計算選中的價格和數(shù)量String shopPrice = getShopPrice();//判斷商品是否全部選中booleanb = selectAll();adapterData.Data(v,shopPrice,b);notifyDataSetChanged();}? ? ? ? });holder.number.setText(dataList.get(groupPosition).get(childPosition).getNumber().toString());holder.ck.setChecked(childList.get(groupPosition).get(childPosition));returnconvertView;}@Overridepublic booleanisChildSelectable(intgroupPosition, intchildPosition) {return false;}classGroupViewHolder {? ? ? ? TextViewtv;CheckBoxck;}classChildViewHolder {? ? ? ? TextViewtv;CheckBoxck;TextViewjianshao;TextViewzengjia;TextViewnumber;}//設(shè)置二級列表的選中狀態(tài),根據(jù)一級列表的狀態(tài)來改變private voidsetChildCheckAll() {for(inti =0;i integerBooleanHashMap1 =childList.get(i);Set> entries = integerBooleanHashMap1.entrySet();for(Map.Entry entry : entries) {? ? ? ? ? ? ? ? entry.setValue(groupHashMap.get(i));}? ? ? ? }? ? ? ? notifyDataSetChanged();}//判斷二級列表是否全部選中private voidChildisChecked(intgroupPosition) {booleanischecked =true;HashMap hashMap =childList.get(groupPosition);Set> entries = hashMap.entrySet();for(Map.Entry entry : entries) {if(!entry.getValue()) {? ? ? ? ? ? ? ? ischecked =false;break;}? ? ? ? }groupHashMap.put(groupPosition,ischecked);notifyDataSetChanged();}//全選public voidcheckAllShop(booleanchecked) {? ? ? ? Set> entries =groupHashMap.entrySet();for(Map.Entry entry : entries) {? ? ? ? ? ? entry.setValue(checked);}//調(diào)用讓二級列表全選的方法setChildCheckAll();notifyDataSetChanged();}//計算價格publicStringgetShopPrice() {intprice =0;intnumber =0;for(inty =0;y integerBooleanHashMap1 =childList.get(y);Set> entries = integerBooleanHashMap1.entrySet();for(Map.Entry entry : entries) {if(entry.getValue()) {? ? ? ? ? ? ? ? ? ? Bean bean =dataList.get(y).get(entry.getKey());price += Integer.parseInt(bean.getPrice()) * Integer.parseInt(bean.getNumber());number += Integer.parseInt(bean.getNumber());}? ? ? ? ? ? }? ? ? ? }returnprice +","+ number;}//編輯一級和二級列表,如果全部選中,全選按鈕也選中public booleanselectAll() {booleanisChecked =true;for(inty =0;y hashMap =childList.get(y);Set> entries = hashMap.entrySet();for(Map.Entry entry : entries) {if(!entry.getValue()) {? ? ? ? ? ? ? ? ? ? isChecked =false;break;}? ? ? ? ? ? }? ? ? ? }returnisChecked;}privateAdapterDataadapterData;public interfaceAdapterData {voidData(View v,String str, booleanb);}public voidgetAdapterData(AdapterData adapterData) {this.adapterData= adapterData;}}
bean
public classBean {privateStringprice;privateStringnumber;publicBean(String price,String number) {this.price= price;this.number= number;}publicStringgetPrice() {returnprice;}public voidsetPrice(String price) {this.price= price;}publicStringgetNumber() {returnnumber;}public voidsetNumber(String number) {this.number= number;}}