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

购物车大致思路:

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

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

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

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

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

我是使用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

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

  1. android文件选择器、仿淘宝编辑页面、新手引导层等源码

    Android精选源码 单片机和安卓应用,传感器 文件选择器 android滑动选择的尺子view源码 android视频录制 视频压缩的源码 仿今日头条顶部导航指示器源码 Android框架+常用控 ...

  2. Android仿淘宝购物车demo

    夏的热情渐渐退去,秋如期而至,丰收的季节,小编继续着实习之路,走着走着,就走到了购物车,逛过淘宝或者是京东的小伙伴都知道购物车里面的宝贝可不止一件,对于爱购物的姑娘来说,购物车里面的商品恐怕是爆满,添 ...

  3. android电子书App、自定义图表、仿腾讯漫画App、仿淘宝优惠券、3D选择容器等源码

    Android精选源码 仿支付宝记账本功能,饼状图:数字键盘 android一款功能完善的电子书应用源码 Android自定义图标库,使用方便,扩展性强 android 3D立体无限旋转容器源码 an ...

  4. Android中仿淘宝首页顶部滚动自定义HorizontalScrollView定时水平自动切换图片

    Android中仿淘宝首页顶部滚动自定义HorizontalScrollView定时水平自动切换图片 自定义ADPager 自定义水平滚动的ScrollView效仿ViewPager 当遇到要在Vie ...

  5. android版高仿淘宝客户端源码V2.3

    android版高仿淘宝客户端源码V2.3,这个版本我已经更新到2.3了,源码也上传到源码天堂那里了,大家可以看一下吧,该应用实现了我们常用的购物功能了,也就是在手机上进行网购的流程的,如查看产品(浏 ...

  6. Android仿淘宝继续上拉进入商品详情页的效果,使用双Fragment动画切换;

    仿淘宝继续上拉进入商品详情页的效果,双Fragment实现: 动画效果: slide_above_in.xml <?xml version="1.0" encoding=&q ...

  7. Android仿淘宝头条滚动广告条

    之前我使用TextView+Handler+动画,实现了一个简单的仿淘宝广告条的滚动,https://download.csdn.net/download/qq_35605213/9660825: 无 ...

  8. 高仿淘宝和聚美优品商城详情页实现《IT蓝豹》

    高仿淘宝和聚美优品商城详情页实现 android-vertical-slide-view高仿淘宝和聚美优品商城详情页实现,在商品详情页,向上拖动时,可以加载下一页. 使用ViewDragHelper, ...

  9. JavaScript仿淘宝实现放大镜效果的实例

    我们都知道放大镜效果一般都是用于一些商城中的,列如每当我们打开淘宝,天猫等pc端时,看到心仪的物品时,点击图片时,便呈现出放大镜的效果.在没有去理解分析它的原理时,感觉非常的神奇,当真正地去接触,也是 ...

随机推荐

  1. react 实现路由按需加载

    import() 方法: async.js 文件内容: import React from 'react'; // import "babel-polyfill"; //compo ...

  2. python装饰器学习笔记

    定义:本质上就是个函数,(装饰器其他函数)就是为了给其他函数添加附加功能 原则:1.不能修改被装饰的函数的源代码 2.不能修改被装饰的函数的调用方式 #-*-coding:utf-8-*- 1 imp ...

  3. Zookeeper常用操作命令create,set,delete

    一.zk特性的session的基本原理 1.客户端与服务端之间的连接存在会话 2.每个会话都可以设置一个超时时间 3.心跳结束,session则过期 4.session过期,则临时节点znode会被抛 ...

  4. deque/defaultdict/orderedict/collections.namedtuple()/collections.ChainMap() 笔记

    关于deque的使用 collections.deque([list[, max_length]]) # 不限定长度,可随意添加没有上限 >>> from collections i ...

  5. 中文自然语言处理工具hanlp隐马角色标注详解

    本文旨在介绍如何利用HanLP训练分词模型,包括语料格式.语料预处理.训练接口.输出格式等. 目前HanLP内置的训练接口是针对一阶HMM-NGram设计的,另外附带了通用的语料加载工具,可以通过少量 ...

  6. curl发送post请求,统计响应时间

    curl  -o /dev/null -s -w %{time_namelookup}::%{time_connect}::%{time_starttransfer}::%{time_total}:: ...

  7. IK分词器的使用

    1.下载 根据自己的版本进行下载 https://github.com/medcl/elasticsearch-analysis-ik/releases wget https://github.com ...

  8. [InfluxDB] 安装与配置

    [InfluxDB] 安装与配置 1- 下载 ubtuntu: wget https://dl.influxdata.com/influxdb/releases/influxdb_1.5.2_amd6 ...

  9. 黄聪:在.NET中使用GeckoFX 29

    GeckoFX is a .NET control, that works similarly to “System.Windows.Forms.WebBrowser” Control, while ...

  10. 黄聪:ffmpeg参数说明(转载)

    ffmpeg.exe -i F:\闪客之家\闪客之歌.mp3 -ab 56 -ar 22050 -b 500 -r 15 -s 320x240 f:\11.flv ffmpeg -i F:\01.wm ...