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

黑马Mybatis

Mybatis 表现层&#xff1a;页面展示 业务层&#xff1a;逻辑处理 持久层&#xff1a;持久数据化保存 在这里插入图片描述 Mybatis快速入门 ![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/6501c2109c4442118ceb6014725e48e4.png //logback.xml <?xml ver…...

23-Oracle 23 ai 区块链表(Blockchain Table)

小伙伴有没有在金融强合规的领域中遇见&#xff0c;必须要保持数据不可变&#xff0c;管理员都无法修改和留痕的要求。比如医疗的电子病历中&#xff0c;影像检查检验结果不可篡改行的&#xff0c;药品追溯过程中数据只可插入无法删除的特性需求&#xff1b;登录日志、修改日志…...

C# SqlSugar:依赖注入与仓储模式实践

C# SqlSugar&#xff1a;依赖注入与仓储模式实践 在 C# 的应用开发中&#xff0c;数据库操作是必不可少的环节。为了让数据访问层更加简洁、高效且易于维护&#xff0c;许多开发者会选择成熟的 ORM&#xff08;对象关系映射&#xff09;框架&#xff0c;SqlSugar 就是其中备受…...

微信小程序云开发平台MySQL的连接方式

注&#xff1a;微信小程序云开发平台指的是腾讯云开发 先给结论&#xff1a;微信小程序云开发平台的MySQL&#xff0c;无法通过获取数据库连接信息的方式进行连接&#xff0c;连接只能通过云开发的SDK连接&#xff0c;具体要参考官方文档&#xff1a; 为什么&#xff1f; 因为…...

学习STC51单片机32(芯片为STC89C52RCRC)OLED显示屏2

每日一言 今天的每一份坚持&#xff0c;都是在为未来积攒底气。 案例&#xff1a;OLED显示一个A 这边观察到一个点&#xff0c;怎么雪花了就是都是乱七八糟的占满了屏幕。。 解释 &#xff1a; 如果代码里信号切换太快&#xff08;比如 SDA 刚变&#xff0c;SCL 立刻变&#…...

零基础在实践中学习网络安全-皮卡丘靶场(第九期-Unsafe Fileupload模块)(yakit方式)

本期内容并不是很难&#xff0c;相信大家会学的很愉快&#xff0c;当然对于有后端基础的朋友来说&#xff0c;本期内容更加容易了解&#xff0c;当然没有基础的也别担心&#xff0c;本期内容会详细解释有关内容 本期用到的软件&#xff1a;yakit&#xff08;因为经过之前好多期…...

PAN/FPN

import torch import torch.nn as nn import torch.nn.functional as F import mathclass LowResQueryHighResKVAttention(nn.Module):"""方案 1: 低分辨率特征 (Query) 查询高分辨率特征 (Key, Value).输出分辨率与低分辨率输入相同。"""def __…...

现有的 Redis 分布式锁库(如 Redisson)提供了哪些便利?

现有的 Redis 分布式锁库&#xff08;如 Redisson&#xff09;相比于开发者自己基于 Redis 命令&#xff08;如 SETNX, EXPIRE, DEL&#xff09;手动实现分布式锁&#xff0c;提供了巨大的便利性和健壮性。主要体现在以下几个方面&#xff1a; 原子性保证 (Atomicity)&#xff…...

pycharm 设置环境出错

pycharm 设置环境出错 pycharm 新建项目&#xff0c;设置虚拟环境&#xff0c;出错 pycharm 出错 Cannot open Local Failed to start [powershell.exe, -NoExit, -ExecutionPolicy, Bypass, -File, C:\Program Files\JetBrains\PyCharm 2024.1.3\plugins\terminal\shell-int…...

spring Security对RBAC及其ABAC的支持使用

RBAC (基于角色的访问控制) RBAC (Role-Based Access Control) 是 Spring Security 中最常用的权限模型&#xff0c;它将权限分配给角色&#xff0c;再将角色分配给用户。 RBAC 核心实现 1. 数据库设计 users roles permissions ------- ------…...