Android 仿淘宝购物车实现

本文详细介绍了如何在Android平台上实现类似淘宝购物车的功能,包括商品按店铺分类显示,全选/反选操作,商品数量变化及总价实时更新。通过设置商品和店铺的select属性,并在CheckBox状态改变时同步更新这些属性,实现购物车的交互效果。涉及到的代码包括activity_shopping_car布局文件、Java业务逻辑代码和adapter的相关实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

功能基本和淘宝购物车一样,商品按照店铺分类显示,全选,反选,选中商品数量变化,总价随之变化。效果图

思路:店铺和商品都增加一个select属性,列表的CheckBox选择或未选中状态改变同时设置店铺和商品的select属性,每次CheckBox状态改变设置select的值等于cb.isChecked()

购物车页面布局文件activity_shopping_car

<?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=".ui.activity.ShoppingCarActivity">

    <com.hjq.bar.TitleBar
        android:id="@+id/titleBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:rightTitle="管理"
        app:title="购物车" />

    <com.qiyetec.flyingsnail.widget.HintLayout
        android:id="@+id/hintLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <com.scwang.smartrefresh.layout.SmartRefreshLayout
            android:id="@+id/smartRefreshLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.scwang.smartrefresh.layout.header.ClassicsHeader
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rv"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </com.scwang.smartrefresh.layout.SmartRefreshLayout>
    </com.qiyetec.flyingsnail.widget.HintLayout>

    <LinearLayout
        android:id="@+id/ll_bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/ali_auth_space_10"
        android:background="#fff"
        android:gravity="center_vertical"
        android:padding="15dp">

        <CheckBox
            android:id="@+id/cb_all"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/selector_checkbox_green"
            android:text="全选" />

        <View
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_weight="1" />

        <LinearLayout
            android:id="@+id/ll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="合计:"
                android:textColor="#333"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/tv_total_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="¥0.00"
                android:textColor="#DA3527"
                android:textSize="24sp" />

            <TextView
                android:id="@+id/tv_buy"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:background="@drawable/shape_red10"
                android:paddingLeft="15dp"
                android:paddingTop="5dp"
                android:paddingRight="15dp"
                android:paddingBottom="5dp"
                android:text="结算"
                android:textColor="#fff"
                android:textSize="18sp" />
        </LinearLayout>

        <TextView
            android:id="@+id/tv_del"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/shape_stroke_red15"
            android:paddingLeft="15dp"
            android:paddingTop="5dp"
            android:paddingRight="15
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值