【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) 标题…...

(LeetCode 每日一题) 3442. 奇偶频次间的最大差值 I (哈希、字符串)
题目:3442. 奇偶频次间的最大差值 I 思路 :哈希,时间复杂度0(n)。 用哈希表来记录每个字符串中字符的分布情况,哈希表这里用数组即可实现。 C版本: class Solution { public:int maxDifference(string s) {int a[26]…...

MFC内存泄露
1、泄露代码示例 void X::SetApplicationBtn() {CMFCRibbonApplicationButton* pBtn GetApplicationButton();// 获取 Ribbon Bar 指针// 创建自定义按钮CCustomRibbonAppButton* pCustomButton new CCustomRibbonAppButton();pCustomButton->SetImage(IDB_BITMAP_Jdp26)…...
多场景 OkHttpClient 管理器 - Android 网络通信解决方案
下面是一个完整的 Android 实现,展示如何创建和管理多个 OkHttpClient 实例,分别用于长连接、普通 HTTP 请求和文件下载场景。 <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas…...
Python爬虫实战:研究feedparser库相关技术
1. 引言 1.1 研究背景与意义 在当今信息爆炸的时代,互联网上存在着海量的信息资源。RSS(Really Simple Syndication)作为一种标准化的信息聚合技术,被广泛用于网站内容的发布和订阅。通过 RSS,用户可以方便地获取网站更新的内容,而无需频繁访问各个网站。 然而,互联网…...

html-<abbr> 缩写或首字母缩略词
定义与作用 <abbr> 标签用于表示缩写或首字母缩略词,它可以帮助用户更好地理解缩写的含义,尤其是对于那些不熟悉该缩写的用户。 title 属性的内容提供了缩写的详细说明。当用户将鼠标悬停在缩写上时,会显示一个提示框。 示例&#x…...

【p2p、分布式,区块链笔记 MESH】Bluetooth蓝牙通信 BLE Mesh协议的拓扑结构 定向转发机制
目录 节点的功能承载层(GATT/Adv)局限性: 拓扑关系定向转发机制定向转发意义 CG 节点的功能 节点的功能由节点支持的特性和功能决定。所有节点都能够发送和接收网格消息。节点还可以选择支持一个或多个附加功能,如 Configuration …...
鸿蒙HarmonyOS 5军旗小游戏实现指南
1. 项目概述 本军旗小游戏基于鸿蒙HarmonyOS 5开发,采用DevEco Studio实现,包含完整的游戏逻辑和UI界面。 2. 项目结构 /src/main/java/com/example/militarychess/├── MainAbilitySlice.java // 主界面├── GameView.java // 游戏核…...

leetcode73-矩阵置零
leetcode 73 思路 记录 0 元素的位置:遍历整个矩阵,找出所有值为 0 的元素,并将它们的坐标记录在数组zeroPosition中置零操作:遍历记录的所有 0 元素位置,将每个位置对应的行和列的所有元素置为 0 具体步骤 初始化…...
iOS 项目怎么构建稳定性保障机制?一次系统性防错经验分享(含 KeyMob 工具应用)
崩溃、内存飙升、后台任务未释放、页面卡顿、日志丢失——稳定性问题,不一定会立刻崩,但一旦积累,就是“上线后救不回来的代价”。 稳定性保障不是某个工具的功能,而是一套贯穿开发、测试、上线全流程的“观测分析防范”机制。 …...
【学习记录】使用 Kali Linux 与 Hashcat 进行 WiFi 安全分析:合法的安全测试指南
文章目录 📌 前言🧰 一、前期准备✅ 安装 Kali Linux✅ 获取支持监听模式的无线网卡 🛠 二、使用 Kali Linux 进行 WiFi 安全测试步骤 1:插入无线网卡并确认识别步骤 2:开启监听模式步骤 3:扫描附近的 WiFi…...