Android购物车的实现,仿淘宝天猫京东等APP。处理RecyclerView或listview中的选中事件;

Stella981
• 阅读 628

很久之前的代码了,拉出来晾晾!

Android购物车的实现,仿淘宝天猫京东等APP。处理RecyclerView或listview中的选中事件;

购物车大致思路:

分为:商品、店铺、全选;

商品全部选中后--店铺自动选中;商品未全部选中(若有一个商品未选中)--店铺不选中。

店铺全部选中后--全选自动选中;店铺未全部选中(若有一个店铺未选中)--全选不选中。

选中店铺 -- 店铺中的商品全部选中。

全选选中 -- 店铺全部选中。(全部选中);

我是使用RecyclerView来写的,适配器用的是BaseRecyclerViewAdapterHelper;当然ListView也能实现,思路代码什么的都一样。

好,直接上代码:

核心代码:

package com.example.admin.ccb.fragment;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.admin.ccb.R;
import com.example.admin.ccb.activity.GoodsDatailsActivity;
import com.example.admin.ccb.activity.ShopHomeActivity;
import com.example.admin.ccb.base.BaseFragment;
import com.example.admin.ccb.base.ShopPingCartBean;
import com.example.admin.ccb.utils.ResCcb;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Ccb simple {@link Fragment} subclass.
 */
public class SpcFragment extends BaseFragment {


    private SpcAdapter spcAp;
    private ShopPingCartBean spcs;
    private CheckBox cbAll;
    private TextView jiesuan;

    @Override
    protected View initContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_spc, container, false);
    }
    private RecyclerView rvShop;
    @Override
    public void initView(View view) {
      rvShop = view.findViewById(R.id.rvShop);
      cbAll = view.findViewById(R.id.spc_cb_all);
        jiesuan = view.findViewById(R.id.jiesuan);
      rvShop.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL, false));
        spcAp = new SpcAdapter(R.layout.item_spc_shop);
      rvShop.setAdapter(spcAp);
    }
     
    @Override
    public void loadData() {
        Random random = new Random();
        spcs = new ShopPingCartBean();
        spcs.shopList = new ArrayList<>();
        for (int i = 0; i < ResCcb.getMenus().size(); i++) {   //添加店铺
            ShopPingCartBean.ShopBean shop = new ShopPingCartBean.ShopBean();
            shop.shopName = ResCcb.getMenus().get(i);
            int jj = random.nextInt(5)+1;
            shop.carList = new ArrayList<>();
            for (int j = 0; j < jj; j++) {  //添加商品
                ShopPingCartBean.ShopBean.CarListBean goods = new ShopPingCartBean.ShopBean.CarListBean();
                goods.title = ResCcb.getspcGoodsImages().get(random.nextInt(ResCcb.getspcGoodsImages().size()-1));
                goods.icon = ResCcb.getSpcImages().get(random.nextInt(ResCcb.getSpcImages().size()-1));
                shop.carList.add(goods);
            }
            spcs.shopList.add(shop);
        }
        spcAp.setNewData(spcs.shopList);
    }

    @Override
    public void initListener() {
        cbAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean is = cbAll.isChecked();
                for (int i = 0; i < spcs.shopList.size(); i++) {
                    spcs.shopList.get(i).isSelectShop = is;
                    for (int j = 0; j < spcs.shopList.get(i).carList.size(); j++) {
                        spcs.shopList.get(i).carList.get(j).isSelectGoods = is;
                    }
                }
                spcAp.notifyDataSetChanged();
            }
        });
    }

    /**
     * 适配器
     */
    public class SpcAdapter extends BaseQuickAdapter<ShopPingCartBean.ShopBean,BaseViewHolder> {
        public SpcAdapter(int layoutResId) {
            super(layoutResId);
        }

        @Override
        protected void convert(final BaseViewHolder helper, ShopPingCartBean.ShopBean item) {
            helper.setText(R.id.tvShop,item.shopName)
                    .setOnClickListener(R.id.tvShop, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            mContext.startActivity(new Intent(mContext, ShopHomeActivity.class));
                        }
                    });
            final CheckBox cbShop = helper.getView(R.id.spc_cb_shops);
            cbShop.setChecked(item.isSelectShop);
            cbShop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    spcs.shopList.get(helper.getAdapterPosition()).isSelectShop = cbShop.isChecked();
                    for (int i = 0; i < spcs.shopList.get(helper.getAdapterPosition()).carList.size(); i++) {  //改变这个店铺中所有商品的数据
                        spcs.shopList.get(helper.getAdapterPosition()).carList.get(i).isSelectGoods = cbShop.isChecked();
                    }
                     //遍历有所的店铺集合,看是否全部选中,若选中则改变全选标记
                    List<Boolean> shoplist = new ArrayList<>();
                    for (int i = 0; i < spcs.shopList.size(); i++) {
                        shoplist.add(spcs.shopList.get(i).isSelectShop);
                    }
                    if (shoplist.contains(false)){
                        cbAll.setChecked(false);
                    }else{
                        cbAll.setChecked(true);
                    }
                    notifyDataSetChanged();
                }
            });
            RecyclerView rvGoods = helper.getView(R.id.rvGoods);
            rvGoods.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false));
            GoodsAdapter goodsAp = new GoodsAdapter(R.layout.item_spc_goods, helper.getAdapterPosition());
            rvGoods.setAdapter(goodsAp);
            goodsAp.setNewData(item.carList);

        }
    }

    class GoodsAdapter extends BaseQuickAdapter<ShopPingCartBean.ShopBean.CarListBean,BaseViewHolder>{
        private int positionShop;
        public GoodsAdapter(int layoutResId,int positionShop) {
            super(layoutResId);
            this.positionShop = positionShop;
        }

        @Override
        protected void convert(final BaseViewHolder helper, ShopPingCartBean.ShopBean.CarListBean item) {
            helper.setText(R.id.spc_tv_shop_name_msg,item.title)
                   .setOnClickListener(R.id.rl, new View.OnClickListener() {
                       @Override
                       public void onClick(View view) {
                           mContext.startActivity(new Intent(mContext, GoodsDatailsActivity.class));
                       }
                   });
            helper.setImageResource(R.id.spc_iv_page,item.icon);
            final CheckBox cbGoods = helper.getView(R.id.spc_cb_goods);
            cbGoods.setChecked(item.isSelectGoods);
            cbGoods.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    spcs.shopList.get(positionShop).carList.get(helper.getAdapterPosition()).isSelectGoods = cbGoods.isChecked();
                    //改变这个商品的选中状态后,遍历看是否全部选中,若全部选中则改变店铺的选中状态
                    List<Boolean> goodslist = new ArrayList<>();
                    for (int i = 0; i < spcs.shopList.get(positionShop).carList.size(); i++) {
                        goodslist.add(spcs.shopList.get(positionShop).carList.get(i).isSelectGoods);
                    }
                    if (goodslist.contains(false)){
                        spcs.shopList.get(positionShop).isSelectShop = false;
                    }else{
                        spcs.shopList.get(positionShop).isSelectShop = true;
                    }
                    //改变这个店铺的选中状态后,遍历看是否全部选中,若全部选中则改变全选的选中状态
                    List<Boolean> shoplist = new ArrayList<>();
                    for (int i = 0; i < spcs.shopList.size(); i++) {
                        shoplist.add(spcs.shopList.get(i).isSelectShop);
                    }
                    if (shoplist.contains(false)){
                        cbAll.setChecked(false);
                    }else{
                        cbAll.setChecked(true);
                    }
                    spcAp.notifyDataSetChanged();
                    notifyDataSetChanged();
                }
            });
        }
    }
}

