购物车json实现

shop.json文件

{  "code" : 200 ,
  "orderData" : [
    {

      "shopId": 1,
      "shopName":"京东自营",
      "cartlist": [
        {
          "id": 1,
          "shopId": 1,
          "shopName": "京东自营",
          "defaultPic": "https://img30.360buyimg.com/popWareDetail/jfs/t3208/194/7616404169/244198/369625db/58b7d093N03520fb7.jpg",
          "productId": 1,
          "productName": "三只松鼠_零食大礼包",
          "color": "黑色",
          "size": "18L",
          "price": 20,
          "count":1
        },
        {
          "id": 2,
          "shopId": 1,
          "shopName": "京东自营",
          "defaultPic": "https://img14.360buyimg.com/n0/jfs/t2971/15/167732091/93002/204c1016/574d9d9aNe4e6fa7a.jpg",
          "productId": 2,
          "productName": "小米心跳手环",
          "color": "白色",
          "size": "20XXL",
          "price": 148,
          "count": 1
        }
      ]
    }
  ,
    {
      "shopId": 2,
      "shopName":"海澜之家",
      "cartlist": [
        {
          "id": 1,
          "shopId": 2,
          "shopName": "海澜之家",
          "defaultPic": "https://img30.360buyimg.com/popWaterMark/jfs/t4075/83/1343091204/132469/9034cb9c/5873496bN68020ba8.jpg",
          "productId": 1,
          "productName": "短袖T恤男 2017夏季新品",
          "color": "蓝色",
          "size": "30X",
          "price": 181,
          "count":1
        }
      ]
    }
  ,
    {
      "shopId": 3,
      "shopName":"OPPO官方旗舰店",
      "cartlist": [
        {
          "id": 1,
          "shopId": 3,
          "shopName": "OPPO官方旗舰店",
          "defaultPic": "https://img10.360buyimg.com/cms/jfs/t6064/272/2163314583/157700/442d6477/593c1c49N7c63a7d9.jpg",
          "productId": 1,
          "productName": "OPPO R11 全网通",
          "color": "蓝色",
          "size": "30X",
          "price": 1999,
          "count":1
        },
        {
          "id": 2,
          "shopId": 3,
          "shopName": "OPPO官方旗舰店",
          "defaultPic": "https://img14.360buyimg.com/n0/jfs/t3142/194/4953241722/254855/1651c2b1/585b9021Nf653e48a.jpg",
          "productId": 1,
          "productName": "OPPO R9 全网通",
          "color": "蓝色",
          "size": "30X",
          "price": 999,
          "count":1
        }
      ]
    }

  ]
}
首先要创建一个自定义加减控件CustomView

package com.qizu.shoppingcarcar;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

/**
 * Created by jiuhao on 2017/11/16.
 * data 2017/11/16
 * time 下午 06:35
 */

public class CustomView extends LinearLayout{
    private EditText editText;
    private Button revserse;
    private Button add;
    private int mCount = 1 ;

    public CustomView(Context context) {
        super(context);
    }

    public CustomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        View view =   LayoutInflater.from(context).inflate(R.layout.customview,null,false);
        revserse = (Button) view.findViewById(R.id.revserse);
        add = (Button) view.findViewById(R.id.add);
        editText = (EditText) view.findViewById(R.id.content);
        revserse.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //减号
                try {
                    String content = editText.getText().toString().trim();
                    int count = Integer.valueOf(content)-1;
                    mCount = count;
                    if(count>=1){
                        editText.setText(count+"");
                    }
                    if(listener != null){
                        listener.click(count);
                    }
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
        });
        add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //加号
                try {
                    String content = editText.getText().toString().trim();
                    int count = Integer.valueOf(content)+1;
                    mCount = count;
                    editText.setText(count+"");
                    if(listener != null){
                        listener.click(count);
                    }

                } catch (NumberFormatException e) {
                    e.printStackTrace();


                }
            }
        });
        addView(view);
    }
    public int getCurrentCount(){
        return mCount;
    }

    public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public ClickListener listener;
    public void setListener(ClickListener listener){
        this.listener = listener;
    }
    public interface ClickListener {
        public void click(int count);
    }
}
创建的Bean类
package com.qizu.shoppingcarcar;

import java.util.List;

