Android实现商城购物车功能的实例代码
程序员文章站
2023-11-22 15:12:22
最近公司项目做商城模块,需要实现购物车功能,主要实现了单选、全选,金额合计,商品删除,商品数量加减等功能,先看看效果图:
在这里插入图片描述
一、实现步骤:...
最近公司项目做商城模块,需要实现购物车功能,主要实现了单选、全选,金额合计,商品删除,商品数量加减等功能,先看看效果图:
在这里插入图片描述
一、实现步骤:
0、添加依赖库
1.购物车主界面布局文件(activity_main.xml)
2.购物车实现逻辑主界面(mainactivity.class)
3.使用expandablelistview,继承baseexpandablelistadapter
4.购物车数据的bean类(shoppingcardatabean.class)
5.分店铺实现布局
6.购物车中商品item布局文件
二、实现过程:
0.添加依赖库
implementation 'com.jakewharton:butterknife:5.1.1' implementation 'com.google.code.gson:gson:2.2.4' implementation 'com.github.bumptech.glide:glide:3.7.0'
1.购物车主界面布局文件(activity_main.xml)
<?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>
2.购物车实现逻辑主界面(mainactivity.class)
package com.showly.yglin.shoppingcar; import android.content.context; import android.content.dialoginterface; import android.os.bundle; import android.support.v7.app.appcompatactivity; 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 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); } } /** * 判断是否要弹出删除的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(可以自定义弹窗,不用删除即可) * * @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; } } }; }
3.使用expandablelistview,继承baseexpandablelistadapter
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; } 4.购物车数据的bean类(shoppingcardatabean.class) 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; } } } }
5.分店铺实现布局
效果图(红色部分):
在这里插入图片描述
<?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>
6.购物车中商品item布局文件
效果图:
在这里插入图片描述
<?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>
自此,购物车的功能基本已经实现了
总结
以上所述是小编给大家介绍的android实现商城购物车功能的实例代码,希望对大家有所帮助
下一篇: PHP实现的构造sql语句类实例