电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Android實現淘寶購物車

瀏覽:136日期:2022-06-04 11:04:41

本文實例為大家分享了Android實現淘寶購物車的具體代碼,供大家參考,具體內容如下

功能基本和淘寶購物車一樣,商品按照店鋪分類顯示,全選,反選,選中商品數量變化,總價隨之變化。效果圖

Android實現淘寶購物車

思路:店鋪和商品都增加一個select屬性,列表的CheckBox選擇或未選中狀態改變同時設置店鋪和商品的select屬性,每次CheckBox狀態改變設置select的值等于cb.isChecked()

購物車頁面布局文件activity_shopping_car

<?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' xmlns:tools='http://schemas.android.com/tools' android:layout_width='match_parent' android:layout_height='match_parent' android:orientation='vertical' tools:context='.ui.activity.ShoppingCarActivity'> <com.hjq.bar.TitleBarandroid: android:layout_width='match_parent'android:layout_height='wrap_content'app:rightTitle='管理'app:title='購物車' /> <com.qiyetec.flyingsnail.widget.HintLayoutandroid: android:layout_width='match_parent'android:layout_height='0dp'android:layout_weight='1'> <com.scwang.smartrefresh.layout.SmartRefreshLayout android: android:layout_width='match_parent' android:layout_height='match_parent'> <com.scwang.smartrefresh.layout.header.ClassicsHeaderandroid:layout_width='match_parent'android:layout_height='wrap_content' /> <androidx.recyclerview.widget.RecyclerViewandroid: android:layout_width='match_parent'android:layout_height='wrap_content' /> </com.scwang.smartrefresh.layout.SmartRefreshLayout> </com.qiyetec.flyingsnail.widget.HintLayout> <LinearLayoutandroid: android:layout_width='match_parent'android:layout_height='wrap_content'android:layout_marginTop='@dimen/ali_auth_space_10'android:background='#fff'android:gravity='center_vertical'android:padding='15dp'> <CheckBox android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:button='@drawable/selector_checkbox_green' android:text='全選' /> <View android:layout_width='0dp' android:layout_height='1dp' android:layout_weight='1' /> <LinearLayout android: android:layout_width='wrap_content' android:layout_height='wrap_content'> <TextViewandroid:layout_width='wrap_content'android:layout_height='wrap_content'android:text='合計:'android:textColor='#333'android:textSize='15sp' /> <TextViewandroid: android:layout_width='wrap_content'android:layout_height='wrap_content'android:layout_marginLeft='5dp'android:text='¥0.00'android:textColor='#DA3527'android:textSize='24sp' /> <TextViewandroid: android:layout_width='wrap_content'android:layout_height='wrap_content'android:layout_marginLeft='10dp'android:background='@drawable/shape_red10'android:paddingLeft='15dp'android:paddingTop='5dp'android:paddingRight='15dp'android:paddingBottom='5dp'android:text='結算'android:textColor='#fff'android:textSize='18sp' /></LinearLayout> <TextView android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:background='@drawable/shape_stroke_red15' android:paddingLeft='15dp' android:paddingTop='5dp' android:paddingRight='15dp' android:paddingBottom='5dp' android:text='刪除' android:textColor='#DA3527' android:textSize='18sp' android:visibility='gone' /> </LinearLayout></LinearLayout>

Java代碼