主界面布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.admin.ccb.fragment.SpcFragment">

    <RelativeLayout
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp44">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="16dp"
            android:textColor="#212121"
            android:gravity="center"
            android:text="购物车" />
        <View
            android:layout_alignParentBottom="true"
            style="@style/Line_e0e0e0_Horizontal"/>
    </RelativeLayout>
    <View
        android:layout_below="@id/title"
        style="@style/Line_e0e0e0_Horizontal"/>
    <RelativeLayout
        android:id="@+id/layout_bottom"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:background="@color/colorWhite"
        android:layout_height="45dp">
       <View
           android:layout_width="match_parent"
           android:layout_height="1dp"
           android:background="@color/defaultDividerLine"
           />
        <CheckBox
            android:id="@+id/spc_cb_all"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:paddingLeft="@dimen/side_distance"
            android:paddingRight="@dimen/side_distance"
            style="@style/spc_checkbox_style"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="全选"
            android:layout_toRightOf="@id/spc_cb_all"
            android:layout_centerVertical="true"
            android:textColor="@color/color_666666"
            android:textSize="13dp"
            />

        <TextView
            android:id="@+id/jiesuan"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:text="结算"
            android:gravity="center"
            android:textSize="@dimen/dp16"
            android:background="@color/mainColor"
            android:textColor="@color/colorWhite"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="¥:1016.69"
            android:layout_toLeftOf="@id/jiesuan"
            android:layout_marginRight="@dimen/dp5"
            android:layout_centerVertical="true"
            android:layout_marginLeft="@dimen/dp4"
            android:textColor="@color/mainColor"
            android:textSize="15dp"
            />

    </RelativeLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvShop"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/title"
        android:layout_above="@id/layout_bottom"
        ></android.support.v7.widget.RecyclerView>

</RelativeLayout>

