Android仿淘宝订单页面效果
程序员文章站
2022-11-08 15:03:22
一般电商项目会涉及到的订单管理模块,类似淘宝样式的订单
主要是讲一下订单页面的实现。当然实现的方法有很多,我知道的有两种方法:一种是采用listview嵌套lis...
一般电商项目会涉及到的订单管理模块,类似淘宝样式的订单
主要是讲一下订单页面的实现。当然实现的方法有很多,我知道的有两种方法:一种是采用listview嵌套listview的方式,这种方式需要重写listview中onmearsure方法;还有一种是采用接口回调的方式,这种方式主要对后台返回的数据有依赖;
今天主要说下第二种方法:
实现的思想:首先tab下面的布局还是用一个listview实现,然后将listview中的item分为上中下三部分内容;
创建三个xml文件,分别实现三个布局;第一部分为店铺名称那一栏,第二部分为商品信息那一栏,第三部分为订单状态那一栏;
然后创建一个回调接口
代码如下
public interface ordercontent { public int getlayout(); public boolean isclickable(); public view getview(context context, view convertview, layoutinflater inflater); }
利用接口回调分别实现三个布局
第一部分
public class itemordertop implements ordercontent { private order order; private oncesendview2 shopimageview; private list<order_goods> list; private acache acache; public itemordertop(order order, list<order_goods> list) { this.order = order; this.list = list; } @override public int getlayout() { return r.layout.item_order_top; } @override public boolean isclickable() { return true; } public order order() { return order; } @override public view getview(final context context, view convertview, layoutinflater inflater) { acache = acache.get(context); inflater = layoutinflater.from(context); convertview = inflater.inflate(getlayout(), null); final gtextview orderid = (gtextview) convertview.findviewbyid(r.id.tv_order_num1); gtextview orderstatus = (gtextview) convertview.findviewbyid(r.id.tv_order_state1); orderid.settext("订单号码:" + order.getorder_sn()); switch (order.getstatus().tostring()) { case "20": orderstatus.settext("待发货"); break; case "30": orderstatus.settext("已发货"); break; case "40": orderstatus.settext("已收货待评价"); break; default: break; } return convertview; } }
中间部分
public class orderimiddle implements ordercontent { private order_goods order_goods; private imageloader imageloader; private displayimageoptions options; list<order_goods> order_goodslist; private string status; public orderimiddle(order_goods order_goods, string status) { this.order_goods = order_goods; order_goodslist = new arraylist<order_goods>(); order_goodslist.add(order_goods); this.status = status; imageloader = imageloader.getinstance(); // 使用displayimageoptions.builder()创建displayimageoptions options = new displayimageoptions.builder() .showimageonloading(r.drawable.publicloading) .showimageforemptyuri(r.drawable.publicloading) .showimageonfail(r.drawable.publicloading) .cacheinmemory(true) .build(); } @override public int getlayout() { return r.layout.listitem_goods; } @override public boolean isclickable() { return true; } public order_goods order_goods() { return order_goods; } @override public view getview(final context context, view convertview, layoutinflater inflater) { inflater = layoutinflater.from(context); convertview = inflater.inflate(getlayout(), null); gimageview mimage = (gimageview) convertview.findviewbyid(r.id.iv_goods_image1); gtextview goods_info = (gtextview) convertview.findviewbyid(r.id.tv_goods_info1); gtextview goods_attribute = (gtextview) convertview.findviewbyid(r.id.tv_goods_attribute1); gtextview goods_num = (gtextview) convertview.findviewbyid(r.id.tv_goods_num1); gtextview goods_price = (gtextview) convertview.findviewbyid(r.id.tv_goods_price1); goods_info.settext(order_goods.getgoods_name()); goods_attribute.settext(order_goods.getspecification()); goods_num.settext("x" + order_goods.getquantity()); goods_price.settext("¥:" + order_goods.getprice()); log.i("tag", "order_goods.getorder_id()=" + order_goods.getorder_id()); imageloader.init(fileutils.getcache(context)); imageloader.displayimage(order_goods.getgoods_image(), mimage, options); convertview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { switch (status) { case "40": intent intent = new intent(context, message_logistics.class); intent.putextra("order_id", order_goods.getorder_id()); context.startactivity(intent); break; case "30": intent intent2 = new intent(context, message_logistics.class); intent2.putextra("order_id", order_goods.getorder_id()); context.startactivity(intent2); break; case "20": intent intent3 = new intent(context, message_delivery.class); intent3.putextra("order_id", order_goods.getorder_id()); context.startactivity(intent3); break; default: break; } } }); return convertview; } }
底部:
public class orderbottom implements ordercontent { private order order; private oncesendview2 shopimageview; private list<order_goods> list; private acache acache; public orderbottom(order order, list<order_goods> list) { this.order = order; this.list = list; } @override public int getlayout() { return r.layout.item_list_list; } @override public boolean isclickable() { return true; } public order order() { return order; } @override public view getview(final context context, view convertview, layoutinflater inflater) { acache = acache.get(context); inflater = layoutinflater.from(context); convertview = inflater.inflate(getlayout(), null); gtextview ordertotal = (gtextview) convertview.findviewbyid(r.id.tv_order_amount1); gtextview tv_order_datetime = (gtextview) convertview.findviewbyid(r.id.tv_order_datetime); final gbutton oncesend = (gbutton) convertview.findviewbyid(r.id.btn_send1); gtextview yunfei_text = (gtextview) convertview.findviewbyid(r.id.yunfei_text); yunfei_text.settext("(含运费: ¥ "+order.getshipping_fee()+")"); ordertotal.settext(order.getorder_amount()); tv_order_datetime.settext(data_time.getdatetostring(order.getadd_time())); final view finalconvertview = convertview; if (order.getstatus().equals("20")) { oncesend.settext("立即发货"); oncesend.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent otherintent = new intent(context, message_delivery.class); otherintent.putextra("order_id", order.getorder_id()); otherintent.putextra("token", acache.getasstring("token")); context.startactivity(otherintent); } }); } else if (order.getstatus().equals("30")){ oncesend.setbackgroundresource(r.color.highlight_color); oncesend.settext("查看物流"); oncesend.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent = new intent(context, message_logistics.class); intent.putextra("order_id", order.getorder_id()); context.startactivity(intent); } }); } else { oncesend.setvisibility(view.gone); } return convertview; } }
接收接口返回的数据,然后利用for循环将数据循环读入到上中下三个布局中
//解析后台返回的数据 type tp = new typetoken<orderitems>() { }.gettype(); orderitems goodsitem = (orderitems) parseutils.gson2object(s, tp); list<orderitems> orderitems = new arraylist<orderitems>(); orderitems.add(goodsitem); list<ordercontent> ordercontents; list<order> orderlist = new arraylist<order>(); if (goodsitem.getdata() != null) { orderlist = goodsitem.getdata().getorders(); ordercontents = new arraylist<ordercontent>(); list<order_goods> ordergoodses = new arraylist<order_goods>(); int totalpages = goodsitem.getdata().getpage().getpage_count(); //外部第一个循环,将数据循环读取后存到订单顶部 for (int k = 0; k < orderlist.size(); k++) { ordergoodses = orderlist.get(k).getorder_goods(); order ordertop = new order(); ordertop.setorder_sn(orderlist.get(k).getorder_sn()); ordertop.setstatus(orderlist.get(k).getstatus()); ordertop.setevaluation_status(orderlist.get(k).getevaluation_status()); itemordertop itemordertop = new itemordertop(ordertop, ordergoodses); ordercontents.add(itemordertop); if (ordergoodses == null) { baseutil.showtoast(context, "没有订单"); } else { //中间for循环,将数据循环读取后存到订单中间部分 for (int j = 0; j < ordergoodses.size(); j++) { order_goods goods = new order_goods(); goods.setprice(ordergoodses.get(j).getprice()); goods.setgoods_name(ordergoodses.get(j).getgoods_name()); goods.setquantity(ordergoodses.get(j).getquantity()); goods.setgoods_image(ordergoodses.get(j).getgoods_image()); goods.setspecification(ordergoodses.get(j).getspecification()); goods.setorder_id(ordergoodses.get(j).getorder_id()); orderimiddle orderimiddle = new orderimiddle(goods, orderlist.get(k).getstatus()); ordercontents.add(orderimiddle); log.i("mylog", "ordercontents =" + ordercontents); } } //外部第二个循环,将数据循环读取后存到订单底部 order order = new order(); order.setorder_sn(orderlist.get(k).getorder_sn()); order.setorder_id(orderlist.get(k).getorder_id()); order.setstatus(orderlist.get(k).getstatus()); order.setadd_time(orderlist.get(k).getadd_time()); order.setorder_amount(orderlist.get(k).getorder_amount()); order.setshipping_fee(orderlist.get(k).getshipping_fee()); orderbottom orderbottom = new orderbottom(order, ordergoodses); ordercontents.add(orderbottom); } mupdatelistview(ordercontents, morderadapter, mlistview, page, totalpages); } else { baseutil.showtoast(context, "没有订单"); } }
然后利用setadapter方法将数据传到adapter中。
以下是adapter的实现方法
public class orderparentlistadapter extends baseadapter { private context context; private list<ordercontent> ordercontents; private layoutinflater miflater; public orderparentlistadapter(context context, list<ordercontent> ordercontents) { this.context = context; if(ordercontents!=null) { this.ordercontents = ordercontents; } else { this.ordercontents =new arraylist<ordercontent>(); } } @override public int getcount() { return ordercontents.size(); } @override public object getitem(int position) { return ordercontents.get(position); } @override public long getitemid(int position) { return position; } @override public boolean isenabled(int position) { // todo auto-generated method stub return ordercontents.get(position).isclickable(); } @override public view getview(int position, view convertview, viewgroup parent) { return ordercontents.get(position).getview(context,convertview,miflater); } public void upatelist(list<ordercontent> ordercontents) { // todo auto-generated method stub this.ordercontents.addall(ordercontents); this.notifydatasetchanged(); } public void clearlistview() { // todo auto-generated method stub this.ordercontents.clear(); } }
最后,使用此方法需要和接口沟通好数据返回的格式;
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。