当前位置: 首页 > news >正文

【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】实现简易购物车功能(附源码)

先上结果&#xff1a; 代码&#xff1a; 首先引入图片加载&#xff1a; implementation com.github.bumptech.glide:glide:4.15.1配置权限清单&#xff1a; <!-- 网络权限 --><uses-permission android:name"android.permission.INTERNET"/><uses…...

使用Excel计算--任务完成总工作日时间段

(Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu) 引言 计算任务完成时间周期&#xff0c;和计算金钱一样&#xff0c;是一个比较细致严谨的工作。 通常&#xff0c;我们可能以为&#xff0c;完成周期形如&#xff1a; 任务完成周期 任务结束时间 - 任务开始时间 但是…...

.NET高级面试指南专题一【委托和事件】

在C#中&#xff0c;委托&#xff08;Delegate&#xff09;和事件&#xff08;Event&#xff09;是两个重要的概念&#xff0c;它们通常用于实现事件驱动编程和回调机制。 委托定义&#xff1a; 委托是一个类&#xff0c;它定义了方法的类型&#xff0c;使得可以将方法当作另一个…...

基于springboot+vue的在线教育系统(前后端分离)

博主主页&#xff1a;猫头鹰源码 博主简介&#xff1a;Java领域优质创作者、CSDN博客专家、公司架构师、全网粉丝5万、专注Java技术领域和毕业设计项目实战 主要内容&#xff1a;毕业设计(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实例分割是目标检测算法的一个变种&#xff0c;主要用于识别和分割图像中的多个物体。它是在YOLOv5的基础上&#xff0c;通过添加一个实例分割模块来实现的。 在实例分割中&#xff0c;算法不仅…...

C++核心编程:类和对象 笔记

4.类和对象 C面向对象的三大特性为:封装,继承,多态C认为万事万物都皆为对象&#xff0c;对象上有其属性和行为 例如&#xff1a; 人可以作为对象&#xff0c;属性有姓名、年龄、身高、体重...,行为有走、跑、跳、说话...车可以作为对象&#xff0c;属性有轮胎、方向盘、车灯…...

机器学习实验3——支持向量机分类鸢尾花

文章目录 &#x1f9e1;&#x1f9e1;实验内容&#x1f9e1;&#x1f9e1;&#x1f9e1;&#x1f9e1;数据预处理&#x1f9e1;&#x1f9e1;代码认识数据相关性分析径向可视化各个特征之间的关系图 &#x1f9e1;&#x1f9e1;支持向量机SVM求解&#x1f9e1;&#x1f9e1;直觉…...

R语言【taxlist】——clean():移除孤立的记录

Package taxlist version 0.2.4 Description 对于 taxlist 类对象的操作可能会产生独立的条目。clean() 方法就是用来删除这样的条目&#xff0c;并恢复 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的安全机制&#xff0c;分布式集群管理工具&#xff0c;就是容器编排。安全机制的核心&#xff1a;APIserver。为整个集群内部通信的中介&#xff0c;也是外控控制的入口。所有的机制都是围绕apiserver来进行设计&#xff1a; 请求api资源&#xff1a; 1、认证 2、鉴权 …...

GitHub 一周热点汇总第7期(2024/01/21-01/27)

GitHub一周热点汇总第7期 (2024/01/21-01/27) &#xff0c;梳理每周热门的GitHub项目&#xff0c;离春节越来越近了&#xff0c;不知道大家都买好回家的票没有&#xff0c;希望大家都能顺利买到票&#xff0c;一起来看看这周的项目吧。 #1 rustdesk 项目名称&#xff1a;rust…...

kotlin data clas 数据类

data class 介绍 kotlin 中 data class 是一种持有数据的特殊类 编译器自动从主构造函数中声明的所有属性导出以下成员&#xff1a; .equals()/.hashCode() 对 .toString() 格式是 "User(nameJohn, age42)" .componentN() 函数 按声明顺序对应于所有属性。…...

Java基础知识-异常

资料来自黑马程序员 异常 异常&#xff0c;就是不正常的意思。在生活中:医生说,你的身体某个部位有异常,该部位和正常相比有点不同,该部位的功能将受影响.在程序中的意思就是&#xff1a; 异常 &#xff1a;指的是程序在执行过程中&#xff0c;出现的非正常的情况&#xff0c;…...

跟着cherno手搓游戏引擎【12】渲染context和首个三角形

渲染上下文&#xff1a; 目的&#xff1a;修改WindowsWindow的结构&#xff0c;把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\> 标签 在我们的教程中&#xff0c;我们设计了…...

低代码开发业务在AIGC时代的应用

随着人工智能和图形计算能力的快速发展&#xff0c;低代码开发平台在AIGC&#xff08;人工智能&#xff0c;物联网&#xff0c;大数据和云计算&#xff09;时代中扮演着至关重要的角色。本文将介绍低代码开发业务的概念和优势&#xff0c;探讨其在AIGC时代的应用及其对传统软件…...

