android开发购物车加减,55.使用ExpandableListView实现购物车

elected);//点击事件holderP.checkParentGwc.setOnClickListener(newView.OnClickListener() {

@Overridepublic voidonClick(View v) {

//点击商家得checkBoxif(mMyOnClickListener!= null) {

mMyOnClickListener.onCheckedAllChange(groupPosition);}

}

});returnconvertView;}

//父类的ViewHolderpublic static classViewHolder {

publicCheckBox checkParentGwc;publicTextView txtNamePareatGwc;publicViewHolder(View rootView) {

this.checkParentGwc= rootView.findViewById(R.id.check_parent_gwc);this.txtNamePareatGwc= rootView.findViewById(R.id.txt_name_pareat_gwc);}

}

//二级列表 子列表@OverridepublicView getChildView(final intgroupPosition, final intchildPosition, booleanisLastChild,View convertView,ViewGroup parent) {

//首先得到data数据GWCShopBean.DataBean dataBean = list.get(groupPosition);//得到listList list = dataBean.getList();//得到子布局中的数据GWCShopBean.DataBean.ListBean listBean = list.get(childPosition);GWCShopBean.DataBean.ListBean listBean1 = list.get(0);ViewHolder2 holderC;if(convertView == null) {

convertView = View.inflate(mContext,R.layout.item_gwc_expandable_child, null);holderC = newViewHolder2(convertView);convertView.setTag(holderC);}else{

holderC = (ViewHolder2) convertView.getTag();}

//赋值if(listBean!=null){

//是否选中holderC.checkChild.setChecked(listBean.getSelected() == 1);//图片String images = listBean1.getImages();String[] split = images.split("\\|");Glide.with(mContext).load(HttpsToHttp.myHttpsToHttp(split[0])).into(holderC.imgProductChild);//标题holderC.txtProductTitle.setText(listBean.getTitle());//价格holderC.txtProductPrice.setText(listBean.getPrice()+"");holderC.addRemoveView.setNumber(listBean.getNum());holderC.checkChild.setOnClickListener(newView.OnClickListener() {

@Overridepublic voidonClick(View v) {

if(mMyOnClickListener!=null){

mMyOnClickListener.onProductCheckedChange(groupPosition,childPosition);}

}

});//为加减器设置点击接口回调holderC.addRemoveView.setMyOnClickListener(newMyAddAndRemove.myOnClickListener() {

@Overridepublic voidonNumChanger(intnum) {

//拿到商品数量if(mMyOnClickListener!=null){

mMyOnClickListener.onProductNumChange(groupPosition,childPosition,num);}

}

});}

returnconvertView;}

//子类的ViewHolderclassViewHolder2 {

publicView rootView;publicCheckBox checkChild;publicImageView imgProductChild;publicTextView txtProductTitle;publicTextView txtProductPrice;publicMyAddAndRemove addRemoveView;publicViewHolder2(View rootView) {

this.rootView= rootView;this.checkChild= rootView.findViewById(R.id.check_child);this.imgProductChild= rootView.findViewById(R.id.img_product_child);this.txtProductTitle= rootView.findViewById(R.id.txt_product_title);this.txtProductPrice= rootView.findViewById(R.id.txt_product_price);this.addRemoveView= rootView.findViewById(R.id.add_remove_view);}

}

//3.1当前商家所有商品是否被选中public booleanisCurrentSellerAllProductSelected(intgroupPosition) {

//通过List得到商家databeanGWCShopBean.DataBean dataBean = list.get(groupPosition);//拿到商家所有的商品List listBeans = dataBean.getList();//Bean类中有一个selected来判断是否选中,未选中是0//遍历商家中的所有数据for(GWCShopBean.DataBean.ListBean listBean : listBeans) {

//只要有一个未选中,商家就直接未选中if(listBean.getSelected() == 0) {

return false;}

}

return true;}

//3.2判断所有商品是否被选中,使用双重for循环,取双重集合中的数据public booleanisAllProductsSelected() {