外层RecyclerView(店铺)的item:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#ffffff"
    android:descendantFocusability="blocksDescendants"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        >
        <CheckBox
            android:id="@+id/spc_cb_shops"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:paddingLeft="@dimen/side_distance"
            android:paddingRight="@dimen/side_distance"
            style="@style/spc_checkbox_style"
            />
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@mipmap/orderfrom_shangdian"
            />
        <TextView
            android:id="@+id/tvShop"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:paddingRight="@dimen/side_distance"
            android:gravity="center_vertical"
            android:text="ShopName"
            android:textColor="#666666"
            android:textSize="15dp"
            />
    </LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/defaultBackground"
    >
    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvGoods"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
    </android.support.v7.widget.RecyclerView>
</LinearLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="44dp"
    android:gravity="center_vertical"
    >
        <TextView
            android:id="@+id/spc_tv_zong_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16dp"
            android:textColor="@color/mainColor"
            android:layout_alignParentRight="true"
            android:text="100.00"
            android:layout_marginRight="@dimen/side_distance"
            />
    <TextView
        android:id="@+id/spc_tv_price_money"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="总价:"
        android:textColor="#666666"
        android:textSize="15dp"
        android:layout_toLeftOf="@id/spc_tv_zong_price"
        android:layout_marginRight="10dp"
        />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_toLeftOf="@id/spc_tv_price_money"
        android:layout_marginRight="15dp"
        >
        <TextView
            android:id="@+id/spc_tv_shop_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#666666"
            android:textSize="15dp"
            android:text="共2件商品"
            />
    </LinearLayout>
</RelativeLayout>
    <View
       style="@style/Line_e0e0e0_Horizontal"/>
</LinearLayout>

内层商品Item;

<?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="wrap_content"
    android:background="@color/defaultBackground"
    android:descendantFocusability="blocksDescendants"
    >

    <RelativeLayout
        android:id="@+id/rl"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:paddingTop="5dp">
        <View
            android:layout_alignParentBottom="true"
            style="@style/Line_e0e0e0_Horizontal"/>
        <CheckBox
            android:id="@+id/spc_cb_goods"
            style="@style/spc_checkbox_style"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:paddingLeft="@dimen/side_distance"
            android:paddingRight="@dimen/side_distance" />

        <ImageView
            android:id="@+id/spc_iv_page"
            android:layout_width="85dp"
            android:layout_height="85dp"
            android:layout_toRightOf="@id/spc_cb_goods"
            android:layout_alignParentBottom="true"
            android:src="@mipmap/default_icon" />

            <TextView
                android:id="@+id/spc_tv_shop_name_msg"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="@dimen/side_distance"
                android:layout_marginTop="8dp"
                android:layout_toRightOf="@id/spc_iv_page"
                android:lines="2"
                android:text="title"
                android:lineSpacingExtra="1dp"
                android:textColor="@color/defaultTextview"
                android:textSize="13dp" />

            <TextView
                android:id="@+id/spc_tv_pinlei"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="已选:28码"
                android:layout_below="@id/spc_tv_shop_name_msg"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="3dp"
                android:layout_toRightOf="@id/spc_iv_page"
                android:textColor="@color/defaultTextview"
                android:textSize="11dp"
                android:visibility="visible" />

                <TextView
                    android:id="@+id/spc_tv_zong_price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="¥:109.89"
                    android:layout_below="@id/spc_tv_pinlei"
                    android:layout_marginLeft="10dp"
                    android:layout_toRightOf="@id/spc_iv_page"
                    android:layout_marginTop="@dimen/dp4"
                    android:textColor="@color/mainColor"
                    android:textSize="16dp" />


        <Button
                android:id="@+id/spc_btn_comm_count_jian"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:layout_alignTop="@+id/spc_tv_pinlei"
                android:layout_toLeftOf="@+id/spc_et_comm_count"
                android:layout_toStartOf="@+id/spc_et_comm_count"
                android:background="#ffffff"
                android:text="一"
                android:textColor="@color/defaultTextview"
                android:textSize="16dp" />

        <TextView
                android:id="@+id/spc_et_comm_count"
                android:layout_width="44dp"
                android:layout_height="32dp"
                android:layout_alignTop="@+id/spc_tv_pinlei"
                android:layout_toLeftOf="@+id/spc_btn_comm_count_jia"
                android:layout_toStartOf="@+id/spc_btn_comm_count_jia"
                android:background="#ffffff"
                android:gravity="center"
                android:text="0"
                android:textColor="@color/defaultTextview"
                android:textSize="16dp" />

        <Button
                android:id="@+id/spc_btn_comm_count_jia"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:layout_alignParentRight="true"
                android:layout_alignTop="@+id/spc_tv_pinlei"
                android:layout_marginRight="@dimen/side_distance"
                android:background="#ffffff"
                android:text="+"
                android:textColor="@color/defaultTextview"
                android:textSize="16dp"
                android:layout_alignRight="@+id/spc_tv_shop_name_msg"
                android:layout_alignEnd="@+id/spc_tv_shop_name_msg" />

    </RelativeLayout>


