<?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="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>
<?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: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"
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>
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import cn.zhaoliang5156.shopcart.R;
import cn.zhaoliang5156.shopcart.ui.adapter.ShopCartAdapter;
import cn.zhaoliang5156.shopcart.ui.bean.ShopBean;
import cn.zhaoliang5156.shopcart.ui.event.OnResfreshListener;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* 购物车主界面
*
* @author zhaoliang
* @version 1.0
* @create 2018/9/20
*/
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(this, mAllOrderList);
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 v) {
// 对全选状态进行去反
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(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Gson gson = new Gson();
ShopBean shopBean = gson.fromJson(response.body().string(), ShopBean.class);
Log.i(TAG, "shopBean:" + shopBean.getMsg());
for (ShopBean.DataBean dataBean : shopBean.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();
}
});
}
});
}
/**
* 判断是否是商品的第一个商品 是第一个商品 需要显示商铺
*
* @author zhaoliang
* @version 1.0
* @create 2018/9/17
*/
public static void isSelectFirst(List<ShopBean.DataBean.ListBean> list) {
// 1. 判断是否有商品 有商品 根据商品是否是第一个显示商铺
if (list.size() > 0) {
//头个商品一定属于它所在商铺的第一个位置,isFirst标记为1.
list.get(0).setFirst(true);
for (int i = 1; i < list.size(); i++) {
//每个商品跟它前一个商品比较,如果Shopid相同isFirst则标记为2,
//如果Shopid不同,isFirst标记为1.
if (list.get(i).getSellerid() == list.get(i - 1).getSellerid()) {
list.get(i).setFirst(false);
} else {
list.get(i).setFirst(true);
}
}
}
}
}
adapter包
package cn.zhaoliang5156.shopcart.ui.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bumptech.glide.Glide;
import java.util.List;
import cn.zhaoliang5156.shopcart.R;
import cn.zhaoliang5156.shopcart.ui.activity.MainActivity;
import cn.zhaoliang5156.shopcart.ui.bean.ShopBean;
import cn.zhaoliang5156.shopcart.ui.event.OnResfreshListener;
import cn.zhaoliang5156.shopcart.ui.holder.ShopCartHolder;
/**
* 购物车Adapter
*
* @author zhaoliang
* @version 1.0
* @create 2018/9/20
*/
public class ShopCartAdapter extends RecyclerView.Adapter<ShopCartHolder> {
private Context context;
private List<ShopBean.DataBean.ListBean> data;
public ShopCartAdapter(Context context, List<ShopBean.DataBean.ListBean> data) {
this.context = context;
this.data = data;
}
@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(data.get(position).getImages().split("\\|")[0]).into(holder.ivShopCartClothPic);
/* 商品的基本信息 */
// holder.tvShopCartClothColor.setText("颜色:" + data.get(position).getColor());
// holder.tvShopCartClothSize.setText("尺寸:" + data.get(position).getSize());
// holder.tvShopCartClothName.setText(data.get(position).getProductName());
holder.tvShopCartShopName.setText(data.get(position).getShopName());
holder.tvShopCartClothPrice.setText("¥" + data.get(position).getPrice());
holder.etShopCartClothNum.setText(data.get(position).getNum() + "");
/* 显示前面的选中状态 */
if (data.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 (data.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 (position > 0) {
/* 判断是否是同一个商铺的商品 */
if (data.get(position).getSellerid() == data.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 < data.size(); i++) {
if (data.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) {
data.get(position).setNum(data.get(position).getNum() + 1);
notifyDataSetChanged();
}
});
/* 商品数量减 */
holder.ivShopCartClothMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (data.get(position).getNum() > 1) {
data.get(position).setNum(data.get(position).getNum() - 1);
notifyDataSetChanged();
}
}
});
/* 删除操作 */
holder.ivShopCartClothDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
data.remove(position);
//重新排序,标记所有商品不同商铺第一个的商品位置
MainActivity.isSelectFirst(data);
notifyDataSetChanged();
}
});
/* 单个商品 选中状态 */
holder.ivShopCartClothSel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
data.get(position).setSelected(data.get(position).getSelected() == 0 ? 1 : 0);
//通过循环找出不同商铺的第一个商品的位置
for (int i = 0; i < data.size(); i++) {
if (data.get(i).isFirst()) {
//遍历去找出同一家商铺的所有商品的勾选情况
for (int j = 0; j < data.size(); j++) {
//如果是同一家商铺的商品,并且其中一个商品是未选中,那么商铺的全选勾选取消
if (data.get(j).getSellerid() == data.get(i).getSellerid() && data.get(j).getSelected() == 1) {
data.get(i).setShopSelect(false);
break;
} else {
//如果是同一家商铺的商品,并且所有商品是选中,那么商铺的选中全选勾选
data.get(i).setShopSelect(true);
}
}
}
}
notifyDataSetChanged();
}
});
/* 商铺选中状态 */
holder.ivShopCartShopSel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (data.get(position).isFirst()) {
// 商铺选中状态执反
data.get(position).setShopSelect(!data.get(position).isShopSelect());
// 改变商品的选中状态和商铺一样
for (int i = 0; i < data.size(); i++) {
if (data.get(i).getSellerid() == data.get(position).getSellerid()) {
data.get(i).setSelected(data.get(position).isShopSelect() ? 0 : 1);
}
}
notifyDataSetChanged();
}
}
});
}
@Override
public int getItemCount() {
return data.size();
}
// 刷新的接口
private OnResfreshListener mOnResfreshListener;
public void setOnResfreshListener(OnResfreshListener mOnResfreshListener) {
this.mOnResfreshListener = mOnResfreshListener;
}
}
event包
public interface OnResfreshListener {
/**
* 刷新界面 总价、总数量和全选的标识
*
* @param isSelect 是否是全选
*/
void onResfresh(boolean isSelect);
}
hodel包
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);
}
}
go_pay.xml
<?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
<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
<?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>