很久之前的代码了,拉出来晾晾!
购物车大致思路:
分为:商品、店铺、全选;
商品全部选中后--店铺自动选中;商品未全部选中(若有一个商品未选中)--店铺不选中。
店铺全部选中后--全选自动选中;店铺未全部选中(若有一个店铺未选中)--全选不选中。
选中店铺 -- 店铺中的商品全部选中。
全选选中 -- 店铺全部选中。(全部选中);
我是使用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;