当前位置: 首页 > 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) 标题…...

wordpress后台更新后 前端没变化的解决方法

使用siteground主机的wordpress网站&#xff0c;会出现更新了网站内容和修改了php模板文件、js文件、css文件、图片文件后&#xff0c;网站没有变化的情况。 不熟悉siteground主机的新手&#xff0c;遇到这个问题&#xff0c;就很抓狂&#xff0c;明明是哪都没操作错误&#x…...

C++实现分布式网络通信框架RPC(3)--rpc调用端

目录 一、前言 二、UserServiceRpc_Stub 三、 CallMethod方法的重写 头文件 实现 四、rpc调用端的调用 实现 五、 google::protobuf::RpcController *controller 头文件 实现 六、总结 一、前言 在前边的文章中&#xff0c;我们已经大致实现了rpc服务端的各项功能代…...

Swift 协议扩展精进之路:解决 CoreData 托管实体子类的类型不匹配问题(下)

概述 在 Swift 开发语言中&#xff0c;各位秃头小码农们可以充分利用语法本身所带来的便利去劈荆斩棘。我们还可以恣意利用泛型、协议关联类型和协议扩展来进一步简化和优化我们复杂的代码需求。 不过&#xff0c;在涉及到多个子类派生于基类进行多态模拟的场景下&#xff0c;…...

无法与IP建立连接,未能下载VSCode服务器

如题&#xff0c;在远程连接服务器的时候突然遇到了这个提示。 查阅了一圈&#xff0c;发现是VSCode版本自动更新惹的祸&#xff01;&#xff01;&#xff01; 在VSCode的帮助->关于这里发现前几天VSCode自动更新了&#xff0c;我的版本号变成了1.100.3 才导致了远程连接出…...

连锁超市冷库节能解决方案:如何实现超市降本增效

在连锁超市冷库运营中&#xff0c;高能耗、设备损耗快、人工管理低效等问题长期困扰企业。御控冷库节能解决方案通过智能控制化霜、按需化霜、实时监控、故障诊断、自动预警、远程控制开关六大核心技术&#xff0c;实现年省电费15%-60%&#xff0c;且不改动原有装备、安装快捷、…...

P3 QT项目----记事本(3.8)

3.8 记事本项目总结 项目源码 1.main.cpp #include "widget.h" #include <QApplication> int main(int argc, char *argv[]) {QApplication a(argc, argv);Widget w;w.show();return a.exec(); } 2.widget.cpp #include "widget.h" #include &q…...

Mac软件卸载指南,简单易懂!

刚和Adobe分手&#xff0c;它却总在Library里给你写"回忆录"&#xff1f;卸载的Final Cut Pro像电子幽灵般阴魂不散&#xff1f;总是会有残留文件&#xff0c;别慌&#xff01;这份Mac软件卸载指南&#xff0c;将用最硬核的方式教你"数字分手术"&#xff0…...

微服务商城-商品微服务

数据表 CREATE TABLE product (id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 商品id,cateid smallint(6) UNSIGNED NOT NULL DEFAULT 0 COMMENT 类别Id,name varchar(100) NOT NULL DEFAULT COMMENT 商品名称,subtitle varchar(200) NOT NULL DEFAULT COMMENT 商…...

【配置 YOLOX 用于按目录分类的图片数据集】

现在的图标点选越来越多&#xff0c;如何一步解决&#xff0c;采用 YOLOX 目标检测模式则可以轻松解决 要在 YOLOX 中使用按目录分类的图片数据集&#xff08;每个目录代表一个类别&#xff0c;目录下是该类别的所有图片&#xff09;&#xff0c;你需要进行以下配置步骤&#x…...

JUC笔记(上)-复习 涉及死锁 volatile synchronized CAS 原子操作

一、上下文切换 即使单核CPU也可以进行多线程执行代码&#xff0c;CPU会给每个线程分配CPU时间片来实现这个机制。时间片非常短&#xff0c;所以CPU会不断地切换线程执行&#xff0c;从而让我们感觉多个线程是同时执行的。时间片一般是十几毫秒(ms)。通过时间片分配算法执行。…...