</RelativeLayout>

好了,打完收工;其中ResCcb是我的制造的假数据源;代码我已上传Github;

完整代码:https://github.com/CuiChenbo/CcMall

点赞
收藏
评论区
推荐文章
Stella981 Stella981
2年前
Android自定义Dialog多选对话框(Dialog+Listview+CheckBox)
先放效果截图项目中需要有个Dialog全选对话框,点击全选全部选中,取消全选全部取消。下午查了些资料,重写了一下Dialog对话框。把代码放出来。!这里写图片描述(https://oscimg.oschina.net/oscnet/0231bb305ff838c957875702a8373cf7c26.gif)publicclassM
Stella981 Stella981
2年前
Android ListView长按事件弹出菜单并获取选中的item
看了很多listview的长按事件,但几乎都是只给出弹出菜单的代码,没有给出选中的某个项的代码,我贴个全的吧,免得摸索麻烦思路就是listview在父窗口先注册一个长按弹出菜单registerReceiver》设置一个长按的listener,保存好选中item数据》onCreateContextMenu添加菜单》onContextItemSel
Stella981 Stella981
2年前
ReactNative state更新,视图不更新的问题
开发中遇到这样的问题,我更新了state一个数组的某个元素的选中状态,打印出的数据也显示修改正确了,但是界面却没更新。例如下图点击某项修改选中状态。!(https://oscimg.oschina.net/oscnet/c3291a62b5f638d1e35dd7a719ade39f226.png)代码中之前是这样写的,结果界面没有更新。
淘宝天猫商品详情接口代码展示(商品销量接口,商品列表接口,商品视频接口,商品优惠券接口)
淘宝商品详情接口,淘宝商品销量接口,淘宝商品列表接口,淘宝商品视频接口,淘宝商品优惠券接口,天猫商品详情接口,天猫商品销量接口,天猫商品列表接口,天猫商品视频接口,天猫商品优惠券接口,淘宝api接口,天猫API接口
lazada选品:lazada商品详情数据接口采集代码展示
lazada商品详情接口,lazada商品列表接口,lazada商品评论接口,lazada店铺商品接口,lazada商品API接口,lazada商品数据接口,lazada商品优惠券接口,lazada商品属性接口,lazada商品sku信息接口,lazada整店商品接口,lazada店铺商品接口,lazada商品描述接口,lazada评论内容接口,lazada商品销量接口
shopee商品详情接口,店铺商品接口,商品评论接口代码封装教程
shopee商品详情接口,shopee商品列表接口,shopee商品数据接口,shopee店铺商品接口,关键词搜索shopee商品接口,关键词搜索shopee商品列表接口,shopee商品API接口,shopee商品详情属性接口,shopee详情sku数据,shopee店铺详情接口,shopee规格数据接口,shopee商品销量接口,shopee商品sku接口,shopee尺寸接口,shopee重量接口
如何批量复制淘宝商品上传上架到虾皮 shopee,淘宝商品采集上架虾皮 (轻松学会宝贝复制技巧)
今天,入驻虾皮的商家越来越多,且很多的shopee店主在国内都开了淘宝店,当shopee店铺入驻成功后,想把淘宝店铺的商品搬到shopee,怎么搬呐?方法/步骤方法1:用软件上架整理你想要采集的店铺首页链接为TXT文本格式,存放在桌面。2.打开登录软件,进
京东云开发者 京东云开发者
3个月前
千万级数据深分页查询SQL性能优化实践
一、系统介绍和问题描述如何在Mysql中实现上亿数据的遍历查询?先来介绍一下系统主角:关注系统,主要是维护京东用户和业务对象之前的关注关系;并对外提供各种关系查询,比如查询用户的关注商品或店铺列表,查询用户是否关注了某个商品或店铺等。但是最近接到了一个新需
淘宝天猫店铺所有商品接口(整店商品采集接口)代码展示
淘宝店铺所有商品接口,淘宝整店商品接口,天猫店铺所有商品接口,天猫店铺商品接口,淘宝商品详情接口,天猫商品详情接口,淘宝店铺详情接口,天猫店铺详情接口,淘宝店铺详情接口,天猫店铺详情接口
天猫APP商品详情接口(商品销量接口,商品优惠券接口,商品价格接口)代码展示
天猫商品详情接口,天猫商品销量接口,天猫商品优惠券接口,天猫商品价格接口,天猫商品sku属性查询接口,天猫商品sku信息接口,淘宝商品详情接口,淘宝商品销量接口,淘宝商品优惠券接口,淘宝商品列表接口,天猫商品列表接口