for(inti = 0;i < list.size();i++) {

//得到商家数据GWCShopBean.DataBean dataBean = list.get(i);//得到所有商家商品List listBeans = dataBean.getList();//遍历所有商品for(intj = 0;j < listBeans.size();j++) {

if(listBeans.get(j).getSelected() == 0) {

//如果没有选中 就返回FALSEreturn false;}

}

}

return true;}

//3.3计算商品总的数量public intcalculateTotalNumber() {

inttotalNumber = 0;for(inti = 0;i < list.size();i++) {

GWCShopBean.DataBean dataBean = list.get(i);List listBeans = dataBean.getList();for(intj = 0;j < listBeans.size();j++) {

//只要是选中状态if(listBeans.get(j).getSelected() == 1) {

//如果选中了 就得到本商品的数量intnum = listBeans.get(j).getNum();//赋值给总价totalNumber += num;}

}

}

returntotalNumber;}

//3.4计算总价public floatcalculateTotalPrice() {

floattotalPrice = 0;//遍历所有数据for(inti = 0;i < list.size();i++) {

GWCShopBean.DataBean dataBean = list.get(i);List listBeans = dataBean.getList();for(intj = 0;j < listBeans.size();j++) {

//只要是选中状态if(listBeans.get(j).getSelected() == 1) {

//如果商品被选中了 就得到他的价格 和 数量floatprice = listBeans.get(j).getPrice();intnum = listBeans.get(j).getNum();//重新赋值给总价//注 : 总价=商品单价*商品数量totalPrice += price * num;}

}

}

returntotalPrice;}

//3.2.1当 商家 得checkbox被点击得时候调用,//设置当前商家得所有商品得状态--------商家组其所有的商品public voidchangeCurrentSellerAllProductsStatus(intgroupPosition, booleanisSelected) {

GWCShopBean.DataBean dataBean = list.get(groupPosition);List listBeans = dataBean.getList();for(inti = 0;i < listBeans.size();i++) {

//得到当前商品的所有数据GWCShopBean.DataBean.ListBean listBean = listBeans.get(i);//使用三元运算符 返回的1或0代表的是 是否选中listBean.setSelected(isSelected ? 1: 0);}

}

//3.2.2当 商品 得checkbox被点击得时候调用,//改变当前商品状态---------商品子条目public voidchangeCurrentProductStatus(intgroupPosition, intchildPosition) {

GWCShopBean.DataBean dataBean = list.get(groupPosition);List listBeans = dataBean.getList();GWCShopBean.DataBean.ListBean listBean = listBeans.get(childPosition);//使用三元运算符 返回的1或0代表的是 是否选中listBean.setSelected(listBean.getSelected() == 0? 1: 0);}

//3.2.3设置所有商品得状态public voidchangeAllProductsStatus(booleanselected) {

//遍历所有商品for(inti = 0;i < list.size();i++) {

//得到当前的商家数据GWCShopBean.DataBean dataBean = list.get(i);//得到当前商家下的所有商品数据List listBeans = dataBean.getList();for(intj = 0;j < listBeans.size();j++) {

listBeans.get(j).setSelected(selected?1:0);}

}

}

//3.2.4 //当加减器被点击得时候调用,改变当前商品得数量public voidchangeCurrentProductNumber(intgroupPosition, intchildPosition, intnumber) {

GWCShopBean.DataBean dataBean = list.get(groupPosition);List listBeans = dataBean.getList();GWCShopBean.DataBean.ListBean listBean = listBeans.get(childPosition);listBean.setNum(number);}

@Overridepublic booleanisChildSelectable(intgroupPosition, intchildPosition) {

return false;}

@OverridepublicObject getGroup(intgroupPosition) {

return null;}

@OverridepublicObject getChild(intgroupPosition, intchildPosition) {

return null;}

@Overridepublic longgetGroupId(intgroupPosition) {

return0;}

@Overridepublic longgetChildId(intgroupPosition, intchildPosition) {

return0;}

@Overridepublic booleanhasStableIds() {

return false;}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值