功能基本和淘宝购物车一样,商品按照店铺分类显示,全选,反选,选中商品数量变化,总价随之变化。效果图

思路:店铺和商品都增加一个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:id="@+id/titleBar"android:layout_width="match_parent"android:layout_height="wrap_content"app:rightTitle="管理"app:title="购物车" /><com.qiyetec.flyingsnail.widget.HintLayoutandroid:id="@+id/hintLayout"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"><com.scwang.smartrefresh.layout.SmartRefreshLayoutandroid:id="@+id/smartRefreshLayout"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:id="@+id/rv"android:layout_width="match_parent"android:layout_height="wrap_content" /></com.scwang.smartrefresh.layout.SmartRefreshLayout></com.qiyetec.flyingsnail.widget.HintLayout><LinearLayoutandroid:id="@+id/ll_bottom"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"><CheckBoxandroid:id="@+id/cb_all"android:layout_width="wrap_content"android:layout_height="wrap_content"android:button="@drawable/selector_checkbox_green"android:text="全选" /><Viewandroid:layout_width="0dp"android:layout_height="1dp"android:layout_weight="1" /><LinearLayoutandroid:id="@+id/ll"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:id="@+id/tv_total_price"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:id="@+id/tv_buy"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><TextViewandroid:id="@+id/tv_del"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;@Overrideprotected int getLayoutId() {return R.layout.activity_shopping_car;}@Overrideprotected void initView() {setOnClickListener(R.id.tv_buy, R.id.tv_del);}@Overrideprotected 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() {@Overridepublic void onClick(boolean allSelect, String totalPrice) {cb_all.setChecked(allSelect);tv_total_price.setText("¥" + totalPrice);}});rv.setAdapter(shoppingCarAdapter);//全选 反选cb_all.setOnClickListener(new View.OnClickListener() {@Overridepublic 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() {@Overridepublic void onRefresh(@NonNull RefreshLayout refreshLayout) {page = 1;getCarData();refreshLayout.finishRefresh();}}).setOnLoadMoreListener(new OnLoadMoreListener() {@Overridepublic 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();}}@Overridepublic void onDestroy() {super.onDestroy();EventBus.getDefault().unregister(this);}@Overridepublic 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@Overridepublic 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());elseobject.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;}}@Overridepublic HintLayout getHintLayout() {return hintLayout;}}

adapter代码

public final class ShoppingCarAdapter extends MyAdapter<ShoppingCarBean> {public ShoppingCarAdapter(Context context) {super(context);}@NonNull@Overridepublic 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"><CheckBoxandroid:id="@+id/cb"android:layout_width="wrap_content"android:layout_height="wrap_content"android:button="@drawable/selector_checkbox_green" /><ImageViewandroid:id="@+id/iv_shopicon"android:layout_width="25dp"android:layout_height="25dp"android:layout_marginLeft="10dp" /><TextViewandroid:id="@+id/tv_shopname"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:id="@+id/ll"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:id="@+id/cb"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:button="@drawable/selector_checkbox_green" /><ImageViewandroid:id="@+id/iv"android:layout_width="75dp"android:layout_height="75dp"android:layout_centerVertical="true"android:layout_marginLeft="8dp"android:layout_toRightOf="@id/cb" /><TextViewandroid:id="@+id/tv_title"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:id="@+id/tv_guige"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:id="@+id/tv_price"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"><TextViewandroid:id="@+id/tv_des"android:layout_width="25dp"android:layout_height="match_parent"android:gravity="center"android:text="-"android:textColor="#cccccc" /><Viewandroid:layout_width="0.5dp"android:layout_height="match_parent"android:background="#cccccc" /><TextViewandroid:id="@+id/tv_count"android:layout_width="30dp"android:layout_height="match_parent"android:gravity="center" /><Viewandroid:layout_width="0.5dp"android:layout_height="match_parent"android:background="#cccccc" /><TextViewandroid:id="@+id/tv_add"android:layout_width="25dp"android:layout_height="match_parent"android:gravity="center"android:text="+"android:textColor="#cccccc" /></LinearLayout></RelativeLayout>

Android 仿淘宝购物车实现相关推荐

  1. Android仿淘宝购物车

