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) {//设置商品全部选中adpater.checkAllShop(checkAll.isChecked());//计算选中的价格和数量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));//设置二级列表的选中状态,根据一级列表的状态来改变setChildCheckAll();//计算选中的价格和数量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);//计算选中的价格和数量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 +"");//计算选中的价格和数量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 +"");//计算选中的价格和数量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;}//设置二级列表的选中状态,根据一级列表的状态来改变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);}//调用让二级列表全选的方法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;}}