/**
 * Created by jiuhao on 2017/11/17.
 * data 2017/11/17
 * time 下午 03:13
 */

public class MyBean {

    /**
     * msg : 请求成功
     * code : 0
     * data : [{"list":[{"bargainPrice":22.9,"createtime":"2017-10-14T21:48:08","detailUrl":"https://item.m.jd.com/product/2542855.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t1930/284/2865629620/390243/e3ade9c4/56f0a08fNbd3a1235.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2137/336/2802996626/155915/e5e90d7a/56f0a09cN33e01bd0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t1882/31/2772215910/389956/c8dbf370/56f0a0a2Na0c86ea6.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2620/166/2703833710/312660/531aa913/57709035N33857877.jpg!q70.jpg","num":2,"pid":24,"price":288,"pscid":2,"selected":0,"sellerid":1,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋"}],"sellerName":"商家1","sellerid":"1"},{"list":[{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/5025518.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8830/106/1760940277/195595/5cf9412f/59bf2ef5N5ab7dc16.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5428/70/1520969931/274676/b644dd0d/591128e7Nd2f70da0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5566/365/1519564203/36911/620c750c/591128eaN54ac3363.jpg!q70.jpg","num":1,"pid":58,"price":6399,"pscid":40,"selected":0,"sellerid":2,"subhead":"升级4G大显存!Nvme协议Pcie SSD,速度快人一步】GTX1050Ti就选拯救者!专业游戏键盘&新模具全新设计!","title":"联想(Lenovo)拯救者R720 15.6英寸游戏笔记本电脑(i5-7300HQ 8G 1T+128G SSD GTX1050Ti 4G IPS 黑)"},{"bargainPrice":6666,"createtime":"2017-10-10T16:01:31","detailUrl":"https://item.m.jd.com/product/5089273.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8284/363/1326459580/71585/6d3e8013/59b857f2N6ca75622.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t9346/182/1406837243/282106/68af5b54/59b8480aNe8af7f5c.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8434/54/1359766007/56140/579509d9/59b85801Nfea207db.jpg!q70.jpg","num":1,"pid":46,"price":234,"pscid":39,"selected":0,"sellerid":2,"subhead":"【iPhone新品上市】新一代iPhone,让智能看起来更不一样","title":"Apple iPhone 8 Plus (A1864) 64GB 金色 移动联通电信4G手机"}],"sellerName":"商家2","sellerid":"2"},{"list":[{"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":82,"pid":10,"price":555.55,"pscid":1,"selected":0,"sellerid":3,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家3","sellerid":"3"},{"list":[{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","detailUrl":"https://mitem.jd.hk/ware/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1","images":"https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5815/178/2614671118/51656/7f52d137/593276c7N107b725a.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5878/60/2557817477/30873/4502b606/593276caN5a7d6357.jpg!q70.jpg","num":1,"pid":61,"price":14999,"pscid":40,"selected":0,"sellerid":5,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家5","sellerid":"5"},{"list":[{"bargainPrice":159,"createtime":"2017-10-14T21:49:15","detailUrl":"https://item.m.jd.com/product/5061723.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8716/197/1271594444/173291/2f40bb4f/59b743bcN8509428e.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8347/264/1286771527/92188/5cf5ec04/59b7420fN65378e9e.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7363/165/3000956253/190883/179a372/59b743bfNd0c79d93.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7399/112/2935531768/183594/b77c7d4a/59b7441aNc3d40133.jpg!q70.jpg","num":1,"pid":100,"price":2200,"pscid":112,"selected":0,"sellerid":11,"subhead":"针织针织闪闪闪亮你的眼","title":"维迩旎 2017秋冬新款长袖针织连衣裙韩版气质中长款名媛包臀A字裙 zx179709 黑色 XL"}],"sellerName":"商家11","sellerid":"11"},{"list":[{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","detailUrl":"https://mitem.jd.hk/ware/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1","images":"https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5815/178/2614671118/51656/7f52d137/593276c7N107b725a.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5878/60/2557817477/30873/4502b606/593276caN5a7d6357.jpg!q70.jpg","num":1,"pid":69,"price":16999,"pscid":40,"selected":0,"sellerid":13,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家13","sellerid":"13"},{"list":[{"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":1,"pid":1,"price":118,"pscid":1,"selected":0,"sellerid":17,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家17","sellerid":"17"},{"list":[{"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":1,"pid":2,"price":299,"pscid":1,"selected":0,"sellerid":18,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家18","sellerid":"18"}]
     */

