【Android】实现简易购物车功能(附源码)
先上结果:

代码:
首先引入图片加载:
implementation 'com.github.bumptech.glide:glide:4.15.1'

配置权限清单:
<!-- 网络权限 --><uses-permission android:name="android.permission.INTERNET"/><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

页面布局:activity_main.xml
<?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=".MainActivity"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:background="#2196F3"><TextViewandroid:id="@+id/personalCenterText"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="购物车"android:textColor="#ffffff"android:textSize="24sp" /><TextViewandroid:id="@+id/edit"android:text="编辑"android:textColor="@color/white"android:textSize="24sp"android:layout_marginEnd="10dp"android:layout_centerVertical="true"android:layout_alignParentEnd="true"android:layout_width="wrap_content"android:layout_height="wrap_content"/></RelativeLayout><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/recyclerView"android:layout_weight="1"android:layout_width="match_parent"android:layout_height="0dp"/><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="50dp"><CheckBoxandroid:id="@+id/allSelect"android:text="全选"android:layout_centerVertical="true"android:textColor="@color/colorAccent"android:layout_width="wrap_content"android:layout_height="wrap_content"/><TextViewandroid:id="@+id/total"android:textSize="20sp"android:text="合计0.00¥"android:textColor="@color/colorAccent"android:layout_centerVertical="true"android:layout_toStartOf="@id/pay"android:layout_marginEnd="10dp"android:layout_width="wrap_content"android:layout_height="wrap_content"/><Buttonandroid:id="@+id/pay"android:text="结算"android:textColor="@color/white"android:background="@drawable/button_red"android:layout_width="100dp"android:layout_alignParentEnd="true"android:layout_height="wrap_content"/></RelativeLayout></LinearLayout>
条目布局:item_cart.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="100dp"><CheckBoxandroid:id="@+id/checkbox"android:layout_centerVertical="true"android:layout_width="wrap_content"android:layout_height="wrap_content"/><ImageViewandroid:id="@+id/cover"android:layout_width="80dp"android:layout_height="80dp"android:layout_margin="10dp"android:layout_toEndOf="@id/checkbox" /><LinearLayoutandroid:layout_toEndOf="@id/cover"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><TextViewandroid:id="@+id/name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textStyle="bold"android:textSize="18sp"android:textColor="@color/black"android:maxLines="2"android:ellipsize="end"android:layout_marginTop="5dp" /><TextViewandroid:id="@+id/price"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text=""android:textColor="#D32F2F"android:textStyle="bold"android:layout_marginTop="5dp" /></LinearLayout><LinearLayoutandroid:id="@+id/view_number"android:layout_width="wrap_content"android:layout_height="23dp"android:background="@drawable/shape_cart_item_add_cut_border"android:divider="@drawable/shape_divider_1_v"android:orientation="horizontal"android:showDividers="middle"android:layout_marginBottom="10dp"android:layout_marginEnd="10dp"android:layout_alignParentBottom="true"android:layout_alignParentEnd="true"><TextViewandroid:id="@+id/tv_reduce"android:layout_width="27dp"android:layout_height="match_parent"android:gravity="center"android:text="-"android:textColor="#676767"android:textSize="15sp"/><TextViewandroid:id="@+id/tv_num"android:layout_width="wrap_content"android:layout_height="match_parent"android:gravity="center"android:minWidth="40dp"android:paddingHorizontal="12dp"android:singleLine="true"android:text="1"android:textColor="#676767"android:textSize="15sp"/><TextViewandroid:id="@+id/tv_add"android:layout_width="27dp"android:layout_height="match_parent"android:gravity="center"android:text="+"android:textColor="#676767"android:textSize="15sp"/></LinearLayout>
</RelativeLayout>
资源文件:shape_cart_item_add_cut_border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"android:visible="true"><!-- 描边,边框 --><strokeandroid:width="1px"android:color="#E0E0E0"/><!--dashGap虚线段的间距、dashWidth虚线段的长度-->
</shape>
shape_divider_1_v.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"android:visible="true"><sizeandroid:width="1px"/><!-- 宽度和高度 --><!-- 填充 --><solidandroid:color="#E0E0E0"/><!-- 填充的颜色 -->
</shape>
适配器:CartAdapter
public class CartAdapter extends RecyclerView.Adapter<CartAdapter.Holder> {private final List<CartBean> list;private final Context context;private final List<CartBean> selects=new ArrayList<>();public CartAdapter(List<CartBean> list, Context context) {this.list = list;this.context = context;}@NonNull@Overridepublic Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {View view= LayoutInflater.from(context).inflate(R.layout.item_cart,null,false);return new Holder(view);}@Overridepublic void onBindViewHolder(@NonNull Holder holder, int position) {CartBean cartBean =list.get(position);holder.name.setText(cartBean.getName());holder.number.setText(String.valueOf(cartBean.getNumber()));holder.price.setText(String.format("%1$.2f¥", cartBean.getPrice()));holder.reduce.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {int n;n = Integer.parseInt(holder.number.getText().toString());if (n >1){n = n -1;holder.number.setText(String.valueOf(n));cartBean.setNumber(n);}else {Toast.makeText(context,"最少选择一件",Toast.LENGTH_SHORT).show();}updateItem();}});holder.add.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {int n;n = Integer.parseInt(holder.number.getText().toString());n = n + 1;cartBean.setNumber(n);holder.number.setText(String.valueOf(n));updateItem();}});holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton compoundButton, boolean b) {cartBean.setSelect(b);updateItem();}});holder.checkBox.setChecked(cartBean.isSelect());Glide.with(context).load(cartBean.getCover()).into(holder.cover);}@Overridepublic int getItemCount() {return list.size();}public static class Holder extends RecyclerView.ViewHolder{CheckBox checkBox;ImageView cover;TextView name,number,reduce,add,price;public Holder(@NonNull View itemView) {super(itemView);checkBox=itemView.findViewById(R.id.checkbox);cover=itemView.findViewById(R.id.cover);name=itemView.findViewById(R.id.name);number=itemView.findViewById(R.id.tv_num);reduce=itemView.findViewById(R.id.tv_reduce);add=itemView.findViewById(R.id.tv_add);price=itemView.findViewById(R.id.price);}}private void updateItem(){selects.clear();for (CartBean cartBean:list){if (cartBean.isSelect()){selects.add(cartBean);}}onChange.change(selects);}public OnChange onChange;public void setOnChange(OnChange onChange) {this.onChange = onChange;}public List<CartBean> getSelects() {return selects;}//条目改变-接口回调public interface OnChange{void change(List<CartBean> selects);}
}
bean类:CartBean
public class CartBean {private String name;private String cover;private boolean isSelect;private int number;private double price;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getCover() {return cover;}public void setCover(String cover) {this.cover = cover;}public boolean isSelect() {return isSelect;}public void setSelect(boolean select) {isSelect = select;}public int getNumber() {return number;}public void setNumber(int number) {this.number = number;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public CartBean(String name, String cover, boolean isSelect, int number, double price) {this.name = name;this.cover = cover;this.isSelect = isSelect;this.number = number;this.price = price;}@Overridepublic String toString() {return "CartBean{" +"name='" + name + '\'' +", cover='" + cover + '\'' +", isSelect=" + isSelect +", number=" + number +", price=" + price +'}';}
}
源码
github:https://github.com/panzhusheng/CartDemo
gitee:https://gitee.com/pan-zs/cart-demo
相关文章:
【Android】实现简易购物车功能(附源码)
先上结果: 代码: 首先引入图片加载: implementation com.github.bumptech.glide:glide:4.15.1配置权限清单: <!-- 网络权限 --><uses-permission android:name"android.permission.INTERNET"/><uses…...
使用Excel计算--任务完成总工作日时间段
(Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu) 引言 计算任务完成时间周期,和计算金钱一样,是一个比较细致严谨的工作。 通常,我们可能以为,完成周期形如: 任务完成周期 任务结束时间 - 任务开始时间 但是…...
.NET高级面试指南专题一【委托和事件】
在C#中,委托(Delegate)和事件(Event)是两个重要的概念,它们通常用于实现事件驱动编程和回调机制。 委托定义: 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个…...
基于springboot+vue的在线教育系统(前后端分离)
博主主页:猫头鹰源码 博主简介:Java领域优质创作者、CSDN博客专家、公司架构师、全网粉丝5万、专注Java技术领域和毕业设计项目实战 主要内容:毕业设计(Javaweb项目|小程序等)、简历模板、学习资料、面试题库、技术咨询 文末联系获取 项目背景…...
54-函数的3种定义,函数的4种调用:函数模式调用,方法模式调用,构造函数模式调用,apply call bind调用
一.函数的3种定义 1.函数的声明定义:具有声明提升 <script>//函数声明定义function fn(){}</script> 2.函数的表达式定义 <script>//匿名式表达式var fn = function(){}//命名式表达式var fn1 = function a(){}</script> 3.构造函数定义 var 变量…...
[C#]winform部署yolov5实例分割模型onnx
【官方框架地址】 https://github.com/ultralytics/yolov5 【算法介绍】 YOLOv5实例分割是目标检测算法的一个变种,主要用于识别和分割图像中的多个物体。它是在YOLOv5的基础上,通过添加一个实例分割模块来实现的。 在实例分割中,算法不仅…...
C++核心编程:类和对象 笔记
4.类和对象 C面向对象的三大特性为:封装,继承,多态C认为万事万物都皆为对象,对象上有其属性和行为 例如: 人可以作为对象,属性有姓名、年龄、身高、体重...,行为有走、跑、跳、说话...车可以作为对象,属性有轮胎、方向盘、车灯…...
机器学习实验3——支持向量机分类鸢尾花
文章目录 🧡🧡实验内容🧡🧡🧡🧡数据预处理🧡🧡代码认识数据相关性分析径向可视化各个特征之间的关系图 🧡🧡支持向量机SVM求解🧡🧡直觉…...
R语言【taxlist】——clean():移除孤立的记录
Package taxlist version 0.2.4 Description 对于 taxlist 类对象的操作可能会产生独立的条目。clean() 方法就是用来删除这样的条目,并恢复 taxlist 对象的一致性。 Usage clean(object, ...)## S4 method for signature taxlist clean(object, times 2, ...) A…...
CentOS 7.9 OS Kernel Update 3.10 to 4.19
date: 2024-01-18, 2024-01-26 原 OS Kernel 3.10 升级至 4.19 1.检查默认内核 检查 vmlinuz 版本 [rootlocalhost ~]# grubby --default-kernel /boot/vmlinuz-3.10.0-1160.105.1.el7.x86_64 [rootlocalhost ~]#检查 Linux 内核版本 [rootlocalhost ~]# uname -a Linux loc…...
k8s---安全机制
k8s的安全机制,分布式集群管理工具,就是容器编排。安全机制的核心:APIserver。为整个集群内部通信的中介,也是外控控制的入口。所有的机制都是围绕apiserver来进行设计: 请求api资源: 1、认证 2、鉴权 …...
GitHub 一周热点汇总第7期(2024/01/21-01/27)
GitHub一周热点汇总第7期 (2024/01/21-01/27) ,梳理每周热门的GitHub项目,离春节越来越近了,不知道大家都买好回家的票没有,希望大家都能顺利买到票,一起来看看这周的项目吧。 #1 rustdesk 项目名称:rust…...
kotlin data clas 数据类
data class 介绍 kotlin 中 data class 是一种持有数据的特殊类 编译器自动从主构造函数中声明的所有属性导出以下成员: .equals()/.hashCode() 对 .toString() 格式是 "User(nameJohn, age42)" .componentN() 函数 按声明顺序对应于所有属性。…...
Java基础知识-异常
资料来自黑马程序员 异常 异常,就是不正常的意思。在生活中:医生说,你的身体某个部位有异常,该部位和正常相比有点不同,该部位的功能将受影响.在程序中的意思就是: 异常 :指的是程序在执行过程中,出现的非正常的情况,…...
跟着cherno手搓游戏引擎【12】渲染context和首个三角形
渲染上下文: 目的:修改WindowsWindow的结构,把glad抽离出来 WindowsWindow.h:新建m_Context #pragma once #include "YOTO/Window.h" #include <YOTO/Renderer/GraphicsContext.h> #include<GLFW/glfw3.h> #include…...
MybatisPlus二级映射和关联对象ResultMap
文章目录 一、业务背景1. 数据库表结构2. 需求 二、使用映射直接得到指定结构三、其他文件1. Mapper2. Service3. Controller 四、概念理解一级映射二级映射聚合 五、标签使用1. \<collection\> 标签2. \<association\> 标签 在我们的教程中,我们设计了…...
低代码开发业务在AIGC时代的应用
随着人工智能和图形计算能力的快速发展,低代码开发平台在AIGC(人工智能,物联网,大数据和云计算)时代中扮演着至关重要的角色。本文将介绍低代码开发业务的概念和优势,探讨其在AIGC时代的应用及其对传统软件…...
惠普1536dnf MFP报52扫描仪错误维修
如果您使用的惠普HP LaserJet 1536dnf MFP打印机可能会遇到“52扫描仪错误”的提示。这个错误可能会阻止你使用打印机的扫描功能。在这里,我将提供一些有用的解决方法来帮助大家去解决这个问题。-----吴中函 故障描述: 一台某单位正在使用的惠普HP LaserJet 1536dnf MFP黑白…...
【MIdjourney】五个特殊物体关键词
1.碳酸(Carbonate) 这一词语的本意是指包含碳(C)、氧(O)和氢(H)元素的化合物。而在MIdjourney中添加该词汇会使得生成的图片具有水滴效果且富有动态感。 2.灯丝(Filament) Filament效果可能包括更逼真的…...
2024/1/27 备战蓝桥杯 1
目录 求和 0求和 - 蓝桥云课 (lanqiao.cn) 成绩分析 0成绩分析 - 蓝桥云课 (lanqiao.cn) 合法日期 0合法日期 - 蓝桥云课 (lanqiao.cn) 时间加法 0时间加法 - 蓝桥云课 (lanqiao.cn) 扫雷 0扫雷 - 蓝桥云课 (lanqiao.cn) 大写 0大写 - 蓝桥云课 (lanqiao.cn) 标题…...
3步实现AutoHotkey脚本独立运行:Ahk2Exe编译工具完全指南
3步实现AutoHotkey脚本独立运行:Ahk2Exe编译工具完全指南 【免费下载链接】Ahk2Exe Official AutoHotkey script compiler - written itself in AutoHotkey 项目地址: https://gitcode.com/gh_mirrors/ah/Ahk2Exe 你是否厌倦了每次运行AutoHotkey脚本都需要安…...
Python try...except ImportError 语句详解
在Python编程中,ImportError 是与模块导入相关的核心异常。优雅地处理它,是编写健壮、可维护和跨平台代码的关键。try...except ImportError 结构正是实现这一目标的标准工具。本文将为你抽丝剥茧,从基础概念到高级实践,全面解析这…...
Unity游戏接入TapTap登录,从后台配置到打包上线的完整避坑指南
Unity游戏接入TapTap登录的全流程避坑指南:从配置到上线的实战经验 在独立游戏开发领域,TapTap平台凭借其庞大的用户基础和便捷的登录系统,已成为许多开发者的首选接入方案。然而,从后台配置到最终打包上线的完整流程中࿰…...
SimulinkVeriStandLabVIEW协同开发——从模型编译到交互式仪表盘部署
1. 工具链协同开发的核心价值 在电力电子和工业控制领域,快速原型开发往往需要跨越建模、实时测试和人机交互三个关键环节。Simulink、VeriStand和LabVIEW组成的工具链,就像汽车制造的流水线——Simulink是设计图纸的工程师,VeriStand是组装车…...
AI Agent执行链路的安全机制:权限控制与沙箱隔离方案
AI Agent执行链路安全深度解析:权限控制与沙箱隔离全栈落地方案 摘要/引言 你有没有遇到过这些场景:刚上线的企业内部运维Agent被恶意Prompt注入后,直接调用了删除生产库的工具;你做的数据分析Agent被诱导执行了恶意Python代码,把公司的用户隐私数据传到了境外黑客服务器…...
低温预警!固化慢、易开裂……密封胶冬季施工手册
低温预警!固化慢、易开裂……密封胶冬季施工手册 硅酮耐候密封胶主要作用是保障幕墙的气密性、水密性。其出现问题,可能会导致耐候密封失效,从而造成幕墙漏水漏气,影响幕墙的正常使用。耐候密封胶由于考虑到现场施工,几乎都是单组分硅酮密封胶产品。进入冬季,气候变化明…...
Java 大厂面试 200 题完整版含答案解析
前言本文整理了近两年从阿里、腾讯、字节、美团、京东、拼多多等大厂面试中高频出现的 200 道 Java 面试题,覆盖 Java 基础、集合、并发、JVM、Spring、MySQL、Redis、消息队列、分布式、场景设计 等核心模块,每题都附有简明扼要的答案解析,助…...
别再点‘忽略’了!开机弹出Visual C++ Runtime Library错误的终极排查指南(附Adobe软件关联排查)
Visual C Runtime Library错误:从崩溃到根治的全链路解决方案 每次开机时那个刺眼的Visual C Runtime Library错误弹窗,就像一位不请自来的访客,固执地打断你的工作节奏。对于依赖Adobe Creative Cloud或达芬奇等创意工具的专业人士来说&…...
Vircadia Native Core:开源虚拟世界服务器核心架构与部署实战
1. 项目概述:一个开源虚拟世界的“引擎心脏”如果你对构建一个属于自己的、去中心化的虚拟世界(Metaverse)感兴趣,或者你正在寻找一个能支撑起大规模、高自由度社交与协作应用的底层平台,那么Vircadia Native Core绝对…...
CircuitPython状态灯、安全模式与文件系统故障排查实战指南
1. 项目概述与核心价值 如果你正在用CircuitPython做项目,无论是物联网传感器节点、智能穿戴设备还是互动艺术装置,大概率都遇到过这样的瞬间:板子上的RGB状态灯突然开始闪烁诡异的颜色,或者电脑上那个熟悉的 CIRCUITPY U盘图标…...