public class ShoppingCarActivity extends MyActivity implements StatusAction { @BindView(R.id.titleBar) TitleBar titleBar; @BindView(R.id.rv) RecyclerView rv; @BindView(R.id.hintLayout) HintLayout hintLayout; @BindView(R.id.ll_bottom) LinearLayout ll_bottom; @BindView(R.id.ll) LinearLayout ll; @BindView(R.id.tv_total_price) TextView tv_total_price; @BindView(R.id.tv_del) TextView tv_del; @BindView(R.id.cb_all) CheckBox cb_all; @BindView(R.id.smartRefreshLayout) SmartRefreshLayout smartRefreshLayout; private ShoppingCarAdapter shoppingCarAdapter; private int total_page, page = 1; @Override protected int getLayoutId() {return R.layout.activity_shopping_car; } @Override protected void initView() {setOnClickListener(R.id.tv_buy, R.id.tv_del); } @Override protected void initData() {EventBus.getDefault().register(this);//網絡請求 獲取購物車數據getCarData();LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);rv.setLayoutManager(linearLayoutManager);shoppingCarAdapter = new ShoppingCarAdapter(this);//商品選中價格改變回調shoppingCarAdapter.setOnPriceChangeListener(new ShoppingCarAdapter.OnPriceChangeListener() { @Override public void onClick(boolean allSelect, String totalPrice) { cb_all.setChecked(allSelect);tv_total_price.setText('¥' + totalPrice); }});rv.setAdapter(shoppingCarAdapter);//全選 反選cb_all.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) {if (shoppingCarAdapter != null && shoppingCarAdapter.getData() != null) { for (int i = 0; i < shoppingCarAdapter.getData().size(); i++) {shoppingCarAdapter.getData().get(i).setSelect(cb_all.isChecked());for (int j = 0; j < shoppingCarAdapter.getData().get(i).getItem_data().size(); j++) { shoppingCarAdapter.getData().get(i).getItem_data().get(j).setSelect(cb_all.isChecked());} } shoppingCarAdapter.notifyDataSetChanged(); shoppingCarAdapter.calculatePrice();} }});smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh(@NonNull RefreshLayout refreshLayout) {page = 1;getCarData();refreshLayout.finishRefresh(); }}).setOnLoadMoreListener(new OnLoadMoreListener() { @Override public void onLoadMore(@NonNull RefreshLayout refreshLayout) {if (page <= total_page) { getCarData();}refreshLayout.finishLoadMore(); }}); } @Subscribe(threadMode = ThreadMode.MAIN) public void onGetMessage(MessageWrap message) {if (message.message.equals('refresh_car')) { getCarData();} } @Override public void onDestroy() {super.onDestroy();EventBus.getDefault().unregister(this); } @Override public void onRightClick(View v) {if (titleBar.getRightTitle().equals('管理')) { titleBar.setRightTitle('完成'); ll.setVisibility(View.GONE); tv_del.setVisibility(View.VISIBLE);} else if (titleBar.getRightTitle().equals('完成')) { titleBar.setRightTitle('管理'); ll.setVisibility(View.VISIBLE); tv_del.setVisibility(View.GONE);} } @SingleClick @Override public void onClick(View v) {switch (v.getId()) { //點擊結算 case R.id.tv_buy:if (shoppingCarAdapter != null && shoppingCarAdapter.getData() != null) { JSONArray ja = new JSONArray(); for (ShoppingCarBean shoppingCarBean : shoppingCarAdapter.getData()) {JSONArray array = new JSONArray();JSONObject object = null;for (ShoppingCarBean.ItemDataBean itemDataBean : shoppingCarBean.getItem_data()) if (itemDataBean.isSelect()) {object = new JSONObject();if (StringUtils.isNotNullOrEmpty(shoppingCarBean.getShop().getShop_id())) object.put('shop_id', shoppingCarBean.getShop().getShop_id());else object.put('shop_id', 1);JSONObject object1 = new JSONObject();object1.put('item_id', itemDataBean.getItem_id());object1.put('count', itemDataBean.getCount());object1.put('cart_id', itemDataBean.getCart_id());object1.put('sku_id', itemDataBean.getSku_id());array.add(object1);object.put('items', array); }if (object != null) ja.add(object); } // Log.i('logger', 'onClick: ' + JSONObject.toJSONString(ja)); if (ja != null && ja.size() > 0) {//提交confirmOrder(ja); } elsetoast('請選擇商品');}break; case R.id.tv_del:if (shoppingCarAdapter != null && shoppingCarAdapter.getData() != null) { List<String> list = new ArrayList<>(); for (ShoppingCarBean shoppingCarBean : shoppingCarAdapter.getData()) {for (ShoppingCarBean.ItemDataBean itemDataBean : shoppingCarBean.getItem_data()) if (itemDataBean.isSelect()) {list.add(itemDataBean.getCart_id()); } } delCart(list);}break;} } @Override public HintLayout getHintLayout() {return hintLayout; } }

adapter代碼

public final class ShoppingCarAdapter extends MyAdapter<ShoppingCarBean> { public ShoppingCarAdapter(Context context) {super(context); } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {return new ViewHolder(); } final class ViewHolder extends MyAdapter.ViewHolder {@BindView(R.id.cb)CheckBox cb;@BindView(R.id.iv_shopicon)ImageView iv_shopicon;@BindView(R.id.tv_shopname)TextView tv_shopname;@BindView(R.id.ll)LinearLayout linearLayout; ViewHolder() { super(R.layout.item_car);} @Overridepublic void onBindView(int position) { ShoppingCarBean bean = getItem(position); tv_shopname.setText(bean.getShop().getShop_name()); cb.setChecked(bean.isSelect()); //每次添加前先移除所有布局 linearLayout.removeAllViews(); if (bean.getItem_data() != null) {//動態添加商品列表for (int i = 0; i < bean.getItem_data().size(); i++) { View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_item_car, null); CheckBox c_cb = view.findViewById(R.id.cb); ImageView iv_cover = view.findViewById(R.id.iv); TextView tv_title = view.findViewById(R.id.tv_title); TextView tv_guige = view.findViewById(R.id.tv_guige); TextView tv_price = view.findViewById(R.id.tv_price); TextView tv_count = view.findViewById(R.id.tv_count); TextView tv_des = view.findViewById(R.id.tv_des); TextView tv_add = view.findViewById(R.id.tv_add); tv_title.setText(bean.getItem_data().get(i).getTitle()); tv_price.setText('¥' + bean.getItem_data().get(i).getZk_price()); tv_count.setText('' + bean.getItem_data().get(i).getCount()); tv_guige.setText(bean.getItem_data().get(i).getSku()); Glide.with(getContext()).load(bean.getItem_data().get(i).getPic()) .transform(new RoundedCorners((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getContext().getResources().getDisplayMetrics()))) .into(iv_cover); if (bean.isSelect()) {c_cb.setChecked(true); } else {c_cb.setChecked(bean.getItem_data().get(i).isSelect()); } int finalI = i; tv_des.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) { if (bean.getItem_data().get(finalI).getCount() > 1) {bean.getItem_data().get(finalI).setCount(bean.getItem_data().get(finalI).getCount()-1);tv_count.setText(bean.getItem_data().get(finalI).getCount()+'');calculatePrice(); } elseToastUtils.show('不能再少了哦~'); } }); tv_add.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) { bean.getItem_data().get(finalI).setCount(bean.getItem_data().get(finalI).getCount()+1); tv_count.setText(bean.getItem_data().get(finalI).getCount()+''); calculatePrice();} }); c_cb.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) { boolean select = true; bean.getItem_data().get(finalI).setSelect(c_cb.isChecked()); for (int j = 0; j < bean.getItem_data().size(); j++) {if (bean.getItem_data().get(j).isSelect() == false) { select = false; break;} } bean.setSelect(select); notifyDataSetChanged(); calculatePrice();} }); view.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) { Intent intent = new Intent(getContext(), ShoppingDetailActivity.class); intent.putExtra('item_id', bean.getItem_data().get(finalI).getItem_id()); startActivity(intent);} }); linearLayout.addView(view);} } cb.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) { bean.setSelect(cb.isChecked()); for (int j = 0; j < bean.getItem_data().size(); j++) {bean.getItem_data().get(j).setSelect(cb.isChecked()); } notifyDataSetChanged(); calculatePrice();} });} } public interface OnPriceChangeListener {void onClick(boolean allSelect, String totalPrice); } private OnPriceChangeListener onPriceChangeListener; public void setOnPriceChangeListener(OnPriceChangeListener onPriceChangeListener) {this.onPriceChangeListener = onPriceChangeListener; } public void calculatePrice() {if (onPriceChangeListener != null) { BigDecimal total = new BigDecimal('0.00'); boolean allSelect = true; List<ShoppingCarBean> list = getData(); for (int i = 0; i < getItemCount(); i++) {if (list.get(i).isSelect() == false) allSelect = false;for (int j = 0; j < list.get(i).getItem_data().size(); j++) { if (list.get(i).getItem_data().get(j).isSelect()) {BigDecimal multiply = new BigDecimal(list.get(i).getItem_data().get(j).getZk_price()).multiply(new BigDecimal(list.get(i).getItem_data().get(j).getCount()));BigDecimal price = new BigDecimal(multiply + '');total = total.add(price); }} } onPriceChangeListener.onClick(allSelect, total + '');} }}

RecyclerView條目布局item_car

<?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='wrap_content' android:layout_marginLeft='15dp' android:layout_marginTop='10dp' android:layout_marginRight='15dp' android:background='@drawable/shape_white5' android:orientation='vertical' android:paddingLeft='10dp' android:paddingTop='15dp' android:paddingRight='15dp' android:paddingBottom='20dp'> <LinearLayoutandroid:layout_width='match_parent'android:layout_height='wrap_content'android:gravity='center_vertical'> <CheckBox android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:button='@drawable/selector_checkbox_green' /> <ImageView android: android:layout_width='25dp' android:layout_height='25dp' android:layout_marginLeft='10dp' /> <TextView android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:layout_marginLeft='10dp' android:textColor='#333' android:textSize='15sp' android:textStyle='bold' /> </LinearLayout> <LinearLayoutandroid: android:layout_width='match_parent'android:layout_height='wrap_content'android:orientation='vertical'> </LinearLayout> </LinearLayout>

商品條目布局layout_item_car

<?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='wrap_content' android:paddingTop='10dp' android:paddingBottom='5dp'> <CheckBoxandroid: android:layout_width='wrap_content'android:layout_height='wrap_content'android:layout_centerVertical='true'android:button='@drawable/selector_checkbox_green' /> <ImageViewandroid: android:layout_width='75dp'android:layout_height='75dp'android:layout_centerVertical='true'android:layout_marginLeft='8dp'android:layout_toRightOf='@id/cb' /> <TextViewandroid: android:layout_width='wrap_content'android:layout_height='wrap_content'android:layout_marginLeft='10dp'android:layout_toRightOf='@id/iv'android:ellipsize='end'android:maxLines='2'android:textColor='#333'android:textSize='15sp' /> <TextViewandroid: android:layout_width='wrap_content'android:layout_height='wrap_content'android:layout_below='@id/tv_title'android:layout_marginLeft='10dp'android:layout_marginTop='3dp'android:layout_toRightOf='@id/iv'android:background='@drawable/shape_gray5'android:paddingLeft='3dp'android:paddingRight='3dp'android:textColor='#C1C1C1'android:textSize='10sp' /> <TextViewandroid: android:layout_width='wrap_content'android:layout_height='wrap_content'android:layout_below='@id/tv_guige'android:layout_marginLeft='10dp'android:layout_marginTop='8dp'android:layout_toRightOf='@id/iv'android:textColor='#DA3527'android:textSize='18sp' /> <LinearLayoutandroid:layout_width='wrap_content'android:layout_height='25dp'android:layout_below='@id/tv_guige'android:layout_alignParentRight='true'android:layout_marginTop='8dp'android:layout_marginBottom='5dp'android:background='@drawable/stroke_gray2'> <TextView android: android:layout_width='25dp' android:layout_height='match_parent' android:gravity='center' android:text='-' android:textColor='#cccccc' /> <View android:layout_width='0.5dp' android:layout_height='match_parent' android:background='#cccccc' /> <TextView android: android:layout_width='30dp' android:layout_height='match_parent' android:gravity='center' /> <View android:layout_width='0.5dp' android:layout_height='match_parent' android:background='#cccccc' /> <TextView android: android:layout_width='25dp' android:layout_height='match_parent' android:gravity='center' android:text='+' android:textColor='#cccccc' /> </LinearLayout> </RelativeLayout>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: 淘寶
相關文章:
主站蜘蛛池模板: 钢制拖链生产厂家-全封闭钢制拖链-能源钢铝拖链-工程塑料拖链-河北汉洋机械制造有限公司 | 【直乐】河北石家庄脊柱侧弯医院_治疗椎间盘突出哪家医院好_骨科脊柱外科专业医院_治疗抽动症/关节病骨伤权威医院|排行-直乐矫形中医医院 | 咖啡加盟,咖啡店加盟连锁品牌-卡小逗| 复盛空压机配件-空气压缩机-复盛空压机(华北)总代理 | 模具ERP_模具管理系统_模具mes_模具进度管理_东莞市精纬软件有限公司 | 电子厂招聘_工厂招聘_普工招聘_小时工招聘信息平台-众立方招工网 | 高低温万能试验机_拉力试验机_拉伸试验机-馥勒仪器科技(上海)有限公司 | PC阳光板-PC耐力板-阳光板雨棚-耐力板雨棚,厂家定制[优尼科板材] | 根系分析仪,大米外观品质检测仪,考种仪,藻类鉴定计数仪,叶面积仪,菌落计数仪,抑菌圈测量仪,抗生素效价测定仪,植物表型仪,冠层分析仪-杭州万深检测仪器网 | 上海诺狮景观规划设计有限公司 | 台式低速离心机-脱泡离心机-菌种摇床-常州市万丰仪器制造有限公司 | 垃圾清运公司_环卫保洁公司_市政道路保洁公司-华富环境 | 盘煤仪,盘料仪,盘点仪,堆料测量仪,便携式激光盘煤仪-中科航宇(北京)自动化工程技术有限公司 | 模温机-油温机-电加热导热油炉-工业冷水机「欧诺智能」 | 广州番禺搬家公司_天河黄埔搬家公司_企业工厂搬迁_日式搬家_广州搬家公司_厚道搬迁搬家公司 | 直读光谱仪,光谱分析仪,手持式光谱仪,碳硫分析仪,创想仪器官网 | FFU_空气初效|中效|高效过滤器_空调过滤网-广州梓净净化设备有限公司 | 高防护蠕动泵-多通道灌装系统-高防护蠕动泵-www.bjhuiyufluid.com慧宇伟业(北京)流体设备有限公司 | PVC快速门-硬质快速门-洁净室快速门品牌厂家-苏州西朗门业 | 冲击式破碎机-冲击式制砂机-移动碎石机厂家_青州市富康机械有限公司 | 12cr1mov无缝钢管切割-15crmog无缝钢管切割-40cr无缝钢管切割-42crmo无缝钢管切割-Q345B无缝钢管切割-45#无缝钢管切割 - 聊城宽达钢管有限公司 | 硫化罐-电加热蒸汽硫化罐生产厂家-山东鑫泰鑫智能装备有限公司 | 珠海白蚁防治_珠海灭鼠_珠海杀虫灭鼠_珠海灭蟑螂_珠海酒店消杀_珠海工厂杀虫灭鼠_立净虫控防治服务有限公司 | 高通量组织研磨仪-多样品组织研磨仪-全自动组织研磨仪-研磨者科技(广州)有限公司 | 自动售货机_无人售货机_专业的自动售货机运营商_免费投放售货机-广州富宏主官网 | 杭州网络公司_百度SEO优化-外贸网络推广_抖音小程序开发-杭州乐软科技有限公司 | 送料机_高速冲床送料机_NC伺服滚轮送料机厂家-东莞市久谐自动化设备有限公司 | 碎石机设备-欧版反击破-欧版颚式破碎机(站)厂家_山东奥凯诺机械 高低温试验箱-模拟高低温试验箱订制-北京普桑达仪器科技有限公司【官网】 | 东莞市海宝机械有限公司-不锈钢分选机-硅胶橡胶-生活垃圾-涡电流-静电-金属-矿石分选机 | 海鲜池-专注海鲜鱼缸、移动海鲜缸、饭店鱼缸设计定做-日晟水族厂家 | LED太阳能中国结|发光红灯笼|灯杆造型灯|节日灯|太阳能灯笼|LED路灯杆装饰造型灯-北京中海轩光电 | 塑料脸盆批发,塑料盆生产厂家,临沂塑料广告盆,临沂家用塑料盆-临沂市永顺塑业 | 走心机厂家,数控走心机-台州博城智能科技有限公司 | 仿古建筑设计-仿古建筑施工-仿古建筑公司-汉匠古建筑设计院 | 洁净棚-洁净工作棚-无菌室-净化工程公司_北京卫护科技有限公司 | 沟盖板_复合沟盖板厂_电力盖板_树脂雨水篦子-淄博拜斯特 | 成都竞价托管_抖音代运营_网站建设_成都SEM外包-成都智网创联网络科技有限公司 | 西安标准厂房_陕西工业厂房_西咸新区独栋厂房_长信科技产业园官方网站 | 世界箱包品牌十大排名,女包小众轻奢品牌推荐200元左右,男包十大奢侈品牌排行榜双肩,学生拉杆箱什么品牌好质量好 - Gouwu3.com | 广州监控安装公司_远程监控_安防弱电工程_无线wifi覆盖_泉威安防科技 | 酒糟烘干机-豆渣烘干机-薯渣烘干机-糟渣烘干设备厂家-焦作市真节能环保设备科技有限公司 |