    private String msg;
    private String code;
    private List 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 getData() {
        return data;
    }

    public void setData(List data) {
        this.data = data;
    }

    public static class DataBean {
        /**
         * list : [{"bargainPrice":22.9,"createtime":"2017-10-14T21:48:08","detailUrl":"https://item.m.jd.com/product/2542855.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t1930/284/2865629620/390243/e3ade9c4/56f0a08fNbd3a1235.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2137/336/2802996626/155915/e5e90d7a/56f0a09cN33e01bd0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t1882/31/2772215910/389956/c8dbf370/56f0a0a2Na0c86ea6.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2620/166/2703833710/312660/531aa913/57709035N33857877.jpg!q70.jpg","num":2,"pid":24,"price":288,"pscid":2,"selected":0,"sellerid":1,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋"}]
         * sellerName : 商家1
         * sellerid : 1
         */

        private String sellerName;
        private String sellerid;
        private List 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 getList() {
            return list;
        }

        public void setList(List list) {
            this.list = list;
        }

        public static class ListBean {
            /**
             * bargainPrice : 22.9
             * createtime : 2017-10-14T21:48:08
             * detailUrl : https://item.m.jd.com/product/2542855.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends
             * images : https://m.360buyimg.com/n0/jfs/t1930/284/2865629620/390243/e3ade9c4/56f0a08fNbd3a1235.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2137/336/2802996626/155915/e5e90d7a/56f0a09cN33e01bd0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t1882/31/2772215910/389956/c8dbf370/56f0a0a2Na0c86ea6.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2620/166/2703833710/312660/531aa913/57709035N33857877.jpg!q70.jpg
             * num : 2
             * pid : 24
             * price : 288
             * pscid : 2
             * selected : 0
             * sellerid : 1
             * subhead : 三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》
             * title : 三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋
             */

            private double 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 double getBargainPrice() {
                return bargainPrice;
            }

            public void setBargainPrice(double 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;
            }
        }
    }
}
adapter类
 
  
package com.qizu.shoppingcarcar;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by jiuhao on 2017/11/16.
 * data 2017/11/16
 * time 下午 06:36
 */

public class ShopAdapter extends RecyclerView.Adapter {


    private Context context;
    private List list;

    public ShopAdapter(Context context) {
        this.context = context;
    }

    public void add(List list) {
        if (this.list == null) {
            this.list = new ArrayList<>();
        }
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public ShopAdapter.IViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.shop_adapter, null);
        return new IViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final IViewHolder holder, final int position) {
        holder.checkbox.setChecked(list.get(position).isCheck());
        holder.danjia.setText(list.get(position).getPrice()+"");
        ImageLoader.getInstance().displayImage(list.get(position).getDefaultPic(),holder.shopface);
        holder.checkbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list.get(position).setCheck(holder.checkbox.isChecked());
                notifyDataSetChanged();

                if(checkBoxListener != null){
                    checkBoxListener.check(position,holder.customviewid.getCurrentCount(),holder.checkbox.isChecked(),list);
                }
            }
        });
        holder.customviewid.setListener(new CustomView.ClickListener() {
            @Override
            public void click(int count) {
                list.get(position).setCount(count);
                notifyDataSetChanged();
                if(listener != null){
                    listener.click(count,list);
                }
            }
        });

        holder.shopBtnDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                list.remove(position);
                notifyDataSetChanged();
                if(delListener != null){
                    delListener.del(position,list);
                }
            }
        });

    }


    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class IViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.checkbox)
        CheckBox checkbox;
        @BindView(R.id.shopface)
        ImageView shopface;
        @BindView(R.id.danjia)
        TextView danjia;
        @BindView(R.id.customviewid)
        CustomView customviewid;
        @BindView(R.id.shop_btn_del)
        Button shopBtnDel;
        public IViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public List getList(){
        return list;
    }
    CheckBoxListener checkBoxListener;


    public void setCheckBoxListener(CheckBoxListener listener){
        this.checkBoxListener = listener;
    }
    interface CheckBoxListener {
        public void check(int position,int count, boolean check,List list);
    }

    CustomViewListener listener;
    public void setCustomViewListener(CustomViewListener listener){
        this.listener = listener;
    }

    interface CustomViewListener{
        public void click(int count,List list);
    }



    DelListener delListener;
    public  void setDelListener(DelListener listener){
        this.delListener = listener;
    }
    interface DelListener{
        public void del(int position,List list);
    }

}

