//adapter
public class ShopCartAdapter extends RecyclerView.Adapter<ShopCartHolder> { List<ShopBean.DataBean.ListBean> list; Context context; public ShopCartAdapter(List<ShopBean.DataBean.ListBean> list, Context context) { this.list = list; this.context = context; } @NonNull @Override public ShopCartHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { return new ShopCartHolder(LayoutInflater.from(context).inflate(R.layout.shop_cart_item,parent,false)); } @Override public void onBindViewHolder(@NonNull ShopCartHolder holder, final int position) { Glide.with(context).load(list.get(position).getImages().split("\\|")[0]).into(holder.ivShopCartClothPic); holder.tvShopCartShopName.setText(list.get(position).getShopName()); holder.tvShopCartClothPrice.setText("¥" + list.get(position).getPrice()); holder.etShopCartClothNum.setText(list.get(position).getNum() + ""); if(list.get(position).getSelected()==0){ holder.ivShopCartShopSel.setImageDrawable(context.getResources().getDrawable(R.drawable.shopcart_selected)); }else{ holder.ivShopCartShopSel.setImageDrawable(context.getResources().getDrawable(R.drawable.shopcart_unselected)); } if (list.get(position).getSelected() == 0) { holder.ivShopCartClothSel.setImageDrawable(context.getResources().getDrawable(R.drawable.shopcart_selected)); } else { holder.ivShopCartClothSel.setImageDrawable(context.getResources().getDrawable(R.drawable.shopcart_unselected)); } /* 判断是否显示商铺 */ if (position > 0) { /* 判断是否是同一个商铺的商品 */ if (list.get(position).getSellerid() == list.get(position - 1).getSellerid()) { holder.llShopCartHeader.setVisibility(View.GONE); } else { holder.llShopCartHeader.setVisibility(View.VISIBLE); } } else { holder.llShopCartHeader.setVisibility(View.VISIBLE); } // 计算逻辑 与操作 /* 判断是否全选并计算 */ if (mOnResfreshListener != null) { boolean isSelect = false; for (int i = 0; i < list.size(); i++) { if (list.get(i).getSelected() == 1) { isSelect = false; break; } else { isSelect = true; } } mOnResfreshListener.onResfresh(isSelect); } /* 商品数量加 */ holder.ivShopCartClothAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { list.get(position).setNum(list.get(position).getNum() + 1); notifyDataSetChanged(); } }); /* 商品数量减 */ holder.ivShopCartClothMinus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (list.get(position).getNum() > 1) { list.get(position).setNum(list.get(position).getNum() - 1); notifyDataSetChanged(); } } }); /* 删除操作 */ holder.ivShopCartClothDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { list.remove(position); //重新排序,标记所有商品不同商铺第一个的商品位置 MainActivity.isSelectFirst(list); notifyDataSetChanged(); } }); /* 单个商品 选中状态 */ holder.ivShopCartClothSel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { list.get(position).setSelected(list.get(position).getSelected() == 0 ? 1 : 0); //通过循环找出不同商铺的第一个商品的位置 for (int i = 0; i < list.size(); i++) { if (list.get(i).isFirst()) { //遍历去找出同一家商铺的所有商品的勾选情况 for (int j = 0; j < list.size(); j++) { //如果是同一家商铺的商品,并且其中一个商品是未选中,那么商铺的全选勾选取消 if (list.get(j).getSellerid() == list.get(i).getSellerid() && list.get(j).getSelected() == 1) { list.get(i).setShopSelect(false); break; } else { //如果是同一家商铺的商品,并且所有商品是选中,那么商铺的选中全选勾选 list.get(i).setShopSelect(true); } } } } notifyDataSetChanged(); } }); /* 商铺选中状态 */ holder.ivShopCartShopSel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (list.get(position).isFirst()) { // 商铺选中状态执反 list.get(position).setShopSelect(!list.get(position).isShopSelect()); // 改变商品的选中状态和商铺一样 for (int i = 0; i < list.size(); i++) { if (list.get(i).getSellerid() == list.get(position).getSellerid()) { list.get(i).setSelected(list.get(position).isShopSelect() ? 0 : 1); } } notifyDataSetChanged(); } } }); } @Override public int getItemCount() { return list.size(); } // 刷新的接口 private OnResfreshListener mOnResfreshListener; public void setOnResfreshListener(OnResfreshListener mOnResfreshListener) { this.mOnResfreshListener = mOnResfreshListener; } }
//OnResfreshListener 结算总价
public interface OnResfreshListener { void onResfresh(boolean isSelect); }
//holder
public class ShopCartHolder extends RecyclerView.ViewHolder { public ImageView ivShopCartShopSel; public TextView tvShopCartShopName; public TextView tvShopCartClothName; public TextView tvShopCartClothPrice; public TextView etShopCartClothNum; public TextView tvShopCartClothColor; public TextView tvShopCartClothSize; public ImageView ivShopCartClothSel; public ImageView ivShopCartClothMinus; public ImageView ivShopCartClothAdd; public ImageView ivShopCartClothDelete; public ImageView ivShopCartClothPic; public LinearLayout llShopCartHeader; public ShopCartHolder(View itemView) { super(itemView); llShopCartHeader = itemView.findViewById(R.id.ll_shopcart_header); ivShopCartShopSel = itemView.findViewById(R.id.iv_item_shopcart_shopselect); tvShopCartShopName = itemView.findViewById(R.id.tv_item_shopcart_shopname); tvShopCartClothName = itemView.findViewById(R.id.tv_item_shopcart_clothname); tvShopCartClothPrice = itemView.findViewById(R.id.tv_item_shopcart_cloth_price); etShopCartClothNum = itemView.findViewById(R.id.et_item_shopcart_cloth_num); tvShopCartClothColor = itemView.findViewById(R.id.tv_item_shopcart_cloth_color); tvShopCartClothSize = itemView.findViewById(R.id.tv_item_shopcart_cloth_size); ivShopCartClothSel = itemView.findViewById(R.id.tv_item_shopcart_clothselect); ivShopCartClothMinus = itemView.findViewById(R.id.iv_item_shopcart_cloth_minus); ivShopCartClothAdd = itemView.findViewById(R.id.iv_item_shopcart_cloth_add); ivShopCartClothPic = itemView.findViewById(R.id.iv_item_shopcart_cloth_pic); ivShopCartClothDelete = itemView.findViewById(R.id.iv_item_shopcart_cloth_delete); } }
//main主页面
public class MainActivity extends AppCompatActivity { private static final String TAG = MainActivity.class.getSimpleName(); private TextView tvShopCartSubmit, tvShopCartSelect, tvShopCartTotalNum; private RecyclerView rlvShopCart, rlvHotProducts; private ShopCartAdapter mShopCartAdapter; private LinearLayout llPay; private RelativeLayout rlHaveProduct; private List<ShopBean.DataBean.ListBean> mAllOrderList = new ArrayList<>(); private ArrayList<ShopBean.DataBean.ListBean> mGoPayList = new ArrayList<>(); private List<String> mHotProductsList = new ArrayList<>(); private TextView tvShopCartTotalPrice; private int mCount, mPosition; private float mTotalPrice1; private boolean mSelect; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvShopCartSelect = findViewById(R.id.tv_shopcart_addselect); tvShopCartTotalPrice = findViewById(R.id.tv_shopcart_totalprice); tvShopCartTotalNum = findViewById(R.id.tv_shopcart_totalnum); rlHaveProduct = findViewById(R.id.rl_shopcart_have); rlvShopCart = findViewById(R.id.rlv_shopcart); llPay = findViewById(R.id.ll_pay); tvShopCartSubmit = findViewById(R.id.tv_shopcart_submit); rlvShopCart.setLayoutManager(new LinearLayoutManager(this)); mShopCartAdapter = new ShopCartAdapter(mAllOrderList,this); rlvShopCart.setAdapter(mShopCartAdapter); //实时监控全选按钮 mShopCartAdapter.setOnResfreshListener(new OnResfreshListener() { @Override public void onResfresh(boolean isSelect) { mSelect = isSelect; if (isSelect) { Drawable left = getResources().getDrawable(R.drawable.shopcart_selected); tvShopCartSelect.setCompoundDrawablesWithIntrinsicBounds(left, null, null, null); } else { Drawable left = getResources().getDrawable(R.drawable.shopcart_unselected); tvShopCartSelect.setCompoundDrawablesWithIntrinsicBounds(left, null, null, null); } // 计算总价 float mTotalPrice = 0; int mTotalNum = 0; mTotalPrice1 = 0; mGoPayList.clear(); // 遍历所有商品 计算总价 for (int i = 0; i < mAllOrderList.size(); i++) if (mAllOrderList.get(i).getSelected() == 0) { mTotalPrice += mAllOrderList.get(i).getPrice() * mAllOrderList.get(i).getNum(); mTotalNum += 1; mGoPayList.add(mAllOrderList.get(i)); } mTotalPrice1 = mTotalPrice; tvShopCartTotalPrice.setText("总价:" + mTotalPrice); tvShopCartTotalNum.setText("共" + mTotalNum + "件商品"); } }); tvShopCartSelect.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mSelect=!mSelect; if(mSelect){ //全部选中 Drawable left = getResources().getDrawable(R.drawable.shopcart_selected); tvShopCartSelect.setCompoundDrawablesWithIntrinsicBounds(left,null,null,null); for(ShopBean.DataBean.ListBean listBean:mAllOrderList){ listBean.setSelected(0); listBean.setShopSelect(true); } }else{ // 全部取消 Drawable left = getResources().getDrawable(R.drawable.shopcart_unselected); tvShopCartSelect.setCompoundDrawablesWithIntrinsicBounds(left, null, null, null); for (ShopBean.DataBean.ListBean listBean : mAllOrderList) { listBean.setSelected(1); listBean.setShopSelect(false); } } mShopCartAdapter.notifyDataSetChanged(); } }); initData(); } private void initData() { OkHttpClient client=new OkHttpClient(); client.newCall(new Request. Builder(). url("https://www.zhaoapi.cn/product/getCarts?uid=71"). build()). enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { } @Override public void onResponse(Response response) throws IOException { Gson gson=new Gson(); ShopBean bean = gson.fromJson(response.body().string(), ShopBean.class); Log.i(TAG, "shopBean:" + bean.getMsg()); for(ShopBean.DataBean dataBean : bean.getData()){ for(ShopBean.DataBean.ListBean listBean: dataBean.getList()){ listBean.setShopName(dataBean.getSellerName()); mAllOrderList.add(listBean); } } isSelectFirst(mAllOrderList); runOnUiThread(new Runnable() { @Override public void run() { mShopCartAdapter.notifyDataSetChanged(); } }); } }); } public static void isSelectFirst(List<ShopBean.DataBean.ListBean> list) { if(list.size()>0){ list.get(0).setFirst(true); for(int i=1;i<list.size();i++){ if(list.get(i).getSelected()==list.get(i-1).getSelected()){ list.get(i).setFirst(false); }else{ list.get(i).setFirst(true); } } } } }
//主页面布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 购物车列表 --> <RelativeLayout android:id="@+id/rl_shopcart_have" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <android.support.v7.widget.RecyclerView android:id="@+id/rlv_shopcart" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <View android:layout_width="match_parent" android:layout_height="50dp" /> </LinearLayout> <!-- 底部支付 --> <LinearLayout android:id="@+id/ll_pay" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="@color/main_white_text" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:id="@+id/tv_shopcart_addselect" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/margin_10dp" android:drawableLeft="@drawable/shopcart_selected" android:drawablePadding="@dimen/padding_5dp" android:text="全选" /> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <TextView android:id="@+id/tv_shopcart_totalprice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="@dimen/padding_10dp" android:paddingTop="@dimen/padding_10dp" android:text="总价:¥0" android:textColor="@color/main_red_text" android:textSize="@dimen/common_font_size_16" /> <TextView android:id="@+id/tv_shopcart_totalnum" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="@dimen/padding_10dp" android:paddingBottom="@dimen/padding_10dp" android:text="共0件商品" android:textSize="@dimen/common_font_size_14" /> </LinearLayout> <TextView android:id="@+id/tv_shopcart_submit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="@dimen/margin_10dp" android:background="@drawable/shop_btn" android:paddingLeft="@dimen/margin_30dp" android:paddingTop="@dimen/padding_10dp" android:paddingRight="@dimen/margin_30dp" android:paddingBottom="@dimen/padding_10dp" android:text="去结算" android:textColor="@color/main_white_text" /> </LinearLayout> </RelativeLayout> </RelativeLayout>
//shop_cart_item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/main_white_text" android:orientation="vertical"> <LinearLayout android:id="@+id/ll_shopcart_header" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- 距离占位 --> <View android:id="@+id/view" android:layout_width="match_parent" android:layout_height="@dimen/margin_10dp" android:background="@color/background_color"></View> <!-- 商铺 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical"> <ImageView android:id="@+id/iv_item_shopcart_shopselect" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="@dimen/margin_15dp" android:paddingTop="@dimen/margin_10dp" android:paddingRight="@dimen/margin_15dp" android:paddingBottom="@dimen/margin_10dp" android:src="@drawable/shopcart_selected" /> <TextView android:id="@+id/tv_item_shopcart_shopname" android:layout_width="match_parent" android:layout_height="wrap_content" android:drawableLeft="@drawable/shopcart_shop" android:drawablePadding="@dimen/padding_5dp" android:padding="@dimen/padding_10dp" android:text="宝儿家服装" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- 分隔商铺和商铺的线 --> <View android:layout_width="match_parent" android:layout_height="@dimen/margin_1dp" android:background="@color/background_color"></View> <!-- 商品名称 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- 图片占位 为了对其好看 --> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/margin_15dp" android:layout_marginRight="@dimen/margin_15dp" android:src="@drawable/shopcart_selected" android:visibility="invisible" /> <!-- 显示商品名称的 --> <TextView android:id="@+id/tv_item_shopcart_clothname" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="@dimen/padding_10dp" android:paddingTop="@dimen/padding_10dp" android:text="小米6" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"> <!-- 选中图片 --> <ImageView android:id="@+id/tv_item_shopcart_clothselect" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="@dimen/margin_15dp" android:src="@drawable/shopcart_selected" /> <!-- 商品图片 --> <ImageView android:id="@+id/iv_item_shopcart_cloth_pic" android:layout_width="60dp" android:layout_height="60dp" android:layout_margin="@dimen/margin_10dp" /> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <!-- 商品价钱的TextView --> <TextView android:id="@+id/tv_item_shopcart_cloth_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="¥185" android:textColor="@color/main_red_text" android:textSize="@dimen/common_font_size_14" /> <!-- 商品的颜色和尺寸 --> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="@dimen/margin_5dp" android:layout_marginBottom="@dimen/margin_5dp"> <TextView android:id="@+id/tv_item_shopcart_cloth_color" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="颜色:黑色" android:textSize="@dimen/common_font_size_12" /> <TextView android:id="@+id/tv_item_shopcart_cloth_size" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/margin_10dp" android:text="尺寸:XL" android:textSize="@dimen/common_font_size_12" /> </LinearLayout> <!-- 加减键号 商品数量 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical"> <ImageView android:id="@+id/iv_item_shopcart_cloth_minus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/shopcart_minus_grey" /> <TextView android:id="@+id/et_item_shopcart_cloth_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/margin_5dp" android:background="@drawable/shopcart_add_btn" android:paddingLeft="@dimen/padding_20dp" android:paddingTop="@dimen/padding_2dp" android:paddingRight="@dimen/padding_20dp" android:paddingBottom="@dimen/padding_2dp" android:text="1" /> <ImageView android:id="@+id/iv_item_shopcart_cloth_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/margin_5dp" android:src="@drawable/shopcart_add_red" /> </LinearLayout> </LinearLayout> <!-- 竖线 --> <View android:layout_width="@dimen/margin_1dp" android:layout_height="match_parent" android:layout_marginTop="@dimen/padding_10dp" android:layout_marginBottom="@dimen/padding_10dp" android:background="@color/splitline_color"></View> <!-- 删除的图片 --> <ImageView android:id="@+id/iv_item_shopcart_cloth_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="@dimen/margin_20dp" android:src="@drawable/shopcart_delete" /> </LinearLayout> </LinearLayout> </LinearLayout> <!-- 底部的线 --> <View android:layout_width="match_parent" android:layout_height="@dimen/margin_1dp" android:background="@color/background_color"></View> </LinearLayout>
//color
<!--背景颜色--> <color name="background_color">#f6f6f6</color> <!--分割线--> <color name="splitline_color">#dddddd</color> <!--文字--> <color name="main_red_text">#e53e42</color> <color name="main_white_text">#ffffff</color> <!--图标--> <color name="default_icon_color">#999999</color> <color name="pressed_icon_color">#e53e42</color>
//go_pay
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/holo_red_dark" />
<corners android:radius="20dp" />
</shape>
//shop_box
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="@dimen/height_200dp"></corners>
<stroke android:color="@color/default_icon_color" android:width="1dp"></stroke>
</shape>
//shop_btn
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="@dimen/height_200dp"></corners>
<solid android:color="@color/pressed_icon_color"></solid>
</shape>