所用的依赖
implementation 'com.android.support:recyclerview-v7:27.0.2'
compile 'com.squareup.okhttp3:okhttp:3.9.1'
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.github.bumptech.glide:glide:3.6.1'
主方法
package com.bwie.shoppcardemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
public class MainActivity extends AppCompatActivity implements CarView , MyAdapter .CheckGroupInterfaceListener , MyAdapter .ModifyGroupInterfaceListener {
private TextView btnBack;
private TextView btnEditor;
private RecyclerView carListView;
private CheckBox chooseAll;
private TextView totalShowPrice;
private TextView btnAmount;
private MyAdapter adapter;
private List<ShoppingBean.DataBean.ListBean> list;
/**
* 记录用户点击编辑按钮的一个状态
*/
private boolean flag;
private double totalPrice = 0.00 ;
private int totalNum = 0 ;
@Override
protected void onCreate (Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
ShoppPresenter shoppPresenter = new ShoppPresenter(this );
shoppPresenter.getCarInfo();
}
private void initView () {
btnBack = findViewById(R.id.btnBack);
btnEditor = findViewById(R.id.btnEditor);
carListView = findViewById(R.id.carListView);
chooseAll = findViewById(R.id.chooseAll);
totalShowPrice = findViewById(R.id.totalPrice);
btnAmount = findViewById(R.id.btnAmount);
carListView.setLayoutManager(new LinearLayoutManager(this ,LinearLayoutManager.VERTICAL,false ));
carListView.addItemDecoration(new DividerItemDecoration(this ,DividerItemDecoration.HORIZONTAL));
adapter = new MyAdapter(this );
carListView.setAdapter(adapter);
adapter.setCheckGroupInterfaceListener(this );
adapter.setModifyGroupInterfaceListener(this );
btnEditor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
if (!flag){
flag = true ;
adapter.showDelete(flag);
btnEditor.setText("完成" );
}else {
flag = false ;
adapter.showDelete(flag);
btnEditor.setText("编辑" );
}
}
});
chooseAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v) {
if (((CheckBox)v).isChecked()){
isChooseAll(((CheckBox)v).isChecked());
}else {
isChooseAll(((CheckBox)v).isChecked());
}
}
});
}
/**
* 是否全选
*/
private void isChooseAll (boolean isChooseAll){
for (ShoppingBean.DataBean.ListBean listBean : list){
if (isChooseAll){
listBean.setChoosed(true );
}else {
listBean.setChoosed(false );
}
}
adapter.notifyDataSetChanged();
}
@Override
public void onSuccess (Object o) {
ShoppingBean shoppingBean = (ShoppingBean) o;
list = shoppingBean.getData().get(0 ).getList();
adapter.setAdapterList(list);
}
@Override
public void onFailed () {
}
/**
* 检查购物车里面的商品是否全部被选中
*/
public boolean isCheckAll (){
for (ShoppingBean.DataBean.ListBean listBean : list) {
if (!listBean.isChoosed()){
return false ;
}
}
return true ;
}
@Override
public void chekcGourp (int position, boolean isChecked) {
ShoppingBean.DataBean.ListBean listBean = list.get(position);
listBean.setChoosed(isChecked);
if (isCheckAll()){
chooseAll.setChecked(true );
}else {
chooseAll.setChecked(false );
}
adapter.notifyDataSetChanged();
statistics();
}
/**
* 统计
* 计算商品价格
*/
private void statistics (){
totalPrice = 0.00 ;
totalNum = 0 ;
for (ShoppingBean.DataBean.ListBean listBean : list){
if (listBean.isChoosed()){
totalNum = listBean.getNum();
totalPrice += totalNum * listBean.getPrice();
}
}
totalShowPrice.setText(totalPrice+"¥" );
btnAmount.setText("结算 : (" +totalNum+")" );
}
@Override
public void doIncrease (int position, View showNumView, boolean isChecked) {
Toast.makeText(this , "正在添加" , Toast.LENGTH_SHORT).show();
ShoppingBean.DataBean.ListBean listBean = list.get(position);
int currentNum = listBean.getNum();
currentNum++;
listBean.setNum(currentNum);
adapter.notifyDataSetChanged();
statistics();
}
@Override
public void doDecrese (int position, View showNumView, boolean isChecked) {
Toast.makeText(this , "正在点击减" , Toast.LENGTH_SHORT).show();
ShoppingBean.DataBean.ListBean listBean = list.get(position);
int currentNum = listBean.getNum();
if (currentNum == 1 ) {
return ;
}
currentNum--;
listBean.setNum(currentNum);
adapter.notifyDataSetChanged();
statistics();
}
@Override
public void delteItem (int position) {
}
}
适配器
package com.bwie.shoppcardemo;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.List;
/**
* Created by stephen on 13/12/2017.
*/
public class MyAdapter extends RecyclerView .Adapter <MyAdapter .ViewHolder > {
private Context context;
private List<ShoppingBean.DataBean.ListBean> list;
private CheckGroupInterfaceListener checkGroupInterfaceListener;
private ModifyGroupInterfaceListener modifyGroupInterfaceListener;
private boolean isVisible;
public MyAdapter(Context context){
this .context = context;
}
public void setAdapterList(List<ShoppingBean.DataBean.ListBean> list){
this .list = list;
notifyDataSetChanged();
}
/**
* 点击商品条目checkbox一个监听
* @param checkGroupInterfaceListener
*/
public void setCheckGroupInterfaceListener(CheckGroupInterfaceListener checkGroupInterfaceListener){
this .checkGroupInterfaceListener = checkGroupInterfaceListener;
}
/**
* 对商品进行加减的监听
* @param modifyGroupInterfaceListener
*/
public void setModifyGroupInterfaceListener(ModifyGroupInterfaceListener modifyGroupInterfaceListener){
this .modifyGroupInterfaceListener = modifyGroupInterfaceListener;
}
/**
* 是否隐藏删除按钮
* @param isVisible
* @return
*/
public void showDelete(boolean isVisible){
this .isVisible = isVisible;
notifyDataSetChanged();
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.shopp_car_item,parent,false );
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(final MyAdapter.ViewHolder holder, final int position) {
final ShoppingBean.DataBean.ListBean listBean = list.get(position);
holder.tv_commodity_name.setText(listBean.getTitle());
holder.tv_commodity_attr.setText(listBean.getSubhead());
holder.tv_commodity_price.setText(listBean.getPrice()+"" );
holder.tv_commodity_num.setText(listBean.getNum()+"" );
holder.tv_commodity_show_num.setText(listBean.getNum()+"" );
Glide.with (context)
.load(listBean.getImages())
.into(holder.iv_show_pic);
if (listBean.isChoosed()){
holder.ck_chose.setChecked(true );
}else {
holder.ck_chose.setChecked(false );
}
holder.ck_chose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listBean.setCheck(((CheckBox)v).isChecked());
checkGroupInterfaceListener.chekcGourp(position,((CheckBox)v).isChecked());
}
});
holder.iv_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
modifyGroupInterfaceListener.doIncrease(position,holder.tv_commodity_show_num,true );
}
});
holder.iv_sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
modifyGroupInterfaceListener.doDecrese(position,holder.tv_commodity_show_num,true );
}
});
if (!isVisible){
holder.btn_commodity_delete.setVisibility(View.GONE);
}else {
holder.btn_commodity_delete.setVisibility(View.VISIBLE);
}
}
@Override
public int getItemCount() {
return list != null ? list.size() : 0 ;
}
class ViewHolder extends RecyclerView .ViewHolder {
CheckBox ck_chose;
ImageView iv_show_pic;
TextView tv_commodity_name;
TextView tv_commodity_attr;
TextView tv_commodity_price;
TextView tv_commodity_num;
LinearLayout rl_edit;
TextView iv_sub;
TextView tv_commodity_show_num;
TextView iv_add;
Button btn_commodity_delete;
public ViewHolder(View itemView) {
super (itemView);
ck_chose = itemView.findViewById(R.id.ck_chose);
iv_show_pic = itemView.findViewById(R.id.iv_show_pic);
tv_commodity_name = itemView.findViewById(R.id.tv_commodity_name);
tv_commodity_attr = itemView.findViewById(R.id.tv_commodity_attr);
tv_commodity_price = itemView.findViewById(R.id.tv_commodity_price);
tv_commodity_num = itemView.findViewById(R.id.tv_commodity_num);
rl_edit = itemView.findViewById(R.id.rl_edit);
iv_sub = itemView.findViewById(R.id.iv_sub);
tv_commodity_show_num = itemView.findViewById(R.id.tv_commodity_show_num);
iv_add = itemView.findViewById(R.id.iv_add);
btn_commodity_delete = itemView.findViewById(R.id.btn_commodity_delete);
}
}
/**
* 复选框是否被选中的一个接口
*/
public interface CheckGroupInterfaceListener{
void chekcGourp(int position,boolean isChecked);
}
/**
* 商品加减的接口
*/
public interface ModifyGroupInterfaceListener{
void doIncrease(int position,View showNumView,boolean isChecked);
void doDecrese(int position,View showNumView,boolean isChecked);
void delteItem(int position);
}
}
接口
package com.bwie.shoppcardemo;
/**
* Created by stephen on 13/12/2017.
*/
public interface OnFinishListener {
void onSuccess(Object o);
void onFaied();
}
Model
package com.bwie.shoppcardemo;
import android.os.Handler;
import com.google.gson.Gson;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* Created by stephen on 13/12/2017.
*/
public class ShoppingModel {
private static Handler handler = new Handler();
public void getCarInfo (String uid, final OnFinishListener onFinishListener){
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder()
.url("https://www.zhaoapi.cn/product/getCarts?uid=4582&source=android" )
.get()
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure (Call call, IOException e) {
handler.post(new Runnable() {
@Override
public void run () {
onFinishListener.onFaied();
}
});
}
@Override
public void onResponse (Call call, Response response) throws IOException {
Gson gson = GsonUtils.getInstence().getGsonInstance();
String jsonStr = response.body().string();
final ShoppingBean shoppingBean = gson.fromJson(jsonStr,ShoppingBean.class);
handler.post(new Runnable() {
@Override
public void run () {
onFinishListener.onSuccess(shoppingBean);
}
});
}
});
}
}
P层
package com.bwie.shoppcardemo;
/**
* Created by stephen on 13/12/2017.
*/
public class ShoppPresenter implements OnFinishListener {
private final ShoppingModel shoppingModel;
private CarView carView;
public ShoppPresenter (CarView carView){
this .carView = carView;
shoppingModel = new ShoppingModel();
}
/**
* 获取购物车列表信息
*/
public void getCarInfo (){
shoppingModel.getCarInfo("4582" ,this );
}
@Override
public void onSuccess (Object o) {
carView.onSuccess(o);
}
@Override
public void onFaied () {
carView.onFailed();
}
}
接口2
package com.bwie.shoppcardemo;
public interface CarView {
void onSuccess(Object o);
void onFailed();
}
网络封装
package com.bwie.shoppcardemo;
import com.google.gson.Gson;
/**
* Created by stephen on 13/12/2017.
*/
public class GsonUtils {
private static GsonUtils GSONUTILS = null ;
private GsonUtils (){}
public static GsonUtils getInstence (){
if (null == GSONUTILS){
synchronized (GsonUtils.class){
if (null == GSONUTILS){
GSONUTILS = new GsonUtils();
}
}
}
return GSONUTILS;
}
public Gson getGsonInstance (){
return new Gson();
}
}
Bean类
package com.bwie.shoppcardemo;
import java.util.List;
/**
* Created by stephen on 13/12/2017.
*/
public class ShoppingBean {
private String msg;
private String code;
private List<DataBean> data;
public String getMsg () {
return msg;
}
public void setMsg (String msg) {
this .msg = msg;
}
public String getCode () {
return code;
}
public void setCode (String code) {
this .code = code;
}
public List<DataBean> getData () {
return data;
}
public void setData (List<DataBean> data) {
this .data = data;
}
public static class DataBean {
private String sellerName;
private String sellerid;
private List<ListBean> list;
public String getSellerName () {
return sellerName;
}
public void setSellerName (String sellerName) {
this .sellerName = sellerName;
}
public String getSellerid () {
return sellerid;
}
public void setSellerid (String sellerid) {
this .sellerid = sellerid;
}
public List<ListBean> getList () {
return list;
}
public void setList (List<ListBean> list) {
this .list = list;
}
public static class ListBean {
private int bargainPrice;
private String createtime;
private String detailUrl;
private String images;
private int num;
private int pid;
private int price;
private int pscid;
private int selected;
private int sellerid;
private String subhead;
private String title;
public boolean isChoosed;
public boolean isCheck = false ;
public boolean isChoosed () {
return isChoosed;
}
public void setChoosed (boolean choosed) {
isChoosed = choosed;
}
public boolean isCheck () {
return isCheck;
}
public void setCheck (boolean check) {
isCheck = check;
}
public int getBargainPrice () {
return bargainPrice;
}
public void setBargainPrice (int bargainPrice) {
this .bargainPrice = bargainPrice;
}
public String getCreatetime () {
return createtime;
}
public void setCreatetime (String createtime) {
this .createtime = createtime;
}
public String getDetailUrl () {
return detailUrl;
}
public void setDetailUrl (String detailUrl) {
this .detailUrl = detailUrl;
}
public String getImages () {
return images;
}
public void setImages (String images) {
this .images = images;
}
public int getNum () {
return num;
}
public void setNum (int num) {
this .num = num;
}
public int getPid () {
return pid;
}
public void setPid (int pid) {
this .pid = pid;
}
public int getPrice () {
return price;
}
public void setPrice (int price) {
this .price = price;
}
public int getPscid () {
return pscid;
}
public void setPscid (int pscid) {
this .pscid = pscid;
}
public int getSelected () {
return selected;
}
public void setSelected (int selected) {
this .selected = selected;
}
public int getSellerid () {
return sellerid;
}
public void setSellerid (int sellerid) {
this .sellerid = sellerid;
}
public String getSubhead () {
return subhead;
}
public void setSubhead (String subhead) {
this .subhead = subhead;
}
public String getTitle () {
return title;
}
public void setTitle (String title) {
this .title = title;
}
}
}
}
购物车布局
<?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" >
<CheckBox
android:id ="@+id/ck_chose"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:layout_centerVertical ="true"
android:layout_marginLeft ="5dp"
android:scaleX ="0.6"
android:scaleY ="0.6" />
<ImageView
android:id ="@+id/iv_show_pic"
android:layout_width ="70dp"
android:layout_height ="80dp"
android:layout_centerVertical ="true"
android:layout_marginLeft ="5dp"
android:background ="@mipmap/ic_launcher"
android:layout_toRightOf ="@id/ck_chose" />
<LinearLayout
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:layout_marginLeft ="10dp"
android:layout_marginTop ="15dp"
android:layout_toRightOf ="@id/iv_show_pic"
android:orientation ="vertical" >
<TextView
android:id ="@+id/tv_commodity_name"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:text ="酒红色纯红色纯羊毛西服套装"
android:textColor ="@android:color/black"
android:textSize ="12sp"
android:textStyle ="bold" />
<LinearLayout
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:orientation ="horizontal" >
<TextView
android:id ="@+id/tv_commodity_attr"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:layout_marginTop ="3dp"
android:text ="属性:粉蓝色"
android:textSize ="12sp"
android:textColor ="@color/colorPrimary" />
</LinearLayout >
<LinearLayout
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:layout_marginTop ="4dp"
android:orientation ="horizontal" >
<TextView
android:id ="@+id/tv_commodity_price"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:text ="¥390"
android:textColor ="@android:color/holo_red_dark"
android:textSize ="12sp"
android:textStyle ="bold" />
<TextView
android:id ="@+id/tv_commodity_num"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:layout_marginLeft ="20dp"
android:text ="x1"
android:textColor ="@android:color/darker_gray" />
<LinearLayout
android:id ="@+id/rl_edit"
android:layout_width ="120dp"
android:background ="@android:color/holo_orange_light"
android:layout_height ="30dp"
android:layout_marginLeft ="20dp"
>
<TextView
android:id ="@+id/iv_sub"
android:layout_width ="0dp"
android:layout_weight ="1"
android:gravity ="center"
android:textColor ="@android:color/black"
android:background ="@android:color/white"
android:layout_margin ="1dp"
android:layout_height ="match_parent"
android:text =" - " />
<TextView
android:id ="@+id/tv_commodity_show_num"
android:layout_width ="0dp"
android:layout_weight ="1"
android:gravity ="center"
android:background ="@android:color/white"
android:layout_margin ="1dp"
android:layout_height ="match_parent"
android:text ="1"
/>
<TextView
android:id ="@+id/iv_add"
android:layout_width ="0dp"
android:layout_weight ="1"
android:gravity ="center"
android:layout_margin ="1dp"
android:background ="@android:color/white"
android:layout_height ="match_parent"
android:text =" + " />
</LinearLayout >
</LinearLayout >
</LinearLayout >
<Button
android:id ="@+id/btn_commodity_delete"
android:layout_width ="30dp"
android:layout_height ="30dp"
android:layout_alignParentRight ="true"
android:layout_centerVertical ="true"
android:gravity ="center"
android:text ="x"
android:background ="@android:color/holo_blue_light"
android:textSize ="20sp"
android:textColor ="@android:color/holo_green_dark"
android:layout_margin ="5dp"
android:visibility ="visible" />
</RelativeLayout >
主界面布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation ="vertical"
tools:context ="com.bwie.shoppcardemo.MainActivity" >
<include layout ="@layout/layout_tile" >
</include >
<android.support.v7.widget.RecyclerView
android:id ="@+id/carListView"
android:layout_width ="match_parent"
android:layout_height ="match_parent"
android:layout_weight ="1" >
</android.support.v7.widget.RecyclerView >
<View
android:layout_width ="match_parent"
android:layout_height ="0.5dp"
android:background ="@android:color/background_dark" />
<include layout ="@layout/layot_shopping_car_bottom" >
</include >
</LinearLayout >
购物车下面布局
<?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 ="horizontal" >
<CheckBox
android:id ="@+id/chooseAll"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:textSize ="25sp"
android:padding ="10dp"
android:text ="全选" />
<TextView
android:id ="@+id/totalPrice"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:textSize ="25sp"
android:layout_weight ="1"
android:padding ="10dp"
android:gravity ="center"
android:text ="合计:0.00 ¥" />
<TextView
android:id ="@+id/btnAmount"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:text ="结算 : (0)"
android:gravity ="center"
android:background ="@android:color/holo_orange_light"
android:textColor ="@android:color/black"
android:textSize ="25sp"
android:padding ="10dp" />
</LinearLayout >
购物车头
<?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/colorAccent"
android:orientation ="horizontal" >
<TextView
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:textSize ="25sp"
android:padding ="10dp"
android:id ="@+id/btnBack"
android:text ="返回" />
<TextView
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:text ="购物车"
android:textSize ="25sp"
android:layout_weight ="1"
android:gravity ="center"
android:padding ="10dp"
android:layout_gravity ="center_horizontal" />
<TextView
android:padding ="10dp"
android:textSize ="25sp"
android:id ="@+id/btnEditor"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:text ="编辑" />
</LinearLayout >