HttpUtils+MVP的Android购物车怎么实现
程序员文章站
2022-05-25 17:28:56
首先是依赖
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.okhttp3:okhttp...
首先是依赖
compile 'com.google.code.gson:gson:2.6.2' compile 'com.squareup.okhttp3:okhttp:3.3.0' compile 'com.github.bumptech.glide:glide:3.7.0'
购物车的main布局
二级列表的父布局
二级列表的子布局
自定义加减类的布局
//一个灰边shape
mvp用到的接口
//view接口 public interface imainview { //显示数据 void showshoppcart(list data); //显示总价 void showsum(double price); //获取总价的集合 list datap(); } //model接口 public interface imodel { //获取购物车数据 void getshoppdata(string surl, map map,shopplistener shopplistener); //计算总价 void ji_price(list data,jipricelistener jipricelistener); } //presenter接口 public interface ipresenter { void showshoppcarttoview(imodel imodel, imainview imainview); void showsumtoview(imodel imodel, imainview imainview); } //获取购物车数据的监听接口 public interface shopplistener { void shoppsuccess(string json); void shopperror(string error); } //计算价格的监听接口 public interface jipricelistener { void jiprice(double price); }
m层获取数据
public class shoppmodel implements imodel { @override public void getshoppdata(string surl, map map, final shopplistener shopplistener) { httputils httputils = httputils.gethttputils(); httputils.okpost(surl,map); httputils.setokloadlistener(new httputils.okloadlistener() { @override public void okloadsuccess(string json) { shopplistener.shoppsuccess(json); } @override public void okloaderror(string error) { shopplistener.shopperror(error); } }); } //计算价格 @override public void ji_price(list data, jipricelistener jipricelistener) { double sum = 0; for (int i = 0; i < data.size(); i++) { list list = data.get(i).getlist(); for (int j = 0; j < list.size(); j++) { boolean child_flag = list.get(j).ischild_flag(); if (child_flag) { double price = list.get(j).getprice()*list.get(j).getnum(); sum += price; } } } jipricelistener.jiprice(sum); } }
p层一些逻辑运算
public class shopppresenter implements ipresenter { private static final string tag = "shopppresenter+++"; @override public void showshoppcarttoview(imodel imodel, final imainview imainview) { map map = new hashmap<>(); map.put("uid","10756"); imodel.getshoppdata(apiutil.select_url, map, new shopplistener() { @override public void shoppsuccess(string json) { shoppbean shoppbean = new gson().fromjson(json, shoppbean.class); list data = shoppbean.getdata(); imainview.showshoppcart(data); } @override public void shopperror(string error) { log.d(tag,"error:"+error); } }); } @override public void showsumtoview(imodel imodel, final imainview imainview) { imodel.ji_price(imainview.datap(), new jipricelistener() { @override public void jiprice(double price) { imainview.showsum(price); } }); } }
v层展示数据
public class mainviewactivity extends appcompatactivity implements view.onclicklistener, imainview { private expandablelistview expan_able; private checkbox quan_cbox; private textview total; private textview qu_total; private shopppresenter shopppresenter; private list list_tem; private myexadapter myexadapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initviews(); initdatas(); } private void initdatas() { shopppresenter = new shopppresenter(); shopppresenter.showshoppcarttoview(new shoppmodel(), this); } public void initviews() { expan_able = findviewbyid(r.id.expan_able); quan_cbox = findviewbyid(r.id.quan_cbox); total = findviewbyid(r.id.total); qu_total = findviewbyid(r.id.qu_total); quan_cbox.setonclicklistener(this); qu_total.setonclicklistener(this); } @override public void onclick(view v) { switch (v.getid()) { //全选,计算总价 case r.id.quan_cbox: boolean quan_checked = quan_cbox.ischecked(); for (int i = 0; i < list_tem.size(); i++) { list_tem.get(i).setparent_flag(quan_checked); list list = list_tem.get(i).getlist(); for (int j = 0; j < list.size(); j++) { list.get(j).setchild_flag(quan_checked); } } //刷新适配器 myexadapter.notifydatasetchanged(); //调用计算总价的方法 shopppresenter shopppresenter = new shopppresenter(); shopppresenter.showsumtoview(new shoppmodel(),this); break; case r.id.qu_total: toast.maketext(mainviewactivity.this,"去计算总价",toast.length_short).show(); break; } } //展示购物车数据 @override public void showshoppcart(final list data) { list_tem = data; myexadapter = new myexadapter(mainviewactivity.this, data, this); expan_able.setadapter(myexadapter); //展开expandablelistview int count = expan_able.getcount(); for (int i = 0; i < count; i++) { expan_able.expandgroup(i); } } //显示总价 @override public void showsum(double price) { total.settext("合计:" + price); } @override public list datap() { return list_tem; } }
二级列表适配器
public class myexadapter extends baseexpandablelistadapter { private context context; private list data; private imainview imainview; public myexadapter(context context, list data, imainview imainview) { this.context = context; this.data = data; this.imainview = imainview; } @override public int getgroupcount() { return data.size(); } @override public int getchildrencount(int groupposition) { return data.get(groupposition).getlist().size(); } @override public object getgroup(int groupposition) { return data.get(groupposition); } @override public object getchild(int groupposition, int childposition) { return data.get(groupposition).getlist().get(childposition); } @override public long getgroupid(int groupposition) { return groupposition; } @override public long getchildid(int groupposition, int childposition) { return childposition; } @override public boolean hasstableids() { return true; } @override public view getgroupview(final int groupposition, boolean isexpanded, view convertview, viewgroup parent) { final parentviewholder parentviewholder; if (convertview == null){ parentviewholder = new parentviewholder(); convertview = view.inflate(context, r.layout.item_parent,null); parentviewholder.parent_box = convertview.findviewbyid(r.id.parent_box); parentviewholder.parent_title = convertview.findviewbyid(r.id.parent_title); convertview.settag(parentviewholder); }else { parentviewholder = (parentviewholder) convertview.gettag(); } //赋值 parentviewholder.parent_title.settext(data.get(groupposition).getsellername()); //父复选框赋状态值 parentviewholder.parent_box.setchecked(data.get(groupposition).isparent_flag()); //复选框的点击事件 parentviewholder.parent_box.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { boolean ischecked = parentviewholder.parent_box.ischecked(); //给自己父复选框赋值 data.get(groupposition).setparent_flag(ischecked); //给子列表赋值 list list = data.get(groupposition).getlist(); for (int i = 0; i < list.size(); i++) { shoppbean.databean.listbean listbean = list.get(i); listbean.setchild_flag(ischecked); } notifydatasetchanged(); //调用计算的方法 shopppresenter shopppresenter = new shopppresenter(); shopppresenter.showsumtoview(new shoppmodel(),imainview); } }); return convertview; } @override public view getchildview(final int groupposition, final int childposition, boolean islastchild, view convertview, viewgroup parent) { final childviewholder childviewholder; if (convertview == null){ childviewholder = new childviewholder(); convertview = view.inflate(context,r.layout.item_child,null); childviewholder.child_box = convertview.findviewbyid(r.id.child_box); childviewholder.child_img = convertview.findviewbyid(r.id.child_img); childviewholder.child_title = convertview.findviewbyid(r.id.child_title); childviewholder.child_price = convertview.findviewbyid(r.id.child_price); childviewholder.child_delete = convertview.findviewbyid(r.id.child_delete); childviewholder.addsub = convertview.findviewbyid(r.id.addsub); convertview.settag(childviewholder); }else { childviewholder = (childviewholder) convertview.gettag(); } //赋值 final list list = data.get(groupposition).getlist(); childviewholder.child_title.settext(list.get(childposition).gettitle()); childviewholder.child_price.settext(list.get(childposition).getprice()+""); //子复选框赋状态值 childviewholder.child_box.setchecked(list.get(childposition).ischild_flag()); string[] split = list.get(childposition).getimages().split("\\|"); glide.with(context).load(split[0]).into(childviewholder.child_img); childviewholder.addsub.setcount(list.get(childposition).getnum()); childviewholder.addsub.setonaddclick(new addsubview.onaddsubclick() { @override public void onaddsubclick() { string count = childviewholder.addsub.getcount(); list.get(childposition).setnum(integer.parseint(count)); notifydatasetchanged(); shopppresenter shopppresenter = new shopppresenter(); shopppresenter.showsumtoview(new shoppmodel(),imainview); } }); //子条目复选框的点击事件 childviewholder.child_box.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //获取当前条目状态 boolean child_checked = childviewholder.child_box.ischecked(); //设置当前子条目的状态 list.get(childposition).setchild_flag(child_checked); //for循环当前的状态 boolean ch_flag = true;//一个开关标记 for (int i = 0; i 自定义加减类public class addsubview extends linearlayout implements view.onclicklistener { private textview add; private textview sub; private edittext count; private onaddsubclick onaddclick; public addsubview(context context) { this(context,null); } public addsubview(context context, @nullable attributeset attrs) { this(context, attrs,0); } public addsubview(context context, @nullable attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); view view = view.inflate(context, r.layout.add_layout, this); add = view.findviewbyid(r.id.add); sub = view.findviewbyid(r.id.sub); count = view.findviewbyid(r.id.count); add.setonclicklistener(this); sub.setonclicklistener(this); } @override public void onclick(view v) { switch (v.getid()){ case r.id.add: add(); onaddclick.onaddsubclick(); break; case r.id.sub: sub(); onaddclick.onaddsubclick(); break; } } //添加 public void add(){ string s = count.gettext().tostring(); int i = integer.parseint(s); i++; count.settext(i+""); } //减少 public void sub(){ string s = count.gettext().tostring(); int i = integer.parseint(s); if (i>=1){ i--; count.settext(i+""); } } //设置数量 public void setcount(int cousize){ count.settext(cousize+""); } //获取数量 public string getcount(){ return count.gettext().tostring(); } public interface onaddsubclick{ void onaddsubclick(); } public void setonaddclick(onaddsubclick onaddclick){ this.onaddclick = onaddclick; } }();>