<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:background="#ffffff" androID:orIEntation="vertical"> <linearLayout androID:ID="@+ID/ll_gouwuche" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:background="#ededed" androID:orIEntation="vertical"> <relativeLayout androID:layout_wIDth="match_parent" androID:layout_height="48dp" androID:background="#ffffff" androID:orIEntation="vertical"> <ImageVIEw androID:ID="@+ID/tv_Titlebar_left" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_centerVertical="true" androID:layout_marginleft="15dp" androID:padding="5dp" androID:src="@drawable/danghanglan_fanhui" /> <TextVIEw androID:ID="@+ID/tv_Titlebar_center" androID:layout_wIDth="200dp" androID:layout_height="match_parent" androID:layout_centerHorizontal="true" androID:ellipsize="end" androID:gravity="center" androID:maxLength="18" androID:singleline="true" androID:text="购物车" androID:textcolor="#2f302b" androID:textSize="17sp" androID:visibility="visible" /> <TextVIEw androID:ID="@+ID/tv_Titlebar_right" androID:layout_wIDth="wrap_content" androID:layout_height="match_parent" androID:layout_alignParentRight="true" androID:background="@null" androID:gravity="center" androID:paddingleft="15dp" androID:paddingRight="15dp" androID:singleline="true" androID:text="编辑" androID:textcolor="#2f302b" androID:textSize="14sp" androID:visibility="gone" /> <VIEw androID:layout_wIDth="match_parent" androID:layout_height="0.5dp" androID:layout_alignParentBottom="true" androID:background="#cccccc" /> </relativeLayout> <ExpandableListVIEw androID:ID="@+ID/elv_shopPing_car" androID:layout_wIDth="match_parent" androID:layout_height="0dp" androID:layout_weight="1" androID:background="#ededed" androID:divIDer="@null" androID:groupIndicator="@null" androID:scrollbars="none" androID:visibility="gone" /> <relativeLayout androID:ID="@+ID/rl" androID:layout_wIDth="match_parent" androID:layout_height="54dp" androID:background="#ffffff" androID:visibility="gone"> <linearLayout androID:ID="@+ID/ll_select_all" androID:layout_wIDth="wrap_content" androID:layout_height="match_parent" androID:orIEntation="horizontal" androID:paddingRight="10dp"> <ImageVIEw androID:ID="@+ID/iv_select_all" androID:layout_wIDth="20dp" androID:layout_height="20dp" androID:layout_gravity="center_vertical" androID:layout_marginleft="10dp" androID:background="@drawable/gouwuche_unselect_bg" /> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center" androID:layout_marginleft="6dp" androID:text="全选" androID:textcolor="#666666" androID:textSize="12dp" /> </linearLayout> <button androID:ID="@+ID/btn_order" androID:layout_wIDth="125dp" androID:layout_height="40dp" androID:layout_alignParentRight="true" androID:layout_centerVertical="true" androID:layout_marginRight="15dp" androID:background="@drawable/jiarugouwuche_bg" androID:text="结算" androID:textcolor="#ffffff" androID:textSize="16dp" androID:visibility="visible" /> <button androID:ID="@+ID/btn_delete" androID:layout_wIDth="125dp" androID:layout_height="40dp" androID:layout_alignParentRight="true" androID:layout_centerVertical="true" androID:layout_marginRight="15dp" androID:background="@drawable/jiarugouwuche_bg" androID:text="删除" androID:textcolor="#ffffff" androID:textSize="16dp" androID:visibility="gone" /> <relativeLayout androID:ID="@+ID/rl_total_price" androID:layout_wIDth="wrap_content" androID:layout_height="match_parent" androID:layout_toleftOf="@ID/btn_order" androID:layout_toRightOf="@ID/ll_select_all"> <TextVIEw androID:ID="@+ID/tv_total_price" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentRight="true" androID:layout_centerVertical="true" androID:layout_marginleft="2dp" androID:layout_marginRight="10dp" androID:maxLength="12" androID:singleline="true" androID:text="¥0.00" androID:textcolor="#d8b691" androID:textSize="15dp" /> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_centerVertical="true" androID:layout_toleftOf="@ID/tv_total_price" androID:text="合计金额:" androID:textcolor="#555555" androID:textSize="13dp" /> </relativeLayout> <VIEw androID:layout_wIDth="match_parent" androID:layout_height="0.5dp" androID:background="#cccccc" /> </relativeLayout> </linearLayout> <relativeLayout androID:ID="@+ID/rl_no_contant" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_centerInParent="true" androID:visibility="gone"> <ImageVIEw androID:ID="@+ID/iv_no_contant" androID:layout_wIDth="100dp" androID:layout_height="100dp" androID:layout_centerHorizontal="true" androID:scaleType="centerCrop" androID:src="@drawable/kong_gouwuche" /> <TextVIEw androID:layout_wIDth="200dp" androID:layout_height="wrap_content" androID:layout_below="@+ID/iv_no_contant" androID:layout_margintop="20dp" androID:gravity="center" androID:text="购物车竟然是空的" androID:textcolor="#808080" androID:textSize="16dp" /> </relativeLayout></relativeLayout>
item_shopPing_car_child.xml<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:background="#ffffff" androID:orIEntation="vertical"> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="100dp" androID:background="#ffffff"> <ImageVIEw androID:ID="@+ID/iv_select" androID:layout_wIDth="wrap_content" androID:layout_height="match_parent" androID:layout_gravity="center_vertical" androID:padding="10dp" androID:src="@drawable/unselect" /> <relativeLayout androID:layout_wIDth="match_parent" androID:layout_height="match_parent"> <ImageVIEw androID:ID="@+ID/iv_photo" androID:layout_wIDth="90dp" androID:layout_height="90dp" androID:layout_centerVertical="true" androID:background="#ededed" androID:scaleType="centerCrop" /> <TextVIEw androID:ID="@+ID/tv_name" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_marginleft="10dp" androID:layout_marginRight="10dp" androID:layout_margintop="10dp" androID:layout_toRightOf="@ID/iv_photo" androID:ellipsize="end" androID:maxlines="2" androID:text="时空房间啊链接法兰克福骄傲拉开飞机阿里进来撒劫匪了卡减肥了看见拉杀劫匪垃圾费垃圾费啦" androID:textcolor="#333333" androID:textSize="14dp" /> <relativeLayout androID:layout_wIDth="match_parent" androID:layout_height="35dp" androID:layout_alignParentBottom="true" androID:layout_marginleft="10dp" androID:layout_toRightOf="@ID/iv_photo"> <TextVIEw androID:ID="@+ID/tv_price_key" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_centerVertical="true" androID:text="¥" androID:textcolor="#ee1d23" androID:textSize="12dp" /> <TextVIEw androID:ID="@+ID/tv_price_value" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_centerVertical="true" androID:layout_marginleft="2dp" androID:layout_toRightOf="@ID/tv_price_key" androID:text="499" androID:textcolor="#ee1d23" androID:textSize="14dp" /> <linearLayout androID:layout_wIDth="wrap_content" androID:layout_height="match_parent" androID:layout_alignParentRight="true" androID:layout_centerVertical="true" androID:gravity="center" androID:orIEntation="horizontal"> <ImageVIEw androID:ID="@+ID/iv_edit_subtract" androID:layout_wIDth="wrap_content" androID:layout_height="match_parent" androID:paddingleft="15dp" androID:paddingRight="15dp" androID:src="@drawable/iv_edit_subtract" /> <TextVIEw androID:ID="@+ID/tv_edit_buy_number" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:gravity="center" androID:minems="1" androID:text="1" androID:textcolor="#666666" androID:textSize="12dp" /> <ImageVIEw androID:ID="@+ID/iv_edit_add" androID:layout_wIDth="wrap_content" androID:layout_height="match_parent" androID:paddingleft="15dp" androID:paddingRight="15dp" androID:src="@drawable/iv_edit_add" /> </linearLayout> <VIEw androID:ID="@+ID/vIEw" androID:layout_wIDth="match_parent" androID:layout_height="0.5dp" androID:layout_alignParentBottom="true" androID:background="#cccccc" /> </relativeLayout> </relativeLayout> </linearLayout> <VIEw androID:ID="@+ID/vIEw_last" androID:layout_wIDth="match_parent" androID:layout_height="10dp" androID:layout_alignParentBottom="true" androID:background="#ededed" androID:visibility="gone" /></linearLayout>
dialog_two_btn.xml<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:background="@drawable/shape_circle_corner_white" androID:gravity="center" androID:orIEntation="vertical"> <relativeLayout androID:layout_wIDth="match_parent" androID:layout_height="145dp" androID:layout_marginleft="40dp" androID:layout_marginRight="40dp" androID:background="@drawable/shape_circle_corner_white"> <TextVIEw androID:ID="@+ID/tv_message" androID:layout_wIDth="match_parent" androID:layout_height="70dp" androID:gravity="center" androID:maxlines="2" androID:padding="10dp" androID:textcolor="#AEAEAE" androID:textSize="16dp" /> <linearLayout androID:ID="@+ID/ll" androID:layout_wIDth="match_parent" androID:layout_height="50dp" androID:layout_below="@+ID/tv_message" androID:layout_alignParentBottom="true" androID:orIEntation="horizontal"> <TextVIEw androID:ID="@+ID/tv_logout_cancel" androID:layout_wIDth="0dp" androID:layout_height="match_parent" androID:layout_weight="1" androID:gravity="center" androID:text="取消" androID:textcolor="#929292" androID:textSize="17dp" /> <TextVIEw androID:ID="@+ID/tv_logout_confirm" androID:layout_wIDth="0dp" androID:layout_height="match_parent" androID:layout_weight="1" androID:gravity="center" androID:text="确定" androID:textcolor="#D9B07F" androID:textSize="17dp" /> </linearLayout> <VIEw androID:layout_wIDth="match_parent" androID:layout_height="0.5dp" androID:layout_aligntop="@ID/ll" androID:background="#e4e4e4" /> <VIEw androID:layout_wIDth="0.5dp" androID:layout_height="50dp" androID:layout_alignParentBottom="true" androID:layout_centerHorizontal="true" androID:background="#e4e4e4" /> </relativeLayout></linearLayout>
item_shopPing_car_group.xml<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:background="#ffffff" androID:orIEntation="vertical"> <linearLayout androID:ID="@+ID/ll" androID:layout_wIDth="match_parent" androID:layout_height="40dp" androID:orIEntation="horizontal"> <ImageVIEw androID:ID="@+ID/iv_select" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center_vertical" androID:layout_marginleft="10dp" androID:background="@drawable/unselect" /> <ImageVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center_vertical" androID:layout_marginleft="10dp" androID:background="@drawable/biaoqianlan_kefu" /> <TextVIEw androID:ID="@+ID/tv_store_name" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center" androID:layout_marginleft="10dp" androID:text="店铺名称" androID:textcolor="#333333" androID:textSize="14dp" androID:maxlines="1" androID:ellipsize="end"/> </linearLayout> <VIEw androID:layout_wIDth="match_parent" androID:layout_height="0.5dp" androID:layout_marginleft="10dp" androID:background="#cccccc" /></linearLayout>
adapter适配器:package com.showly.yglin.shopPingcar.adapter;import androID.content.Context;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.Baseexpandablelistadapter;import androID.Widget.button;import androID.Widget.ImageVIEw;import androID.Widget.linearLayout;import androID.Widget.relativeLayout;import androID.Widget.TextVIEw;import com.bumptech.glIDe.GlIDe;import com.showly.yglin.shopPingcar.R;import com.showly.yglin.shopPingcar.bean.ShopPingCarDataBean;import com.showly.yglin.shopPingcar.util.ToastUtil;import java.text.DecimalFormat;import java.util.ArrayList;import java.util.List;import butterknife.ButterKnife;import butterknife.InjectVIEw;/** * 购物车的adapter * 因为使用的是ExpandableListVIEw,所以继承Baseexpandablelistadapter */public class ShopPingCaradapter extends Baseexpandablelistadapter { private final Context context; private final linearLayout llSelectAll; private final ImageVIEw ivSelectAll; private final button btnorder; private final button btnDelete; private final relativeLayout rlTotalPrice; private final TextVIEw tvTotalPrice; private List<ShopPingCarDataBean.DatasBean> data; private boolean isSelectAll = false; private double total_price; public ShopPingCaradapter(Context context, linearLayout llSelectAll, ImageVIEw ivSelectAll, button btnorder, button btnDelete, relativeLayout rlTotalPrice, TextVIEw tvTotalPrice) { this.context = context; this.llSelectAll = llSelectAll; this.ivSelectAll = ivSelectAll; this.btnorder = btnorder; this.btnDelete = btnDelete; this.rlTotalPrice = rlTotalPrice; this.tvTotalPrice = tvTotalPrice; } /** * 自定义设置数据方法; * 通过notifyDataSetChanged()刷新数据,可保持当前位置 * * @param data 需要刷新的数据 */ public voID setData(List<ShopPingCarDataBean.DatasBean> data) { this.data = data; notifyDataSetChanged(); } @OverrIDe public int getGroupCount() { if (data != null && data.size() > 0) { return data.size(); } else { return 0; } } @OverrIDe public Object getGroup(int groupposition) { return data.get(groupposition); } @OverrIDe public long getGroupID(int groupposition) { return groupposition; } @OverrIDe public VIEw getGroupVIEw(final int groupposition, final boolean isExpanded, VIEw convertVIEw, VIEwGroup parent) { GroupVIEwHolder groupVIEwHolder; if (convertVIEw == null) { convertVIEw = VIEw.inflate(context, R.layout.item_shopPing_car_group, null); groupVIEwHolder = new GroupVIEwHolder(convertVIEw); convertVIEw.setTag(groupVIEwHolder); } else { groupVIEwHolder = (GroupVIEwHolder) convertVIEw.getTag(); } final ShopPingCarDataBean.DatasBean datasBean = data.get(groupposition); //店铺ID String store_ID = datasBean.getStore_ID(); //店铺名称 String store_name = datasBean.getStore_name(); if (store_name != null) { groupVIEwHolder.tvStorename.setText(store_name); } else { groupVIEwHolder.tvStorename.setText(""); } //店铺内的商品都选中的时候,店铺的也要选中 for (int i = 0; i < datasBean.getGoods().size(); i++) { ShopPingCarDataBean.DatasBean.GoodsBean goodsBean = datasBean.getGoods().get(i); boolean isSelect = goodsBean.getIsSelect(); if (isSelect) { datasBean.setIsSelect_shop(true); } else { datasBean.setIsSelect_shop(false); break; } } //因为set之后要重新get,所以这一块代码要放到一起执行 //店铺是否在购物车中被选中 final boolean isSelect_shop = datasBean.getIsSelect_shop(); if (isSelect_shop) { groupVIEwHolder.ivSelect.setimageResource(R.drawable.xuanze_xuanzhong); } else { groupVIEwHolder.ivSelect.setimageResource(R.drawable.unselect); } //店铺选择框的点击事件 groupVIEwHolder.ll.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { datasBean.setIsSelect_shop(!isSelect_shop); List<ShopPingCarDataBean.DatasBean.GoodsBean> goods = datasBean.getGoods(); for (int i = 0; i < goods.size(); i++) { ShopPingCarDataBean.DatasBean.GoodsBean goodsBean = goods.get(i); goodsBean.setIsSelect(!isSelect_shop); } notifyDataSetChanged(); } }); //当所有的选择框都是选中的时候,全选也要选中 w: for (int i = 0; i < data.size(); i++) { List<ShopPingCarDataBean.DatasBean.GoodsBean> goods = data.get(i).getGoods(); for (int y = 0; y < goods.size(); y++) { ShopPingCarDataBean.DatasBean.GoodsBean goodsBean = goods.get(y); boolean isSelect = goodsBean.getIsSelect(); if (isSelect) { isSelectAll = true; } else { isSelectAll = false; break w;//根据标记,跳出嵌套循环 } } } if (isSelectAll) { ivSelectAll.setBackgroundResource(R.drawable.xuanze_xuanzhong); } else { ivSelectAll.setBackgroundResource(R.drawable.unselect); } //全选的点击事件 llSelectAll.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { isSelectAll = !isSelectAll; if (isSelectAll) { for (int i = 0; i < data.size(); i++) { List<ShopPingCarDataBean.DatasBean.GoodsBean> goods = data.get(i).getGoods(); for (int y = 0; y < goods.size(); y++) { ShopPingCarDataBean.DatasBean.GoodsBean goodsBean = goods.get(y); goodsBean.setIsSelect(true); } } } else { for (int i = 0; i < data.size(); i++) { List<ShopPingCarDataBean.DatasBean.GoodsBean> goods = data.get(i).getGoods(); for (int y = 0; y < goods.size(); y++) { ShopPingCarDataBean.DatasBean.GoodsBean goodsBean = goods.get(y); goodsBean.setIsSelect(false); } } } notifyDataSetChanged(); } }); //合计的计算 total_price = 0.0; tvTotalPrice.setText("¥0.00"); for (int i = 0; i < data.size(); i++) { List<ShopPingCarDataBean.DatasBean.GoodsBean> goods = data.get(i).getGoods(); for (int y = 0; y < goods.size(); y++) { ShopPingCarDataBean.DatasBean.GoodsBean goodsBean = goods.get(y); boolean isSelect = goodsBean.getIsSelect(); if (isSelect) { String num = goodsBean.getGoods_num(); String price = goodsBean.getGoods_price(); double v = Double.parseDouble(num); double v1 = Double.parseDouble(price); total_price = total_price + v * v1; //让Double类型完整显示,不用科学计数法显示大写字母E DecimalFormat decimalFormat = new DecimalFormat("0.00"); tvTotalPrice.setText("¥" + decimalFormat.format(total_price)); } } } //去结算的点击事件 btnorder.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { //创建临时的List,用于存储被选中的商品 List<ShopPingCarDataBean.DatasBean.GoodsBean> temp = new ArrayList<>(); for (int i = 0; i < data.size(); i++) { List<ShopPingCarDataBean.DatasBean.GoodsBean> goods = data.get(i).getGoods(); for (int y = 0; y < goods.size(); y++) { ShopPingCarDataBean.DatasBean.GoodsBean goodsBean = goods.get(y); boolean isSelect = goodsBean.getIsSelect(); if (isSelect) { temp.add(goodsBean); } } } if (temp != null && temp.size() > 0) {//如果有被选中的 /** * 实际开发中,如果有被选中的商品, * 则跳转到确认订单页面,完成后续订单流程。 */ ToastUtil.makeText(context, "跳转到确认订单页面,完成后续订单流程"); } else { ToastUtil.makeText(context, "请选择要购买的商品"); } } }); //删除的点击事件 btnDelete.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { /** * 实际开发中,通过回调请求后台接口实现删除 *** 作 */ if (mDeleteListener != null) { mDeleteListener.onDelete(); } } }); return convertVIEw; } static class GroupVIEwHolder { @InjectVIEw(R.ID.iv_select) ImageVIEw ivSelect; @InjectVIEw(R.ID.tv_store_name) TextVIEw tvStorename; @InjectVIEw(R.ID.ll) linearLayout ll; GroupVIEwHolder(VIEw vIEw) { ButterKnife.inject(this, vIEw); } } //------------------------------------------------------------------------------------------------ @OverrIDe public int getChildrenCount(int groupposition) { if (data.get(groupposition).getGoods() != null && data.get(groupposition).getGoods().size() > 0) { return data.get(groupposition).getGoods().size(); } else { return 0; } } @OverrIDe public Object getChild(int groupposition, int childposition) { return data.get(groupposition).getGoods().get(childposition); } @OverrIDe public long getChildID(int groupposition, int childposition) { return childposition; } @OverrIDe public VIEw getChildVIEw(final int groupposition, final int childposition, boolean isLastChild, VIEw convertVIEw, VIEwGroup parent) { ChildVIEwHolder childVIEwHolder; if (convertVIEw == null) { convertVIEw = VIEw.inflate(context, R.layout.item_shopPing_car_child, null); childVIEwHolder = new ChildVIEwHolder(convertVIEw); convertVIEw.setTag(childVIEwHolder); } else { childVIEwHolder = (ChildVIEwHolder) convertVIEw.getTag(); } final ShopPingCarDataBean.DatasBean datasBean = data.get(groupposition); //店铺ID String store_ID = datasBean.getStore_ID(); //店铺名称 String store_name = datasBean.getStore_name(); //店铺是否在购物车中被选中 final boolean isSelect_shop = datasBean.getIsSelect_shop(); final ShopPingCarDataBean.DatasBean.GoodsBean goodsBean = datasBean.getGoods().get(childposition); //商品图片 String goods_image = goodsBean.getGoods_image(); //商品ID final String goods_ID = goodsBean.getGoods_ID(); //商品名称 String goods_name = goodsBean.getGoods_name(); //商品价格 String goods_price = goodsBean.getGoods_price(); //商品数量 String goods_num = goodsBean.getGoods_num(); //商品是否被选中 final boolean isSelect = goodsBean.getIsSelect(); GlIDe.with(context) .load(R.drawable.img) .into(childVIEwHolder.ivPhoto); if (goods_name != null) { childVIEwHolder.tvname.setText(goods_name); } else { childVIEwHolder.tvname.setText(""); } if (goods_price != null) { childVIEwHolder.tvPriceValue.setText(goods_price); } else { childVIEwHolder.tvPriceValue.setText(""); } if (goods_num != null) { childVIEwHolder.tvEditBuyNumber.setText(goods_num); } else { childVIEwHolder.tvEditBuyNumber.setText(""); } //商品是否被选中 if (isSelect) { childVIEwHolder.ivSelect.setimageResource(R.drawable.xuanze_xuanzhong); } else { childVIEwHolder.ivSelect.setimageResource(R.drawable.unselect); } //商品选择框的点击事件 childVIEwHolder.ivSelect.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { goodsBean.setIsSelect(!isSelect); if (!isSelect == false) { datasBean.setIsSelect_shop(false); } notifyDataSetChanged(); } }); //加号的点击事件 childVIEwHolder.ivEditAdd.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { //模拟加号 *** 作 String num = goodsBean.getGoods_num(); Integer integer = Integer.valueOf(num); integer++; goodsBean.setGoods_num(integer + ""); notifyDataSetChanged(); /** * 实际开发中,通过回调请求后台接口实现数量的加减 */ if (mChangeCountListener != null) { mChangeCountListener.onChangeCount(goods_ID); } } }); //减号的点击事件 childVIEwHolder.ivEditSubtract.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { //模拟减号 *** 作 String num = goodsBean.getGoods_num(); Integer integer = Integer.valueOf(num); if (integer > 1) { integer--; goodsBean.setGoods_num(integer + ""); /** * 实际开发中,通过回调请求后台接口实现数量的加减 */ if (mChangeCountListener != null) { mChangeCountListener.onChangeCount(goods_ID); } } else { ToastUtil.makeText(context, "商品不能再减少了"); } notifyDataSetChanged(); } }); if (childposition == data.get(groupposition).getGoods().size() - 1) { childVIEwHolder.vIEw.setVisibility(VIEw.GONE); childVIEwHolder.vIEwLast.setVisibility(VIEw.VISIBLE); } else { childVIEwHolder.vIEw.setVisibility(VIEw.VISIBLE); childVIEwHolder.vIEwLast.setVisibility(VIEw.GONE); } return convertVIEw; } static class ChildVIEwHolder { @InjectVIEw(R.ID.iv_select) ImageVIEw ivSelect; @InjectVIEw(R.ID.iv_photo) ImageVIEw ivPhoto; @InjectVIEw(R.ID.tv_name) TextVIEw tvname; @InjectVIEw(R.ID.tv_price_key) TextVIEw tvPriceKey; @InjectVIEw(R.ID.tv_price_value) TextVIEw tvPriceValue; @InjectVIEw(R.ID.iv_edit_subtract) ImageVIEw ivEditSubtract; @InjectVIEw(R.ID.tv_edit_buy_number) TextVIEw tvEditBuyNumber; @InjectVIEw(R.ID.iv_edit_add) ImageVIEw ivEditAdd; @InjectVIEw(R.ID.vIEw) VIEw vIEw; @InjectVIEw(R.ID.vIEw_last) VIEw vIEwLast; ChildVIEwHolder(VIEw vIEw) { ButterKnife.inject(this, vIEw); } } //----------------------------------------------------------------------------------------------- @OverrIDe public boolean isChildSelectable(int groupposition, int childposition) { return false; } @OverrIDe public boolean hasStableIDs() { return false; } //删除的回调 public interface OnDeleteListener { voID onDelete(); } public voID setonDeleteListener(OnDeleteListener Listener) { mDeleteListener = Listener; } private OnDeleteListener mDeleteListener; //修改商品数量的回调 public interface OnChangeCountListener { voID onChangeCount(String goods_ID); } public voID setonChangeCountListener(OnChangeCountListener Listener) { mChangeCountListener = Listener; } private OnChangeCountListener mChangeCountListener;}
Bean类:package com.showly.yglin.shopPingcar.bean;import java.util.List;/** * 购物车数据的bean类 */public class ShopPingCarDataBean { private int code; private List<DatasBean> datas; public int getCode() { return code; } public voID setCode(int code) { this.code = code; } public List<DatasBean> getDatas() { return datas; } public voID setDatas(List<DatasBean> datas) { this.datas = datas; } public static class DatasBean { private String store_ID; private String store_name; private boolean isSelect_shop; //店铺是否在购物车中被选中 private List<GoodsBean> goods; public boolean getIsSelect_shop() { return isSelect_shop; } public voID setIsSelect_shop(boolean select_shop) { isSelect_shop = select_shop; } public String getStore_ID() { return store_ID; } public voID setStore_ID(String store_ID) { this.store_ID = store_ID; } public String getStore_name() { return store_name; } public voID setStore_name(String store_name) { this.store_name = store_name; } public List<GoodsBean> getGoods() { return goods; } public voID setGoods(List<GoodsBean> goods) { this.goods = goods; } public static class GoodsBean { private String goods_ID; private String goods_image; private String goods_name; private String goods_num; private String goods_price; private boolean isSelect; //商品是否在购物车中被选中 public boolean getIsSelect() { return isSelect; } public voID setIsSelect(boolean isSelect) { this.isSelect = isSelect; } public String getGoods_ID() { return goods_ID; } public voID setGoods_ID(String goods_ID) { this.goods_ID = goods_ID; } public String getGoods_image() { return goods_image; } public voID setGoods_image(String goods_image) { this.goods_image = goods_image; } public String getGoods_name() { return goods_name; } public voID setGoods_name(String goods_name) { this.goods_name = goods_name; } public String getGoods_num() { return goods_num; } public voID setGoods_num(String goods_num) { this.goods_num = goods_num; } public String getGoods_price() { return goods_price; } public voID setGoods_price(String goods_price) { this.goods_price = goods_price; } } }}
MainActivitypackage com.showly.yglin.shopPingcar;import androID.content.Context;import androID.content.DialogInterface;import androID.os.Bundle;import androID.vIEw.KeyEvent;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.ExpandableListVIEw;import androID.Widget.ImageVIEw;import androID.Widget.linearLayout;import androID.Widget.relativeLayout;import androID.Widget.TextVIEw;import androIDx.appcompat.app.AppCompatActivity;import com.Google.gson.Gson;import com.showly.yglin.shopPingcar.adapter.ShopPingCaradapter;import com.showly.yglin.shopPingcar.bean.ShopPingCarDataBean;import com.showly.yglin.shopPingcar.customvIEw.RoundCornerDialog;import com.showly.yglin.shopPingcar.util.ToastUtil;import java.util.ArrayList;import java.util.List;import butterknife.ButterKnife;import butterknife.InjectVIEw;import butterknife.OnClick;/** * 购物车实现 * 主要功能: * 1.单选、全选; * 2.合计; * 3.删除; * 4.商品数量加减; */public class MainActivity extends AppCompatActivity { @InjectVIEw(R.ID.tv_Titlebar_center) TextVIEw tvTitlebarCenter; @InjectVIEw(R.ID.tv_Titlebar_right) TextVIEw tvTitlebarRight; @InjectVIEw(R.ID.elv_shopPing_car) ExpandableListVIEw elvShopPingCar; @InjectVIEw(R.ID.iv_select_all) ImageVIEw ivSelectAll; @InjectVIEw(R.ID.ll_select_all) linearLayout llSelectAll; @InjectVIEw(R.ID.btn_order) button btnorder; @InjectVIEw(R.ID.btn_delete) button btnDelete; @InjectVIEw(R.ID.tv_total_price) TextVIEw tvTotalPrice; @InjectVIEw(R.ID.rl_total_price) relativeLayout rlTotalPrice; @InjectVIEw(R.ID.rl) relativeLayout rl; @InjectVIEw(R.ID.iv_no_contant) ImageVIEw ivNoContant; @InjectVIEw(R.ID.rl_no_contant) relativeLayout rlNoContant; @InjectVIEw(R.ID.tv_Titlebar_left) ImageVIEw tvTitlebarleft; //模拟的购物车数据(实际开发中使用后台返回的数据) private String shopPingCarData = "{\n" + " \"code\": 200,\n" + " \"datas\": [\n" + " {\n" + " \"goods\": [\n" + " {\n" + " \"goods_ID\": \"111111\",\n" + " \"goods_image\": \"http://pic.58pic.com/58pic/15/62/69/34K58PICbmZ_1024.jpg\",\n" + " \"goods_name\": \"三国演义\",\n" + " \"goods_num\": \"2\",\n" + " \"goods_price\": \"15.00\"\n" + " }\n" + " ],\n" + " \"store_ID\": \"1\",\n" + " \"store_name\": \"书店杂货铺\"\n" + " },\n" + " {\n" + " \"goods\": [\n" + " {\n" + " \"goods_ID\": \"222221\",\n" + " \"goods_image\": \"http://file06.16sucai.com/2016/0511/9711205e4c003182edeed83355e6f1c7.jpg\",\n" + " \"goods_name\": \"西游记\",\n" + " \"goods_num\": \"2\",\n" + " \"goods_price\": \"12.00\"\n" + " },\n" + " {\n" + " \"goods_ID\": \"222222\",\n" + " \"goods_image\": \"http://img01.taopic.com/150424/240473-1504240U13615.jpg\",\n" + " \"goods_name\": \"封神榜\",\n" + " \"goods_num\": \"1\",\n" + " \"goods_price\": \"28.00\"\n" + " }\n" + " ],\n" + " \"store_ID\": \"2\",\n" + " \"store_name\": \"亚马逊书店\"\n" + " },\n" + " {\n" + " \"goods\": [\n" + " {\n" + " \"goods_ID\": \"333331\",\n" + " \"goods_image\": \"http://pic22.nipic.com/20120718/8002769_100147127333_2.jpg\",\n" + " \"goods_name\": \"水浒传\",\n" + " \"goods_num\": \"3\",\n" + " \"goods_price\": \"18.00\"\n" + " },\n" + " {\n" + " \"goods_ID\": \"333332\",\n" + " \"goods_image\": \"http://pic.58pic.com/58pic/14/71/50/40e58PICy54_1024.jpg\",\n" + " \"goods_name\": \"封神演义\",\n" + " \"goods_num\": \"3\",\n" + " \"goods_price\": \"32.00\"\n" + " },\n" + " {\n" + " \"goods_ID\": \"333333\",\n" + " \"goods_image\": \"http://img01.taopic.com/150518/318750-15051PS40671.jpg\",\n" + " \"goods_name\": \"轩辕剑\",\n" + " \"goods_num\": \"3\",\n" + " \"goods_price\": \"3.80\"\n" + " }\n" + " ],\n" + " \"store_ID\": \"3\",\n" + " \"store_name\": \"三味书屋\"\n" + " }\n" + " ]\n" + "}"; private List<ShopPingCarDataBean.DatasBean> datas; private Context context; private ShopPingCaradapter shopPingCaradapter; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); ButterKnife.inject(this); context = this; initExpandableListVIEw(); initData(); } @OnClick({R.ID.tv_Titlebar_left, R.ID.tv_Titlebar_right}) public voID onVIEwClicked(VIEw vIEw) { switch (vIEw.getID()) { case R.ID.tv_Titlebar_left://刷新数据 initData(); break; case R.ID.tv_Titlebar_right://编辑 String edit = tvTitlebarRight.getText().toString().trim(); if (edit.equals("编辑")) { tvTitlebarRight.setText("完成"); rlTotalPrice.setVisibility(VIEw.GONE); btnorder.setVisibility(VIEw.GONE); btnDelete.setVisibility(VIEw.VISIBLE); } else { tvTitlebarRight.setText("编辑"); rlTotalPrice.setVisibility(VIEw.VISIBLE); btnorder.setVisibility(VIEw.VISIBLE); btnDelete.setVisibility(VIEw.GONE); } break; default: break; } } /** * 初始化数据 */ private voID initData() { //使用Gson解析购物车数据, //ShopPingCarDataBean为bean类,Gson按照bean类的格式解析数据 /** * 实际开发中,通过请求后台接口获取购物车数据并解析 */ Gson gson = new Gson(); ShopPingCarDataBean shopPingCarDataBean = gson.fromJson(shopPingCarData, ShopPingCarDataBean.class); datas = shopPingCarDataBean.getDatas(); initExpandableListVIEwData(datas); } /** * 初始化ExpandableListVIEw * 创建数据适配器adapter,并进行初始化 *** 作 */ private voID initExpandableListVIEw() { shopPingCaradapter = new ShopPingCaradapter(context, llSelectAll, ivSelectAll, btnorder, btnDelete, rlTotalPrice, tvTotalPrice); elvShopPingCar.setAdapter(shopPingCaradapter); //删除的回调 shopPingCaradapter.setonDeleteListener(new ShopPingCaradapter.OnDeleteListener() { @OverrIDe public voID onDelete() { initDelete(); /** * 实际开发中,在此请求删除接口,删除成功后, * 通过initExpandableListVIEwData()方法刷新购物车数据。 * 注:通过bean类中的DatasBean的isSelect_shop属性,判断店铺是否被选中; * GoodsBean的isSelect属性,判断商品是否被选中, * (true为选中,false为未选中) */ } }); //修改商品数量的回调 shopPingCaradapter.setonChangeCountListener(new ShopPingCaradapter.OnChangeCountListener() { @OverrIDe public voID onChangeCount(String goods_ID) { /** * 实际开发中,在此请求修改商品数量的接口,商品数量修改成功后, * 通过initExpandableListVIEwData()方法刷新购物车数据。 */ } }); } /** * 初始化ExpandableListVIEw的数据 * 并在数据刷新时,页面保持当前位置 * * @param datas 购物车的数据 */ private voID initExpandableListVIEwData(List<ShopPingCarDataBean.DatasBean> datas) { if (datas != null && datas.size() > 0) { //刷新数据时,保持当前位置 shopPingCaradapter.setData(datas); //使所有组展开 for (int i = 0; i < shopPingCaradapter.getGroupCount(); i++) { elvShopPingCar.expandGroup(i); } //使组点击无效果 elvShopPingCar.setonGroupClickListener(new ExpandableListVIEw.OnGroupClickListener() { @OverrIDe public boolean onGroupClick(ExpandableListVIEw parent, VIEw v, int groupposition, long ID) { return true; } }); tvTitlebarRight.setVisibility(VIEw.VISIBLE); tvTitlebarRight.setText("编辑"); rlNoContant.setVisibility(VIEw.GONE); elvShopPingCar.setVisibility(VIEw.VISIBLE); rl.setVisibility(VIEw.VISIBLE); rlTotalPrice.setVisibility(VIEw.VISIBLE); btnorder.setVisibility(VIEw.VISIBLE); btnDelete.setVisibility(VIEw.GONE); } else { tvTitlebarRight.setVisibility(VIEw.GONE); rlNoContant.setVisibility(VIEw.VISIBLE); elvShopPingCar.setVisibility(VIEw.GONE); rl.setVisibility(VIEw.GONE); } } /** * 判断是否要d出删除的dialog * 通过bean类中的DatasBean的isSelect_shop属性,判断店铺是否被选中; * GoodsBean的isSelect属性,判断商品是否被选中, */ private voID initDelete() { //判断是否有店铺或商品被选中 //true为有,则需要刷新数据;反之,则不需要; boolean hasSelect = false; //创建临时的List,用于存储没有被选中的购物车数据 List<ShopPingCarDataBean.DatasBean> datastemp = new ArrayList<>(); for (int i = 0; i < datas.size(); i++) { List<ShopPingCarDataBean.DatasBean.GoodsBean> goods = datas.get(i).getGoods(); boolean isSelect_shop = datas.get(i).getIsSelect_shop(); if (isSelect_shop) { hasSelect = true; //跳出本次循环,继续下次循环。 continue; } else { datastemp.add(datas.get(i)); datastemp.get(datastemp.size() - 1).setGoods(new ArrayList<ShopPingCarDataBean.DatasBean.GoodsBean>()); } for (int y = 0; y < goods.size(); y++) { ShopPingCarDataBean.DatasBean.GoodsBean goodsBean = goods.get(y); boolean isSelect = goodsBean.getIsSelect(); if (isSelect) { hasSelect = true; } else { datastemp.get(datastemp.size() - 1).getGoods().add(goodsBean); } } } if (hasSelect) { showDeleteDialog(datastemp); } else { ToastUtil.makeText(context, "请选择要删除的商品"); } } /** * 展示删除的dialog(可以自定义d窗,不用删除即可) * * @param datastemp */ private voID showDeleteDialog(final List<ShopPingCarDataBean.DatasBean> datastemp) { VIEw vIEw = VIEw.inflate(context, R.layout.dialog_two_btn, null); final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(context, 0, 0, vIEw, R.style.RoundCornerDialog); roundCornerDialog.show(); roundCornerDialog.setCanceledOntouchOutsIDe(false);// 设置点击屏幕Dialog不消失 roundCornerDialog.setonKeyListener(keyListener);//设置点击返回键Dialog不消失 TextVIEw tv_message = vIEw.findVIEwByID(R.ID.tv_message); TextVIEw tv_logout_confirm = vIEw.findVIEwByID(R.ID.tv_logout_confirm); TextVIEw tv_logout_cancel = vIEw.findVIEwByID(R.ID.tv_logout_cancel); tv_message.setText("确定要删除商品吗?"); //确定 tv_logout_confirm.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { roundCornerDialog.dismiss(); datas = datastemp; initExpandableListVIEwData(datas); } }); //取消 tv_logout_cancel.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { roundCornerDialog.dismiss(); } }); } DialogInterface.OnKeyListener keyListener = new DialogInterface.OnKeyListener() { public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { return true; } else { return false; } } };}
LogUtilpackage com.showly.yglin.shopPingcar.util;import androID.util.Log;/** * 管理Log的工具类:可通过设置mCurrentLevel,控制Log输出级别。 * 项目上线时应将mCurrentLevel设置为LEVEL_NONE,禁止Log输出。 */public class LogUtil { //日志输出级别NONE public static final int LEVEL_NONE = 0; //日志输出级别E public static final int LEVEL_ERROR = 1; //日志输出级别W public static final int LEVEL_WARN = 2; //日志输出级别I public static final int LEVEL_INFO = 3; //日志输出级别D public static final int LEVEL_DEBUG = 4; //日志输出级别V public static final int LEVEL_VERBOSE = 5; //日志输出时的Tag private static String mTag = "LogUtil"; //当前日志输出级别(是否允许输出log) private static int mCurrentLevel = LEVEL_VERBOSE; /** * 以级别为 d 的形式输出LOG */ public static voID v(String tag, String msg) { if (mCurrentLevel >= LEVEL_VERBOSE) { Log.v(tag, msg); } } /** * 以级别为 d 的形式输出LOG */ public static voID d(String tag, String msg) { if (mCurrentLevel >= LEVEL_DEBUG) { Log.d(tag, msg); } } /** * 以级别为 i 的形式输出LOG */ public static voID i(String tag, String msg) { if (mCurrentLevel >= LEVEL_INFO) { Log.i(tag, msg); } } /** * 以级别为 w 的形式输出LOG */ public static voID w(String tag, String msg) { if (mCurrentLevel >= LEVEL_WARN) { Log.w(tag, msg); } } /** * 以级别为 w 的形式输出Throwable */ public static voID w(Throwable tr) { if (mCurrentLevel >= LEVEL_WARN) { Log.w(mTag, "", tr); } } /** * 以级别为 w 的形式输出LOG信息和Throwable */ public static voID w(String msg, Throwable tr) { if (mCurrentLevel >= LEVEL_WARN && null != msg) { Log.w(mTag, msg, tr); } } /** * 以级别为 e 的形式输出LOG */ public static voID e(String tag, String msg) { if (mCurrentLevel >= LEVEL_ERROR) { Log.e(tag, msg); } } /** * 以级别为 e 的形式输出Throwable */ public static voID e(Throwable tr) { if (mCurrentLevel >= LEVEL_ERROR) { Log.e(mTag, "", tr); } } /** * 以级别为 e 的形式输出LOG信息和Throwable */ public static voID e(String msg, Throwable tr) { if (mCurrentLevel >= LEVEL_ERROR && null != msg) { Log.e(mTag, msg, tr); } }}
ToastUtilpackage com.showly.yglin.shopPingcar.util;import androID.content.Context;import androID.os.Handler;import androID.Widget.Toast;/** * 下一个Toast覆盖上一个Toast,防止Toastd出的时间过长 */public class ToastUtil { private static Toast mToast; private static Handler mHandler = new Handler(); private static Runnable r = new Runnable() { public voID run() { mToast.cancel(); } }; public static voID makeText(Context mContext, String text) { mHandler.removeCallbacks(r); if (mToast != null) mToast.setText(text); else mToast = Toast.makeText(mContext, text, Toast.LENGTH_LONG); mHandler.postDelayed(r, 2000); mToast.show(); }}
项目上的图片等@H_301_123@
总结
以上是内存溢出为你收集整理的Android学习之-------实现一个简单购物车全部内容,希望文章能够帮你解决Android学习之-------实现一个简单购物车所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)