在主方法里面改的是
 
  
package com.qizu.shoppingcarcar;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.third_recyclerview)
    RecyclerView thirdRecyclerview;
    @BindView(R.id.third_allselect)
    TextView thirdAllselect;
    @BindView(R.id.third_totalprice)
    TextView thirdTotalprice;
    @BindView(R.id.third_totalnum)
    TextView thirdTotalnum;
    @BindView(R.id.third_submit)
    TextView thirdSubmit;
    @BindView(R.id.third_pay_linear)
    LinearLayout thirdPayLinear;
    private List mAllOrderList = new ArrayList<>();
    private ShopAdapter adapter;
    private GridLayoutManager manager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        getData();
        thirdAllselect.setTag(1);
        manager = new GridLayoutManager(this,2, LinearLayoutManager.VERTICAL, false);
        adapter = new ShopAdapter(this);
        thirdRecyclerview.setLayoutManager(manager);
        thirdRecyclerview.setAdapter(adapter);
        adapter.add(mAllOrderList);
        adapter.setCheckBoxListener(new ShopAdapter.CheckBoxListener() {
            @Override
            public void check(int position, int count, boolean check,List list) {

                sum(list);
            }
        });

        adapter.setCustomViewListener(new ShopAdapter.CustomViewListener() {
            @Override
            public void click(int count,List list) {
                sum(list);
            }
        });

        adapter.setDelListener(new ShopAdapter.DelListener() {
            @Override
            public void del(int position,List list) {
                sum(list);
            }
        });
    }
    float price = 0;
    int count;
    private void sum(List mAllOrderList) {
        price = 0;
        count = 0;

        boolean allCheck = true ;
        for (ShopBean.OrderDataBean.CartlistBean bean : mAllOrderList) {
            if (bean.isCheck()) {
                //得到总价
                price += bean.getPrice() * bean.getCount();
                //得到商品个数
                count += bean.getCount();
            }else {
                // 只要有一个商品未选中,全选按钮 应该设置成 为选中
                allCheck = false;
            }
        }

        thirdTotalprice.setText("总价: " + price);
        thirdTotalnum.setText("共" + count + "件商品");

        if(allCheck){
            thirdAllselect.setTag(2);
            thirdAllselect.setBackgroundResource(R.drawable.shopcart_selected);
        }else {
            thirdAllselect.setTag(1);
            thirdAllselect.setBackgroundResource(R.drawable.shopcart_unselected);
        }

    }
    public void getData() {
        try {
            //模拟网络请求
            InputStream inputStream = getAssets().open("shop.json");
            String data = convertStreamToString(inputStream);
            Gson gson = new Gson();
            ShopBean shopBean = gson.fromJson(data, ShopBean.class);


            for (int i = 0; i < shopBean.getOrderData().size(); i++) {
                int length = shopBean.getOrderData().get(i).getCartlist().size();
                for (int j = 0; j < length; j++) {
                    mAllOrderList.add(shopBean.getOrderData().get(i).getCartlist().get(j));
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static String convertStreamToString(InputStream is) {
        /*
          * To convert the InputStream to String we use the BufferedReader.readLine()
          * method. We iterate until the BufferedReader return null which means
          * there's no more data to read. Each line will appended to a StringBuilder
          * and returned as String.
          */
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return sb.toString();
    }

    boolean select = false ;
    @OnClick(R.id.third_allselect)
    public void onClick() {
        //全选按钮 点击事件
        int tag = (Integer) thirdAllselect.getTag() ;
        if(tag ==1){
            thirdAllselect.setTag(2);
            select = true;
        } else {
            thirdAllselect.setTag(1);
            select = false;
        }
        for (ShopBean.OrderDataBean.CartlistBean bean : mAllOrderList) {
            bean.setCheck(select);
        }
        adapter.notifyDataSetChanged();
        sum(adapter.getList());





    }
}
接下来就是要粘贴主布局了
 
  
xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.qizu.shoppingcarcar.MainActivity">

            android:id="@+id/third_recyclerview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />




            android:layout_weight="0"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#FFFFFF"
        android:gravity="center_vertical"
        android:id="@+id/third_pay_linear"
        >

                    android:id="@+id/third_allselect"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/margin_10dp"
            android:drawableLeft="@drawable/shopcart_unselected"
            android:text="全选"
            android:drawablePadding="@dimen/padding_5dp"
            />

                    android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical"
            >

                            android:id="@+id/third_totalprice"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:paddingLeft="@dimen/padding_10dp"
                android:paddingTop="@dimen/padding_10dp"
                android:text="总价:"
                android:textColor="#000000"
                android:textSize="@dimen/common_font_size_16"
                />

                            android:textColor="#000000"
                android:id="@+id/third_totalnum"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="@dimen/padding_10dp"
                android:text="共0件商品"
                android:textSize="@dimen/common_font_size_14"
                android:paddingBottom="@dimen/padding_10dp"
                />

        

                    android:id="@+id/third_submit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/login_btn"
            android:text="去结算"
            android:paddingLeft="@dimen/margin_30dp"
            android:paddingRight="@dimen/margin_30dp"
            android:paddingTop="@dimen/padding_10dp"
            android:paddingBottom="@dimen/padding_10dp"
            android:textColor="#000000"
            android:layout_marginRight="@dimen/margin_10dp"
            />

    



自定义控件的布局
 
  
xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">


    

适配器的item布局
 
  
xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

            android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />


            android:layout_width="match_parent"
        android:layout_height="wrap_content" android:orientation="vertical">


                    android:id="@+id/shopface"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@mipmap/ic_launcher"/>

                    android:layout_width="match_parent"
            android:layout_height="wrap_content" android:orientation="vertical">


                            android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/danjia"/>

                            android:id="@+id/customviewid"
                android:layout_width="100dp"
                android:layout_height="50dp"/>

        


        
color
 
  
xml version="1.0" encoding="utf-8"?>

    name="colorPrimary">#3F51B5
    name="colorPrimaryDark">#303F9F
    name="colorAccent">#FF4081

    name="cwhite">#FFFFFF

    name="title_bg">#FDE23D

    name="tab_bg">#FFFFFF

    name="tab_normal_textcolor">#373737
    name="tab_selected_textcolor">#FDE23D

    name="coffer">#442509

    name="pressed_icon_color">#e53e42

    name="background_color">#f6f6f6

    name="main_red_text">#e53e42
    name="padding_20dp">20dp

    name="splitline_color">#dddddd

    name="cblack">#000000


dinmens
 
  
xml version="1.0" encoding="utf-8"?>

    <dimen name="margin_10dp">10dpdimen>


    <dimen name="padding_5dp">5dpdimen>
    <dimen name="padding_10dp">10dpdimen>


    <dimen name="common_font_size_16">16spdimen>
    <dimen name="common_font_size_14">14spdimen>


    <dimen name="height_200dp">200dpdimen>

    <dimen name="margin_30dp">30dpdimen>
    <dimen name="margin_15dp">15dpdimen>
    <dimen name="margin_1dp">1dpdimen>
    <dimen name="margin_5dp">5dpdimen>
    <dimen name="common_font_size_12">12spdimen>

    <dimen name="padding_2dp">2dpdimen>
    <dimen name="margin_20dp">20dpdimen>
权限
 
  
android:name="android.permission.READ_EXTERNAL_STORAGE" />
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
android:name="android.permission.INTERNET" />


android:name="android.permission.ACCESS_COARSE_LOCATION" />

android:name="android.permission.ACCESS_FINE_LOCATION" />

android:name="android.permission.ACCESS_NETWORK_STATE" />

android:name="android.permission.ACCESS_WIFI_STATE" />

android:name="android.permission.CHANGE_WIFI_STATE" />

android:name="android.permission.INTERNET" />

android:name="android.permission.READ_PHONE_STATE" />

android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />

android:name="android.permission.BLUETOOTH" />
android:name="android.permission.BLUETOOTH_ADMIN" />

依赖
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'
testCompile 'junit:junit:4.12'
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
compile 'com.jakewharton:butterknife-compiler:8.8.1'
compile 'com.jakewharton:butterknife:8.8.1'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
compile 'com.google.code.gson:gson:2.8.2'

你可能感兴趣的:(购物车json实现)