Android购物车项目快速开发
购物车项目,业务需要实现了一个购物车的项目,简单的了解下实现逻辑:数据计算等是在adapter中计算出来的,通过在adapter中计算出来的数据就可以回调到activity中进行订单操作等功能业务逻辑,每一个店铺产生的数据是走一条流程的,(业务需求:不是作为一个类似淘宝,京东的平台数据又由平台分发,所以我们实现的是一对一的客户交易的交易流程)接着往下看:
1.界面使用到的控件
goodsadapter = new goodscaradapter(shopcaravtivity.this, result); lv_refresh.setadapter(goodsadapter); goodsadapter.setcheckinterface(shopcaravtivity.this);// 关键步骤1,设置复选框接口 goodsadapter.setmodifycountinterface(shopcaravtivity.this);// 关键步骤2,设置数量增减接口 for (int i = 0; i < goodsadapter.getgroupcount(); i++) { lv_refresh.expandgroup(i);// 关键步骤3,初始化时,将expandablelistview以展开的方式呈现 }
2.项目中使用到的数据接口
界面当中的复选框的接口回调
public interface checkgoodslistener { /** * 组选框状态改变触发的事件 * * @param groupposition 组元素位置 * @param ischecked 组元素选中与否 */ void checkgroup(int groupposition, boolean ischecked); /** * 子选框状态改变时触发的事件 * * @param groupposition 组元素位置 * @param childposition 子元素位置 * @param ischecked 子元素选中与否 */ void checkchild(int groupposition, int childposition, boolean ischecked); /** * 购买 * @param groupposition * @param childposition * @param ischecked */ void checkgoodsbuy(shopcarmodel shopcarmodel, double totalmonery, int totalcount); }
商品增加和修改的接口
/** * 改变数量的接口 * created by zhuangah on 2016-11-7. */ public interface modifycountlistener { /** * 增加操作 * * @param groupposition 组元素位置 * @param childposition 子元素位置 * @param showcountview 用于展示变化后数量的view * @param ischecked 子元素选中与否 */ void doincrease(int groupposition, int childposition, view showcountview, boolean ischecked); /** * 删减操作 * * @param groupposition 组元素位置 * @param childposition 子元素位置 * @param showcountview 用于展示变化后数量的view * @param ischecked 子元素选中与否 */ void dodecrease(int groupposition, int childposition, view showcountview, boolean ischecked); /** * 删除子item * * @param groupposition * @param childposition */ void childdelete(int groupposition, int childposition); }
3.在adapter中计算商品的金额数量
单个店铺中有多个商品,所以这个店铺的布局,包含选择全组的按钮,使用了接口的回调 checkinterface.checkgroup(groupposition, ((checkbox) v).ischecked());来判断是否选中全组,在activity中进行数据的便利是否选中商品之后再刷新数据。
@override public view getgroupview(final int groupposition, boolean isexpanded, view convertview, viewgroup parent) { groupviewholder gholder = null; if (convertview != null && !(convertview.gettag() instanceof groupviewholder)) { convertview = null; } if (convertview == null) { gholder = new groupviewholder(); convertview = view.inflate(context, r.layout.item_shopcart_group, null); gholder.determinechekbox = (checkbox) convertview.findviewbyid(r.id.determine_chekbox); gholder.tvsourcename = (textview) convertview.findviewbyid(r.id.tv_source_name); convertview.settag(gholder); } else { gholder = (groupviewholder) convertview.gettag(); } final shopcarmodel group = (shopcarmodel) getgroup(groupposition); gholder.tvsourcename.settext(group.getfactoryname()); gholder.determinechekbox.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { group.setchoosed(((checkbox) v).ischecked()); checkinterface.checkgroup(groupposition, ((checkbox) v).ischecked()); } }); gholder.determinechekbox.setchecked(group.ischoosed()); return convertview; }
在activity中计算便利数据
/*** * 校对组元素 * * @param groupposition 组元素位置 * @param ischecked 组元素选中与否 */ @override public void checkgroup(int groupposition, boolean ischecked) { list<goodsmodel> goodsmodellist = shopcarmodellist.get(groupposition).getgoodsmodel(); for (int i = 0; i < goodsmodellist.size(); i++) { goodsmodellist.get(i).setchoosed(ischecked); } goodsadapter.notifydatasetchanged(); }
在商品中计算数据结果:
@override public view getchildview(final int groupposition, final int childposition, final boolean islastchild, view convertview, final viewgroup parent) { goodsviewholder goodsviewholder = null; int totalcount = 0; double totalprice = 0.00; if (convertview != null && !(convertview.gettag() instanceof goodsviewholder)) { convertview = null; } if (convertview == null) { goodsviewholder = new goodsviewholder(); convertview = view.inflate(context, r.layout.item_shopcart_product, null); goodsviewholder.checkbox = (checkbox) convertview.findviewbyid(r.id.check_box); goodsviewholder.ivadapterlistpic = (imageview) convertview.findviewbyid(r.id.iv_adapter_list_pic); goodsviewholder.tvintro = (textview) convertview.findviewbyid(r.id.tv_intro); goodsviewholder.tvprice = (textview) convertview.findviewbyid(r.id.tv_price); goodsviewholder.tvbuynum = (textview) convertview.findviewbyid(r.id.tv_buy_num); goodsviewholder.rlnoedtor = (relativelayout) convertview.findviewbyid(r.id.rl_no_edtor); goodsviewholder.tvreduce = (textview) convertview.findviewbyid(r.id.tv_reduce); goodsviewholder.tvnum = (textview) convertview.findviewbyid(r.id.tv_num); goodsviewholder.tvadd = (textview) convertview.findviewbyid(r.id.tv_add); goodsviewholder.llchangenum = (linearlayout) convertview.findviewbyid(r.id.ll_change_num); goodsviewholder.layout_item_foot = (linearlayout) convertview.findviewbyid(r.id.layout_item_foot); goodsviewholder.tv_goods_number = (textview) convertview.findviewbyid(r.id.tv_goods_number); goodsviewholder.tv_goods_menoy = (textview) convertview.findviewbyid(r.id.tv_goods_menoy); goodsviewholder.tv_buys = (textview) convertview.findviewbyid(r.id.tv_buys); goodsviewholder.laytou_car = (linearlayout) convertview.findviewbyid(r.id.laytou_car); convertview.settag(goodsviewholder); } else { goodsviewholder = (goodsviewholder) convertview.gettag(); } //进行数据操作 final goodsmodel goodsinfo = (goodsmodel) getchild(groupposition, childposition); if (goodsinfo != null) { //数量初始化为0,金额初始化为0 goodsviewholder.tv_goods_number.settext(typeutils.tostring(0)); goodsviewholder.tv_goods_menoy.settext("¥ " + numberutils.formatmoneyscale(0.00)); list<goodsmodel> goodsmodel = carutrils.getgoodslist(goodshop, groupposition); //判断是否最后一个 if ((goodsmodel.size() - 1) == childposition) { goodsviewholder.layout_item_foot.setvisibility(view.visible); /** * 统计操作<br> * 1.先清空全局计数器<br> * 2.遍历所有子元素,只要是被选中状态的,就进行相关的计算操作<br> * 3.给底部的textview进行数据填充 */ //1判断商品是否选中,再进行计算 for (int j = 0; j < goodsmodel.size(); j++) { goodsmodel model = goodsmodel.get(j); if (model.ischoosed()) { totalcount += model.getselectqty(); totalprice += typeutils.todouble(numberutils.multiply(typeutils.tobigdecimal(numberutils.formatrounded(model.getprice())), numberutils.tobigdecimal(model.getselectqty()))); } } if (totalprice != 0.00) { goodsviewholder.tv_goods_number.settext(typeutils.tostring(totalcount)); goodsviewholder.tv_goods_menoy.settext(numberutils.formatrounded(typeutils.tobigdecimal(totalprice))); goodsviewholder.tv_buys.setbackgroundcolor(context.getresources().getcolor(r.color.main_color)); goodsviewholder.tv_buys.setenabled(true); } else { goodsviewholder.tv_goods_number.settext(typeutils.tostring(0)); goodsviewholder.tv_goods_menoy.settext("¥ " + numberutils.formatmoneyscale(0.00)); goodsviewholder.tv_buys.setbackgroundcolor(context.getresources().getcolor(r.color.resport_line)); goodsviewholder.tv_buys.setenabled(false); } } else { goodsviewholder.layout_item_foot.setvisibility(view.gone); } //设置基础数据 if (goodsinfo.getimagesrc() != null) { glide.with(context) .load(goodsinfo.getimagesrc()) .centercrop() .placeholder(r.mipmap.test2) .crossfade() .into(goodsviewholder.ivadapterlistpic); } goodsviewholder.tvintro.settext(goodsinfo.getname()); goodsviewholder.tvprice.settext("¥ " + numberutils.formatrounded(goodsinfo.getprice())); goodsviewholder.tvbuynum.settext("x " + numberutils.formatqty(goodsinfo.getqty())); //set goods check goodsviewholder.checkbox.setchecked(goodsinfo.ischoosed()); goodsviewholder.tvnum.settext(typeutils.tostring(goodsinfo.getselectqty())); //选中的状态下才能触发点击事件 goodsviewholder.tvadd.setenabled(true); goodsviewholder.tvreduce.setenabled(true); //加减 final goodsviewholder finalgoodsviewholder = goodsviewholder; goodsviewholder.tvadd.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { modifycountinterface.doincrease(groupposition, childposition, finalgoodsviewholder.tvnum, finalgoodsviewholder.checkbox.ischecked());// 暴露增加接口 } }); final goodsviewholder finalgoodsviewholder1 = goodsviewholder; goodsviewholder.tvreduce.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { modifycountinterface.dodecrease(groupposition, childposition, finalgoodsviewholder1.tvnum, finalgoodsviewholder1.checkbox.ischecked());// 暴露删减接口 } }); //goods check state onclick goodsviewholder.checkbox.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { goodsinfo.setchoosed(((checkbox) v).ischecked()); ((checkbox) v).setchecked(((checkbox) v).ischecked()); checkinterface.checkchild(groupposition, childposition, ((checkbox) v).ischecked()); } }); //onclick to orderdetailactivity final double finaltotalprice = totalprice; final int finaltotalcount = totalcount; goodsviewholder.tv_buys.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //check goods is not null //if not null can go other activity shopcarmodel shopcar = carutrils.getcheckshopcar(goodshop, groupposition); if (shopcar != null && shopcar.getgoodsmodel().size() > 0) { checkinterface.checkgoodsbuy(shopcar, finaltotalprice, finaltotalcount); } } }); } goodsviewholder.laytou_car.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { uihelper.toshopcargoodsdetailactivity(context, goodsinfo); } }); return convertview; }
以上代码的是先判断当前组是否为一个店铺中的最后一个,在最后一个元素中局部计算当前的group的数据,根据数据来选择是否复位数据展示以及显示,把计算的数据展示出来,最后通过接口回调的方式跳转界面,把数据传到activity中去。就这样子就处理完这个购物车的逻辑,以上可能不符合你的逻辑,但是你可以稍微修改就拿来使用。
github地址: https://github.com/anhuifix/singleshopcar/tree/master
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: mybatis教程之延迟加载详解
下一篇: Android自定义星星评分控件