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

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

购物车大致思路:

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

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

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

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

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

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

好,直接上代码:

核心代码:

1package com.example.admin.ccb.fragment; 2 3 4import android.content.Intent; 5import android.graphics.Color; 6import android.os.Bundle; 7import android.support.v4.app.Fragment; 8import android.support.v7.widget.LinearLayoutManager; 9import android.support.v7.widget.RecyclerView; 10import android.view.LayoutInflater; 11import android.view.View; 12import android.view.ViewGroup; 13import android.widget.CheckBox; 14import android.widget.TextView; 15 16import com.bigkoo.pickerview.OptionsPickerView; 17import com.chad.library.adapter.base.BaseQuickAdapter; 18import com.chad.library.adapter.base.BaseViewHolder; 19import com.example.admin.ccb.R; 20import com.example.admin.ccb.activity.GoodsDatailsActivity; 21import com.example.admin.ccb.activity.ShopHomeActivity; 22import com.example.admin.ccb.base.BaseFragment; 23import com.example.admin.ccb.base.ShopPingCartBean; 24import com.example.admin.ccb.utils.ResCcb; 25 26import java.util.ArrayList; 27import java.util.List; 28import java.util.Random; 29 30/** 31 * Ccb simple {@link Fragment} subclass. 32 */ 33public class SpcFragment extends BaseFragment { 34 35 36 private SpcAdapter spcAp; 37 private ShopPingCartBean spcs; 38 private CheckBox cbAll; 39 private TextView jiesuan; 40 41 @Override 42 protected View initContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 43 return inflater.inflate(R.layout.fragment_spc, container, false); 44 } 45 private RecyclerView rvShop; 46 @Override 47 public void initView(View view) { 48 rvShop = view.findViewById(R.id.rvShop); 49 cbAll = view.findViewById(R.id.spc_cb_all); 50 jiesuan = view.findViewById(R.id.jiesuan); 51 rvShop.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL, false)); 52 spcAp = new SpcAdapter(R.layout.item_spc_shop); 53 rvShop.setAdapter(spcAp); 54 } 55 56 @Override 57 public void loadData() { 58 Random random = new Random(); 59 spcs = new ShopPingCartBean(); 60 spcs.shopList = new ArrayList<>(); 61 for (int i = 0; i < ResCcb.getMenus().size(); i++) { //添加店铺 62 ShopPingCartBean.ShopBean shop = new ShopPingCartBean.ShopBean(); 63 shop.shopName = ResCcb.getMenus().get(i); 64 int jj = random.nextInt(5)+1; 65 shop.carList = new ArrayList<>(); 66 for (int j = 0; j < jj; j++) { //添加商品 67 ShopPingCartBean.ShopBean.CarListBean goods = new ShopPingCartBean.ShopBean.CarListBean(); 68 goods.title = ResCcb.getspcGoodsImages().get(random.nextInt(ResCcb.getspcGoodsImages().size()-1)); 69 goods.icon = ResCcb.getSpcImages().get(random.nextInt(ResCcb.getSpcImages().size()-1)); 70 shop.carList.add(goods); 71 } 72 spcs.shopList.add(shop); 73 } 74 spcAp.setNewData(spcs.shopList); 75 } 76 77 @Override 78 public void initListener() { 79 cbAll.setOnClickListener(new View.OnClickListener() { 80 @Override 81 public void onClick(View view) { 82 boolean is = cbAll.isChecked(); 83 for (int i = 0; i < spcs.shopList.size(); i++) { 84 spcs.shopList.get(i).isSelectShop = is; 85 for (int j = 0; j < spcs.shopList.get(i).carList.size(); j++) { 86 spcs.shopList.get(i).carList.get(j).isSelectGoods = is; 87 } 88 } 89 spcAp.notifyDataSetChanged(); 90 } 91 }); 92 } 93 94 /** 95 * 适配器 96 */ 97 public class SpcAdapter extends BaseQuickAdapter<ShopPingCartBean.ShopBean,BaseViewHolder> { 98 public SpcAdapter(int layoutResId) { 99 super(layoutResId); 100 } 101 102 @Override 103 protected void convert(final BaseViewHolder helper, ShopPingCartBean.ShopBean item) { 104 helper.setText(R.id.tvShop,item.shopName) 105 .setOnClickListener(R.id.tvShop, new View.OnClickListener() { 106 @Override 107 public void onClick(View view) { 108 mContext.startActivity(new Intent(mContext, ShopHomeActivity.class)); 109 } 110 }); 111 final CheckBox cbShop = helper.getView(R.id.spc_cb_shops); 112 cbShop.setChecked(item.isSelectShop); 113 cbShop.setOnClickListener(new View.OnClickListener() { 114 @Override 115 public void onClick(View view) { 116 spcs.shopList.get(helper.getAdapterPosition()).isSelectShop = cbShop.isChecked(); 117 for (int i = 0; i < spcs.shopList.get(helper.getAdapterPosition()).carList.size(); i++) { //改变这个店铺中所有商品的数据 118 spcs.shopList.get(helper.getAdapterPosition()).carList.get(i).isSelectGoods = cbShop.isChecked(); 119 } 120 //遍历有所的店铺集合,看是否全部选中,若选中则改变全选标记 121 List<Boolean> shoplist = new ArrayList<>(); 122 for (int i = 0; i < spcs.shopList.size(); i++) { 123 shoplist.add(spcs.shopList.get(i).isSelectShop); 124 } 125 if (shoplist.contains(false)){ 126 cbAll.setChecked(false); 127 }else{ 128 cbAll.setChecked(true); 129 } 130 notifyDataSetChanged(); 131 } 132 }); 133 RecyclerView rvGoods = helper.getView(R.id.rvGoods); 134 rvGoods.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false)); 135 GoodsAdapter goodsAp = new GoodsAdapter(R.layout.item_spc_goods, helper.getAdapterPosition()); 136 rvGoods.setAdapter(goodsAp); 137 goodsAp.setNewData(item.carList); 138 139 } 140 } 141 142 class GoodsAdapter extends BaseQuickAdapter<ShopPingCartBean.ShopBean.CarListBean,BaseViewHolder>{ 143 private int positionShop; 144 public GoodsAdapter(int layoutResId,int positionShop) { 145 super(layoutResId); 146 this.positionShop = positionShop; 147 } 148 149 @Override 150 protected void convert(final BaseViewHolder helper, ShopPingCartBean.ShopBean.CarListBean item) { 151 helper.setText(R.id.spc_tv_shop_name_msg,item.title) 152 .setOnClickListener(R.id.rl, new View.OnClickListener() { 153 @Override 154 public void onClick(View view) { 155 mContext.startActivity(new Intent(mContext, GoodsDatailsActivity.class)); 156 } 157 }); 158 helper.setImageResource(R.id.spc_iv_page,item.icon); 159 final CheckBox cbGoods = helper.getView(R.id.spc_cb_goods); 160 cbGoods.setChecked(item.isSelectGoods); 161 cbGoods.setOnClickListener(new View.OnClickListener() { 162 @Override 163 public void onClick(View view) { 164 spcs.shopList.get(positionShop).carList.get(helper.getAdapterPosition()).isSelectGoods = cbGoods.isChecked(); 165 //改变这个商品的选中状态后,遍历看是否全部选中,若全部选中则改变店铺的选中状态 166 List<Boolean> goodslist = new ArrayList<>(); 167 for (int i = 0; i < spcs.shopList.get(positionShop).carList.size(); i++) { 168 goodslist.add(spcs.shopList.get(positionShop).carList.get(i).isSelectGoods); 169 } 170 if (goodslist.contains(false)){ 171 spcs.shopList.get(positionShop).isSelectShop = false; 172 }else{ 173 spcs.shopList.get(positionShop).isSelectShop = true; 174 } 175 //改变这个店铺的选中状态后,遍历看是否全部选中,若全部选中则改变全选的选中状态 176 List<Boolean> shoplist = new ArrayList<>(); 177 for (int i = 0; i < spcs.shopList.size(); i++) { 178 shoplist.add(spcs.shopList.get(i).isSelectShop); 179 } 180 if (shoplist.contains(false)){ 181 cbAll.setChecked(false); 182 }else{ 183 cbAll.setChecked(true); 184 } 185 spcAp.notifyDataSetChanged(); 186 notifyDataSetChanged(); 187 } 188 }); 189 } 190 } 191}

主界面布局文件:

1<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 tools:context="com.example.admin.ccb.fragment.SpcFragment"> 6 7 <RelativeLayout 8 android:id="@+id/title" 9 android:layout_width="match_parent" 10 android:layout_height="@dimen/dp44"> 11 <TextView 12 android:layout_width="match_parent" 13 android:layout_height="match_parent" 14 android:textSize="16dp" 15 android:textColor="#212121" 16 android:gravity="center" 17 android:text="购物车" /> 18 <View 19 android:layout_alignParentBottom="true" 20 style="@style/Line_e0e0e0_Horizontal"/> 21 </RelativeLayout> 22 <View 23 android:layout_below="@id/title" 24 style="@style/Line_e0e0e0_Horizontal"/> 25 <RelativeLayout 26 android:id="@+id/layout_bottom" 27 android:layout_alignParentBottom="true" 28 android:layout_width="match_parent" 29 android:background="@color/colorWhite" 30 android:layout_height="45dp"> 31 <View 32 android:layout_width="match_parent" 33 android:layout_height="1dp" 34 android:background="@color/defaultDividerLine" 35 /> 36 <CheckBox 37 android:id="@+id/spc_cb_all" 38 android:layout_width="wrap_content" 39 android:layout_height="match_parent" 40 android:paddingLeft="@dimen/side_distance" 41 android:paddingRight="@dimen/side_distance" 42 style="@style/spc_checkbox_style" 43 /> 44 <TextView 45 android:layout_width="wrap_content" 46 android:layout_height="wrap_content" 47 android:text="全选" 48 android:layout_toRightOf="@id/spc_cb_all" 49 android:layout_centerVertical="true" 50 android:textColor="@color/color_666666" 51 android:textSize="13dp" 52 /> 53 54 <TextView 55 android:id="@+id/jiesuan" 56 android:layout_width="100dp" 57 android:layout_height="match_parent" 58 android:text="结算" 59 android:gravity="center" 60 android:textSize="@dimen/dp16" 61 android:background="@color/mainColor" 62 android:textColor="@color/colorWhite" 63 android:layout_centerVertical="true" 64 android:layout_alignParentRight="true" 65 /> 66 <TextView 67 android:layout_width="wrap_content" 68 android:layout_height="wrap_content" 69 android:text="¥:1016.69" 70 android:layout_toLeftOf="@id/jiesuan" 71 android:layout_marginRight="@dimen/dp5" 72 android:layout_centerVertical="true" 73 android:layout_marginLeft="@dimen/dp4" 74 android:textColor="@color/mainColor" 75 android:textSize="15dp" 76 /> 77 78 </RelativeLayout> 79 80 <android.support.v7.widget.RecyclerView 81 android:id="@+id/rvShop" 82 android:layout_width="match_parent" 83 android:layout_height="match_parent" 84 android:layout_below="@id/title" 85 android:layout_above="@id/layout_bottom" 86 ></android.support.v7.widget.RecyclerView> 87 88</RelativeLayout>

外层RecyclerView(店铺)的item:

1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="wrap_content" 5 android:orientation="vertical" 6 android:background="#ffffff" 7 android:descendantFocusability="blocksDescendants" 8 > 9 10 <LinearLayout 11 android:layout_width="match_parent" 12 android:layout_height="44dp" 13 android:orientation="horizontal" 14 android:gravity="center_vertical" 15 > 16 <CheckBox 17 android:id="@+id/spc_cb_shops" 18 android:layout_width="wrap_content" 19 android:layout_height="match_parent" 20 android:paddingLeft="@dimen/side_distance" 21 android:paddingRight="@dimen/side_distance" 22 style="@style/spc_checkbox_style" 23 /> 24 <ImageView 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 android:background="@mipmap/orderfrom_shangdian" 28 /> 29 <TextView 30 android:id="@+id/tvShop" 31 android:layout_width="match_parent" 32 android:layout_height="match_parent" 33 android:layout_marginLeft="5dp" 34 android:paddingRight="@dimen/side_distance" 35 android:gravity="center_vertical" 36 android:text="ShopName" 37 android:textColor="#666666" 38 android:textSize="15dp" 39 /> 40 </LinearLayout> 41<LinearLayout 42 android:layout_width="match_parent" 43 android:layout_height="wrap_content" 44 android:background="@color/defaultBackground" 45 > 46 <android.support.v7.widget.RecyclerView 47 android:id="@+id/rvGoods" 48 android:layout_width="match_parent" 49 android:layout_height="wrap_content" 50 > 51 </android.support.v7.widget.RecyclerView> 52</LinearLayout> 53 54<RelativeLayout 55 android:layout_width="match_parent" 56 android:layout_height="44dp" 57 android:gravity="center_vertical" 58 > 59 <TextView 60 android:id="@+id/spc_tv_zong_price" 61 android:layout_width="wrap_content" 62 android:layout_height="wrap_content" 63 android:textSize="16dp" 64 android:textColor="@color/mainColor" 65 android:layout_alignParentRight="true" 66 android:text="100.00" 67 android:layout_marginRight="@dimen/side_distance" 68 /> 69 <TextView 70 android:id="@+id/spc_tv_price_money" 71 android:layout_width="wrap_content" 72 android:layout_height="wrap_content" 73 android:text="总价:" 74 android:textColor="#666666" 75 android:textSize="15dp" 76 android:layout_toLeftOf="@id/spc_tv_zong_price" 77 android:layout_marginRight="10dp" 78 /> 79 <LinearLayout 80 android:layout_width="wrap_content" 81 android:layout_height="wrap_content" 82 android:orientation="horizontal" 83 android:layout_toLeftOf="@id/spc_tv_price_money" 84 android:layout_marginRight="15dp" 85 > 86 <TextView 87 android:id="@+id/spc_tv_shop_count" 88 android:layout_width="wrap_content" 89 android:layout_height="wrap_content" 90 android:textColor="#666666" 91 android:textSize="15dp" 92 android:text="共2件商品" 93 /> 94 </LinearLayout> 95</RelativeLayout> 96 <View 97 style="@style/Line_e0e0e0_Horizontal"/> 98</LinearLayout>

内层商品Item;

1<?xml version="1.0" encoding="utf-8"?> 2<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="wrap_content" 5 android:background="@color/defaultBackground" 6 android:descendantFocusability="blocksDescendants" 7 > 8 9 <RelativeLayout 10 android:id="@+id/rl" 11 android:layout_width="match_parent" 12 android:layout_height="100dp" 13 android:paddingTop="5dp"> 14 <View 15 android:layout_alignParentBottom="true" 16 style="@style/Line_e0e0e0_Horizontal"/> 17 <CheckBox 18 android:id="@+id/spc_cb_goods" 19 style="@style/spc_checkbox_style" 20 android:layout_width="wrap_content" 21 android:layout_height="match_parent" 22 android:paddingLeft="@dimen/side_distance" 23 android:paddingRight="@dimen/side_distance" /> 24 25 <ImageView 26 android:id="@+id/spc_iv_page" 27 android:layout_width="85dp" 28 android:layout_height="85dp" 29 android:layout_toRightOf="@id/spc_cb_goods" 30 android:layout_alignParentBottom="true" 31 android:src="@mipmap/default_icon" /> 32 33 <TextView 34 android:id="@+id/spc_tv_shop_name_msg" 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" 37 android:layout_marginLeft="10dp" 38 android:layout_marginRight="@dimen/side_distance" 39 android:layout_marginTop="8dp" 40 android:layout_toRightOf="@id/spc_iv_page" 41 android:lines="2" 42 android:text="title" 43 android:lineSpacingExtra="1dp" 44 android:textColor="@color/defaultTextview" 45 android:textSize="13dp" /> 46 47 <TextView 48 android:id="@+id/spc_tv_pinlei" 49 android:layout_width="wrap_content" 50 android:layout_height="wrap_content" 51 android:text="已选:28码" 52 android:layout_below="@id/spc_tv_shop_name_msg" 53 android:layout_marginLeft="10dp" 54 android:layout_marginTop="3dp" 55 android:layout_toRightOf="@id/spc_iv_page" 56 android:textColor="@color/defaultTextview" 57 android:textSize="11dp" 58 android:visibility="visible" /> 59 60 <TextView 61 android:id="@+id/spc_tv_zong_price" 62 android:layout_width="wrap_content" 63 android:layout_height="wrap_content" 64 android:text="¥:109.89" 65 android:layout_below="@id/spc_tv_pinlei" 66 android:layout_marginLeft="10dp" 67 android:layout_toRightOf="@id/spc_iv_page" 68 android:layout_marginTop="@dimen/dp4" 69 android:textColor="@color/mainColor" 70 android:textSize="16dp" /> 71 72 73 <Button 74 android:id="@+id/spc_btn_comm_count_jian" 75 android:layout_width="32dp" 76 android:layout_height="32dp" 77 android:layout_alignTop="@+id/spc_tv_pinlei" 78 android:layout_toLeftOf="@+id/spc_et_comm_count" 79 android:layout_toStartOf="@+id/spc_et_comm_count" 80 android:background="#ffffff" 81 android:text="一" 82 android:textColor="@color/defaultTextview" 83 android:textSize="16dp" /> 84 85 <TextView 86 android:id="@+id/spc_et_comm_count" 87 android:layout_width="44dp" 88 android:layout_height="32dp" 89 android:layout_alignTop="@+id/spc_tv_pinlei" 90 android:layout_toLeftOf="@+id/spc_btn_comm_count_jia" 91 android:layout_toStartOf="@+id/spc_btn_comm_count_jia" 92 android:background="#ffffff" 93 android:gravity="center" 94 android:text="0" 95 android:textColor="@color/defaultTextview" 96 android:textSize="16dp" /> 97 98 <Button 99 android:id="@+id/spc_btn_comm_count_jia" 100 android:layout_width="32dp" 101 android:layout_height="32dp" 102 android:layout_alignParentRight="true" 103 android:layout_alignTop="@+id/spc_tv_pinlei" 104 android:layout_marginRight="@dimen/side_distance" 105 android:background="#ffffff" 106 android:text="+" 107 android:textColor="@color/defaultTextview" 108 android:textSize="16dp" 109 android:layout_alignRight="@+id/spc_tv_shop_name_msg" 110 android:layout_alignEnd="@+id/spc_tv_shop_name_msg" /> 111 112 </RelativeLayout> 113 114 115</RelativeLayout>

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

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

点赞
收藏

评论区

加载中...

相关推荐

Android自定义Dialog多选对话框(Dialog+Listview+CheckBox)

先放效果截图项目中需要有个Dialog全选对话框,点击全选全部选中,取消全选全部取消。下午查了些资料,重写了一下Dialog对话框。把代码放出来。!这里写图片描述(https://oscimg.oschina.net/oscnet/0231bb305ff838c957875702a8373cf7c26.gif)publicclassM

Android ListView长按事件弹出菜单并获取选中的item

看了很多listview的长按事件,但几乎都是只给出弹出菜单的代码,没有给出选中的某个项的代码,我贴个全的吧,免得摸索麻烦思路就是listview在父窗口先注册一个长按弹出菜单registerReceiver》设置一个长按的listener,保存好选中item数据》onCreateContextMenu添加菜单》onContextItemSel

ReactNative state更新,视图不更新的问题

开发中遇到这样的问题,我更新了state一个数组的某个元素的选中状态,打印出的数据也显示修改正确了,但是界面却没更新。例如下图点击某项修改选中状态。!(https://oscimg.oschina.net/oscnet/c3291a62b5f638d1e35dd7a719ade39f226.png)代码中之前是这样写的,结果界面没有更新。

shopee商品详情接口,店铺商品接口,商品评论接口代码封装教程

shopee商品详情接口,shopee商品列表接口,shopee商品数据接口,shopee店铺商品接口,关键词搜索shopee商品接口,关键词搜索shopee商品列表接口,shopee商品API接口,shopee商品详情属性接口,shopee详情sku数据,shopee店铺详情接口,shopee规格数据接口,shopee商品销量接口,shopee商品sku接口,shopee尺寸接口,shopee重量接口

千万级数据深分页查询SQL性能优化实践

一、系统介绍和问题描述如何在Mysql中实现上亿数据的遍历查询?先来介绍一下系统主角:关注系统,主要是维护京东用户和业务对象之前的关注关系;并对外提供各种关系查询,比如查询用户的关注商品或店铺列表,查询用户是否关注了某个商品或店铺等。但是最近接到了一个新需

千万级数据深分页查询SQL性能优化实践

作者:京东零售曹志飞一、系统介绍和问题描述如何在Mysql中实现上亿数据的遍历查询?先来介绍一下系统主角:关注系统,主要是维护京东用户和业务对象之前的关注关系;并对外提供各种关系查询,比如查询用户的关注商品或店铺列表,查询用户是否关注了某个商品或店铺等。但

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