    最近项目需要实现类似淘宝购物车的功能,仿了一个,直接上代码: public class MainActivity extends Activity implements OnCartListener, ...

  2. Android仿淘宝购物车demo

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 夏的热情 ...

  3. Android一点 仿淘宝购物车动画

    首先看看ios上的淘宝购物车的动画效果ios淘宝购物车动画 我们实现的效果 看特效是分为两个界面,一个是主view,一个是弹出层.弹出层是用dialog实现的,只是加入了弹出的动画,这里就不分析了,我 ...

  4. Android仿淘宝详情页面viewPager滑动到最后一张图片跳转的功能

    需要做一个仿淘宝客户端ViewPager滑动到最后一页,再拖动的时候跳到详情的功能,刚开始我也迷糊了,通过查阅相关资料发现有好多种实现方法,下面小编给大家分享实例代码,感兴趣的朋友一起看看吧 需要做一 ...

  5. Android仿淘宝、京东Banner滑动查看图文详情

    文章目录 写在前面 效果图 原理分析 核心代码 源码地址 写在前面 本文基于 ViewPager2 实现的 Banner 效果,进而实现了仿淘宝.京东Banner滑动至最后一页时继续滑动来查看图文详情 ...

  6. Android仿淘宝首页UI(附代源代码及示例图片)

    Android仿淘宝首页UI(附代源代码及示例图片) 可以收获 运行出来的效果 部分代码 源代码 可以收获 更改Layout中的文字和drawble中的图片即可生成适应于不同情景的APP,帮助开发者完 ...

  7. Android仿淘宝淘口令实现

    先复制信息到剪切板,然后再打开淘宝,.既然是复制,肯定是复制到系统的剪切板了,我们可以通过下边的代码来把口令给复制到系统的剪切板里 1 2 3 4 5 6 //获取剪贴板管理器: ClipboardM ...

  8. Android仿淘宝tab返回

    一.概述                 淘宝相信大家都在用过,不过不知道各位有没有仔细观察过淘宝的tab界面,尤其是返回的时候的逻辑.最近闲来无事,猛然发现淘宝的tab界面还真的挺好玩,废话不多说, ...

  9. 仿淘宝购物车demo 增加和减少商品数量

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 在上一篇 ...

最新文章

  1. 转赋值表达式解析的流程
  2. Linux 文件系统概述
  3. 2019 深度学习框架大盘点!看 PyTorch、TensorFlow 如何强势上榜?
  4. 第一周周报(3月15-3月21)
  5. kafka : CommitFailedException already rebalanced and assigned max.poll.records
  6. (转载)在IAR及MDK里生成hex及bin文件的标准方法
  7. python网络蜘蛛
  8. 计算机五个盘,电脑分盘分几个盘合适,您知道吗?
  9. idea 配置svn插件
  10. 【小米小爱老师4G网络尊享版使用总结】界面|设置|安装|输入_摘要频道_什么值得买...
  11. Unity 模拟鼠标自动点击事件
  12. qtabwidget右键菜单_QTableWidget添加右键菜单的详细过程
  13. 渗透测试实验_安装Windows 2003 企业版
  14. MATLA 复制文件到指定文件夹
  15. mand-mobile 组件库 tab-bar组件滚动问题
  16. CSAPP LAB4 键盘驱动程序的分析与修改(谢罪)
  17. 小米10获取root权限_小米手机怎么才能完美ROOT-开发版稳定版通用
  18. 卡片式轮播图 效果 实现
  19. 万能实体类(pageDate)
  20. 图文并茂——从Kubernetes的诞生背景到什么是Kubernetes, 带你深度解析Kubernetes

热门文章

  1. Git之解决git stash pop多次产生的文件冲突问题
  2. 企业上市关注的股权结构问题
  3. android 名称的由来,三星新系统名称曝光:命名为Experience
  4. redis做用户登陆
  5. day06-代码发送邮件
  6. Python 爬虫自动下载OpenAI Key Papers
  7. STM32以太网通信-LWIP简介
  8. html+js做音乐播放器
  9. 【计组】学习笔记1.1:8421码和ASCII码
  10. 天津将设立市校两级专项资金 500万促学子创业