惠普1536dnf MFP报52扫描仪错误维修

如果您使用的惠普HP LaserJet 1536dnf MFP打印机可能会遇到“52扫描仪错误”的提示。这个错误可能会阻止你使用打印机的扫描功能。在这里,我将提供一些有用的解决方法来帮助大家去解决这个问题。-----吴中函 故障描述: 一台某单位正在使用的惠普HP LaserJet 1536dnf MFP黑白…...

【MIdjourney】五个特殊物体关键词

1.碳酸(Carbonate) 这一词语的本意是指包含碳&#xff08;C&#xff09;、氧&#xff08;O&#xff09;和氢&#xff08;H&#xff09;元素的化合物。而在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) 标题…...

变量 varablie 声明- Rust 变量 let mut 声明与 C/C++ 变量声明对比分析

一、变量声明设计&#xff1a;let 与 mut 的哲学解析 Rust 采用 let 声明变量并通过 mut 显式标记可变性&#xff0c;这种设计体现了语言的核心哲学。以下是深度解析&#xff1a; 1.1 设计理念剖析 安全优先原则&#xff1a;默认不可变强制开发者明确声明意图 let x 5; …...

云原生核心技术 (7/12): K8s 核心概念白话解读(上):Pod 和 Deployment 究竟是什么?

大家好&#xff0c;欢迎来到《云原生核心技术》系列的第七篇&#xff01; 在上一篇&#xff0c;我们成功地使用 Minikube 或 kind 在自己的电脑上搭建起了一个迷你但功能完备的 Kubernetes 集群。现在&#xff0c;我们就像一个拥有了一块崭新数字土地的农场主&#xff0c;是时…...

Python:操作 Excel 折叠

💖亲爱的技术爱好者们,热烈欢迎来到 Kant2048 的博客!我是 Thomas Kant,很开心能在CSDN上与你们相遇~💖 本博客的精华专栏: 【自动化测试】 【测试经验】 【人工智能】 【Python】 Python 操作 Excel 系列 读取单元格数据按行写入设置行高和列宽自动调整行高和列宽水平…...

Cinnamon修改面板小工具图标

Cinnamon开始菜单-CSDN博客 设置模块都是做好的&#xff0c;比GNOME简单得多&#xff01; 在 applet.js 里增加 const Settings imports.ui.settings;this.settings new Settings.AppletSettings(this, HTYMenusonichy, instance_id); this.settings.bind(menu-icon, menu…...

【碎碎念】宝可梦 Mesh GO : 基于MESH网络的口袋妖怪 宝可梦GO游戏自组网系统

目录 游戏说明《宝可梦 Mesh GO》 —— 局域宝可梦探索Pokmon GO 类游戏核心理念应用场景Mesh 特性 宝可梦玩法融合设计游戏构想要素1. 地图探索&#xff08;基于物理空间 广播范围&#xff09;2. 野生宝可梦生成与广播3. 对战系统4. 道具与通信5. 延伸玩法 安全性设计 技术选…...

是否存在路径(FIFOBB算法)

题目描述 一个具有 n 个顶点e条边的无向图&#xff0c;该图顶点的编号依次为0到n-1且不存在顶点与自身相连的边。请使用FIFOBB算法编写程序&#xff0c;确定是否存在从顶点 source到顶点 destination的路径。 输入 第一行两个整数&#xff0c;分别表示n 和 e 的值&#xff08;1…...

深度学习习题2

1.如果增加神经网络的宽度&#xff0c;精确度会增加到一个特定阈值后&#xff0c;便开始降低。造成这一现象的可能原因是什么&#xff1f; A、即使增加卷积核的数量&#xff0c;只有少部分的核会被用作预测 B、当卷积核数量增加时&#xff0c;神经网络的预测能力会降低 C、当卷…...

HashMap中的put方法执行流程(流程图)

1 put操作整体流程 HashMap 的 put 操作是其最核心的功能之一。在 JDK 1.8 及以后版本中&#xff0c;其主要逻辑封装在 putVal 这个内部方法中。整个过程大致如下&#xff1a; 初始判断与哈希计算&#xff1a; 首先&#xff0c;putVal 方法会检查当前的 table&#xff08;也就…...

Fabric V2.5 通用溯源系统——增加图片上传与下载功能

fabric-trace项目在发布一年后,部署量已突破1000次,为支持更多场景,现新增支持图片信息上链,本文对图片上传、下载功能代码进行梳理,包含智能合约、后端、前端部分。 一、智能合约修改 为了增加图片信息上链溯源,需要对底层数据结构进行修改,在此对智能合约中的农产品数…...

基于Springboot+Vue的办公管理系统

角色&#xff1a; 管理员、员工 技术&#xff1a; 后端: SpringBoot, Vue2, MySQL, Mybatis-Plus 前端: Vue2, Element-UI, Axios, Echarts, Vue-Router 核心功能&#xff1a; 该办公管理系统是一个综合性的企业内部管理平台&#xff0c;旨在提升企业运营效率和员工管理水…...