一级购物car






import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import com.example.gouwuche.Bean.JavaBean;
import com.example.gouwuche.adapter.ShowAdapter;
import com.example.gouwuche.presenter.INewsPresenter;
import com.example.gouwuche.utils.MessageEvent;
import com.example.gouwuche.utils.PriceAndCountEvent;
import com.example.gouwuche.view.INewsView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements INewsView {

    private List<JavaBean.DataBean> list = new ArrayList<>();
    private RecyclerView recy_view;
    private CheckBox cb_all;
    private TextView tv_sum;
    private TextView tv_count;
    private Button btn_jiesuan;
    private ShowAdapter adapter;
    private INewsPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

    }

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    private void initView() {
        recy_view = (RecyclerView) findViewById(R.id.recy_view);
        cb_all = (CheckBox) findViewById(R.id.cb_all);
        tv_sum = (TextView) findViewById(R.id.tv_sum);
        tv_count = (TextView) findViewById(R.id.tv_count);
        btn_jiesuan = (Button) findViewById(R.id.btn_jiesuan);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        recy_view.setLayoutManager(manager);
        adapter = new ShowAdapter(this, list);
        recy_view.setAdapter(adapter);
        presenter = new INewsPresenter();
        presenter.attachView(this);
        presenter.getNews();
        cb_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                adapter.changeAllListCbState(cb_all.isChecked());
            }
        });
    }

    @Override
    public void success(String tag, List<JavaBean.DataBean> news) {
        list.addAll(news);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void failed(String tag, Exception e) {

    }

    @Subscribe
    public void onMessageEvent(MessageEvent event) {
        cb_all.setChecked(event.isChecked());
    }

    @Subscribe
    public void onMessageEvent(PriceAndCountEvent event) {
        tv_count.setText("共" + event.getCount() + "件");
        tv_sum.setText("¥" + event.getPrice());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        if (presenter != null) {
            presenter.detachView();
        }
    }
}




ShowAdapter 适配器
package com.example.gouwuche.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
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.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
import com.bumptech.glide.Glide;
import com.example.gouwuche.Bean.JavaBean;
import com.example.gouwuche.R;
import com.example.gouwuche.utils.MessageEvent;
import com.example.gouwuche.utils.PriceAndCountEvent;

import org.greenrobot.eventbus.EventBus;
import java.util.List;

/**
 * Created by 知足 on 2017/12/16.
 */

public class ShowAdapter extends RecyclerView.Adapter<ShowAdapter.ViewHolder> {
    private Context context;
    private List<JavaBean.DataBean> list;


    public ShowAdapter(Context context, List<JavaBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.item, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        String[] split = list.get(position).getImages().split("[|]");
        Glide.with(context).load(split[0]).into(holder.item_img);
        holder.item_price.setText(list.get(position).getPrice()+"");
        holder.item_title.setText(list.get(position).getTitle());
        holder.et_num.setText(list.get(position).getPid()+"");
        holder.item_cb.setChecked(list.get(position).isCheckbox());
        holder.child_sum.setText(list.get(position).getPid()*list.get(position).getPrice()+"");
        holder.child_num.setText(list.get(position).getPid()+"");
        holder.item_cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                list.get(position).setCheckbox(holder.item_cb.isChecked());
                PriceAndCountEvent priceAndCountEvent = compute();
                EventBus.getDefault().post(priceAndCountEvent);

                if (holder.item_cb.isChecked()) {
                    changGroupCbState(position, true);
                    changeAllCbState(isAllGroupCbSelected());
                } else {
                    changGroupCbState(position, false);
                    changeAllCbState(isAllGroupCbSelected());
                }
                notifyDataSetChanged();
            }
        });
        //加号
        holder.bt_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = list.get(position).getPid();
                holder.et_num.setText(++num + "");
                list.get(position).setPid(num);
                holder.child_sum.setText(list.get(position).getPid()*list.get(position).getPrice()+"");
                holder.child_num.setText(list.get(position).getPid()+"");
                if (holder.item_cb.isChecked()) {
                    EventBus.getDefault().post(compute());
                }
            }
        });
        //减号
        holder.bt_reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = list.get(position).getPid();
                if (num == 1) {
                    return;
                }
                holder.et_num.setText(--num + "");
                list.get(position).setPid(num);
                holder.child_sum.setText(list.get(position).getPid()*list.get(position).getPrice()+"");
                holder.child_num.setText(list.get(position).getPid()+"");
                if (holder.item_cb.isChecked()) {
                    EventBus.getDefault().post(compute());
                }
            }
        });
        //删除
        holder.item_delete.setOnClickListener(new View.OnClickListener() {

            private AlertDialog dialog;

            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("提示");
                builder.setMessage("确认是否删除?");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        list.remove(position);
                        EventBus.getDefault().post(compute());
                        notifyDataSetChanged();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialog.dismiss();
                    }
                });
                dialog = builder.create();
                dialog.show();

            }
        });


    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{

        private final CheckBox item_cb;
        private final ImageView item_img;
        private final TextView item_title;
        private final TextView item_price;

        private final TextView child_num;
        private final TextView child_sum;
        private final Button item_delete;
        private final Button bt_add;
        private final Button bt_reduce;
        private final EditText et_num;

        public ViewHolder(View itemView) {
            super(itemView);
            item_cb = itemView.findViewById(R.id.item_cb);
            item_img = itemView.findViewById(R.id.item_img);
            item_title = itemView.findViewById(R.id.item_title);
            item_price = itemView.findViewById(R.id.item_price);
            child_num = itemView.findViewById(R.id.child_num);
            child_sum = itemView.findViewById(R.id.child_sum);
            item_delete = itemView.findViewById(R.id.item_delete);
            bt_add = itemView.findViewById(R.id.bt_add);
            bt_reduce = itemView.findViewById(R.id.bt_reduce);
            et_num = itemView.findViewById(R.id.et_num);
        }
    }
    /**
     * 改变全选的状态
     *
     * @param flag
     */
    private void changeAllCbState(boolean flag) {
        MessageEvent messageEvent = new MessageEvent();
        messageEvent.setChecked(flag);
        EventBus.getDefault().post(messageEvent);
    }
    /**
     * 计算列表中,选中的钱和数量
     */
    private PriceAndCountEvent compute() {
        int count = 0;
        int price = 0;
        for (int i = 0; i < list.size(); i++) {
            JavaBean.DataBean dataBean = list.get(i);

            if (dataBean.isCheckbox()) {
                price += dataBean.getPid() * dataBean.getPrice();
                count += dataBean.getPid();
            }

        }
        PriceAndCountEvent priceAndCountEvent = new PriceAndCountEvent();
        priceAndCountEvent.setCount(count);
        priceAndCountEvent.setPrice(price);
        return priceAndCountEvent;
    }
    /**
     * 判断一级列表是否全部选中
     *
     * @return
     */
    private boolean isAllGroupCbSelected() {
        for (int i = 0; i < list.size(); i++) {
            JavaBean.DataBean dataBean = list.get(i);

            if (!dataBean.isCheckbox()) {
                return false;
            }
        }
        return true;
    }
    /**
     * 改变一级列表checkbox状态
     *
     * @param groupPosition
     */
    private void changGroupCbState(int groupPosition, boolean flag) {
        JavaBean.DataBean dataBean = list.get(groupPosition);

        dataBean.setCheckbox(flag);
    }
    /**
     * 判断列表是否全部选中
     *
     *
     * @return
     */
    private boolean isAllChildCbSelected() {
        for (int i = 0;i<list.size();i++){
            JavaBean.DataBean dataBean = list.get(i);
            if (!dataBean.isCheckbox()) {
                return false;
            }
        }
        return true;
    }
    /**
     * 设置全选、反选
     *
     * @param flag
     */
    public void changeAllListCbState(boolean flag) {
        for (int i = 0; i < list.size(); i++) {
            changGroupCbState(i, flag);
        }
        EventBus.getDefault().post(compute());
        notifyDataSetChanged();
    }
}



JavaBean包

/**
 * msg : 查询成功
 * code : 0
 * data : [{"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","itemtype":0,"pid":46,"price":234,"pscid":39,"salenum":868,"sellerid":2,"subhead":"【iPhone新品上市】新一代iPhone,让智能看起来更不一样","title":"Apple iPhone 8 Plus (A1864) 64GB 金色 移动联通电信4G手机"},{"bargainPrice":11800,"createtime":"2017-10-10T17:33:37","detailUrl":"https://item.m.jd.com/product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg","itemtype":0,"pid":57,"price":5199,"pscid":40,"salenum":4343,"sellerid":1,"subhead":"【i5 MX150 2G显存】全高清窄边框 8G内存 256固态硬盘 支持指纹识别 预装WIN10系统","title":"小米(MI)Air 13.3英寸全金属轻薄笔记本(i5-7200U 8G 256G PCle SSD MX150 2G独显 FHD 指纹识别 Win10)银\r\n"},{"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","itemtype":1,"pid":58,"price":6399,"pscid":40,"salenum":545,"sellerid":2,"subhead":"升级4G大显存!Nvme协议Pcie SSD,速度快人一步】GTX1050Ti就选拯救者!专业游戏键盘&新模具全新设计!","title":"联想(Lenovo)拯救者R720 15.6英寸游戏笔记本电脑(i5-7300HQ 8G 1T+128G SSD GTX1050Ti 4G IPS 黑)"},{"bargainPrice":5599,"createtime":"2017-10-10T17:30:32","detailUrl":"https://item.m.jd.com/product/4824715.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n12/jfs/t7768/184/1153704394/148460/f42e1432/599a930fN8a85626b.jpg!q70.jpg","itemtype":0,"pid":59,"price":5599,"pscid":40,"salenum":675,"sellerid":3,"subhead":"游戏本选择4G独显,拒绝掉帧】升级版IPS全高清防眩光显示屏,WASD方向键颜色加持,三大出风口立体散热!","title":"戴尔DELL灵越游匣15PR-6648B GTX1050 15.6英寸游戏笔记本电脑(i5-7300HQ 8G 128GSSD+1T 4G独显 IPS)黑"},{"bargainPrice":11800,"createtime":"2017-10-14T21:48:08","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","itemtype":2,"pid":60,"price":13888,"pscid":40,"salenum":466,"sellerid":4,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"},{"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","itemtype":1,"pid":61,"price":14999,"pscid":40,"salenum":5535,"sellerid":5,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"},{"bargainPrice":11800,"createtime":"2017-10-03T23:53:28","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","itemtype":0,"pid":62,"price":15999,"pscid":40,"salenum":43,"sellerid":6,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"},{"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","itemtype":1,"pid":63,"price":10000,"pscid":40,"salenum":3232,"sellerid":7,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"},{"bargainPrice":11800,"createtime":"2017-10-03T23:43:53","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","itemtype":0,"pid":64,"price":11000,"pscid":40,"salenum":0,"sellerid":8,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"},{"bargainPrice":11800,"createtime":"2017-10-14T21:48:08","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","itemtype":2,"pid":65,"price":12000,"pscid":40,"salenum":868,"sellerid":9,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}]
 * page : 1
 */

private String msg;
private String code;
private String page;
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 String getPage() {
    return page;
}

public void setPage(String page) {
    this.page = page;
}

public List<DataBean> getData() {
    return data;
}

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

public static class DataBean {
    /**
     * bargainPrice : 6666.0
     * 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
     * itemtype : 0
     * pid : 46
     * price : 234.0
     * pscid : 39
     * salenum : 868
     * sellerid : 2
     * subhead : 【iPhone新品上市】新一代iPhone,让智能看起来更不一样
     * title : Apple iPhone 8 Plus (A1864) 64GB 金色 移动联通电信4G手机
     */

    private double bargainPrice;
    private String createtime;
    private String detailUrl;
    private String images;
    private int itemtype;
    private int pid;
    private double price;
    private int pscid;
    private int salenum;
    private int sellerid;
    private String subhead;
    private String title;
    private boolean checkbox;

    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 getItemtype() {
        return itemtype;
    }

    public void setItemtype(int itemtype) {
        this.itemtype = itemtype;
    }

    public int getPid() {
        return pid;
    }

    public void setPid(int pid) {
        this.pid = pid;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getPscid() {
        return pscid;
    }

    public void setPscid(int pscid) {
        this.pscid = pscid;
    }

    public int getSalenum() {
        return salenum;
    }

    public void setSalenum(int salenum) {
        this.salenum = salenum;
    }

    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;
    }

    public boolean isCheckbox() {
        
        return checkbox;
    }

    public void setCheckbox(boolean checkbox) {
        this.checkbox = checkbox;
    }
}





Model层

LoggingInterceptor (拦截器)
package com.example.gouwuche.model;

import java.io.IOException;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by 知足 on 2017/12/16.
 */

public class LoggingInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request original = chain.request();
        HttpUrl url=original.url().newBuilder()
                .addQueryParameter("source","android")
                .build();
        //添加请求头
        Request request = original.newBuilder()
                .url(url)
                .build();
        return chain.proceed(request);
    }

}




Presenter层

package com.example.gouwuche.presenter;

import com.example.gouwuche.Bean.JavaBean;
import com.example.gouwuche.utils.CallBack;
import com.example.gouwuche.utils.HttpUtils;
import com.example.gouwuche.view.INewsView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 知足 on 2017/12/16.
 */

public class INewsPresenter {
    private INewsView inv;

    public void attachView(INewsView inv) {
        this.inv = inv;
    }
    public void getNews() {
        //type=top&key=dbedecbcd1899c9785b95cc2d17131c5
        Map<String, String> map = new HashMap<>();
        map.put("keywords", "笔记本");
        map.put("page", "1");
        HttpUtils.getInstance().post("http://120.27.23.105/product/searchProducts", map, new CallBack() {
            @Override
            public void onSuccess(String tag, Object o) {
                JavaBean bean = (JavaBean) o;
                if (bean != null) {
                    List<JavaBean.DataBean> data = bean.getData();
                    inv.success(tag,data);
                }
            }

            @Override
            public void onFailed(String tag, Exception e) {
                inv.failed(tag, e);
            }
        }, JavaBean.class, "news");
    }


    public void detachView() {
        if (inv != null) {
            inv = null;
        }

    }
} 




Utils层

package com.example.gouwuche.utils;

/**
 * Created by 知足 on 2017/12/16.
 */

public interface CallBack {
    void onSuccess(String tag, Object o);
    void onFailed(String tag, Exception e);
}


 
Utils层
GsonUtils (单例模式)
package com.example.gouwuche.utils;

import com.google.gson.Gson;

/**
 * Created by 知足 on 2017/12/16.
 */

public class GsonUtils {
    private static Gson instance;
    private GsonUtils(){

    }
    public static Gson getInstance(){
        if (instance==null){
            instance = new Gson();
        }
        return instance;
    }
}






Utils层

Okhttp进行网络请求封装:

HttpUtils


package com.example.gouwuche.utils;
import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.util.Map;
import android.os.Handler;
import android.util.Log;

import com.example.gouwuche.model.LoggingInterceptor;

/**
 * Created by 知足 on 2017/12/16.
 */

public class HttpUtils {
    private static final String TAG = "HttpUtils";
    private static volatile HttpUtils instance;

    private static Handler handler = new Handler();

    private HttpUtils() {

    }

    public static HttpUtils getInstance() {
        if (null == instance) {
            synchronized (HttpUtils.class) {
                if (instance == null) {
                    instance = new HttpUtils();
                }
            }
        }
        return instance;
    }
    /**
     * 封装的post请求
     *
     * @param url
     * @param map
     * @param callBack
     * @param cls
     */
    public void post(String url, Map<String, String> map, final CallBack callBack, final Class cls, final String tag) {
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new LoggingInterceptor())
                .build();

        FormBody.Builder builder = new FormBody.Builder();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            builder.add(entry.getKey(), entry.getValue());
        }

        FormBody body = builder.build();

        final Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();

        Call call = client.newCall(request);
        call.enqueue(new okhttp3.Callback() {
            @Override
            public void onFailure(Call call, final IOException e) {
                Log.e(TAG, "onFailure: " + e.getCause().getStackTrace() + e.getMessage());
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        callBack.onFailed(tag,e);
                    }
                });

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                Log.i(TAG, "onResponse: " + result);

                final Object o = GsonUtils.getInstance().fromJson(result, cls);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        callBack.onSuccess(tag,o);
                    }
                });
            }
        });
    }
}





Utils层

package com.example.gouwuche.utils;

/**
 * Created by 知足 on 2017/12/16.
 */

public class MessageEvent {
    private boolean checked;

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }
}





Utils层

package com.example.gouwuche.utils;

/**
 * Created by 知足 on 2017/12/16.
 */

public class PriceAndCountEvent {
    private int price;
    private int count;

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}




VIew层


 

package com.example.gouwuche.view;

import com.example.gouwuche.Bean.JavaBean;

import java.util.List;

/**
 * Created by 知足 on 2017/12/16.
 */

public interface INewsView {
    void success(String tag, List<JavaBean.DataBean> news);

    void failed(String tag, Exception e);
}




//布局文件 activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    tools:context="com.example.gouwuche.MainActivity">

    <RelativeLayout
        android:id="@+id/rel"
        android:layout_width="match_parent"
        android:layout_height="40dp">
        <ImageView
            android:id="@+id/img_back"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@mipmap/ic_launcher"
            android:layout_centerVertical="true"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="购物车"
            android:textSize="20sp"
            android:layout_centerInParent="true"
            />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/rel2"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="60dp">
        <CheckBox
            android:id="@+id/cb_all"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            />
        <TextView
            android:id="@+id/all_xuan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="全选"
            android:textSize="20sp"
            android:layout_toRightOf="@+id/cb_all"
            android:layout_centerVertical="true"
            />
        <Button
            android:id="@+id/btn_jiesuan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="结算"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            />

        <TextView
            android:id="@+id/tvv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="共计(件):"
            android:textSize="15sp"
            android:layout_toRightOf="@+id/all_xuan"
            android:layout_marginLeft="20dp"
            />
        <TextView
            android:id="@+id/tv_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textSize="15sp"
            android:layout_toRightOf="@+id/tvv"
            />

        <TextView
            android:id="@+id/tvv2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="总价(元):"
            android:layout_toRightOf="@+id/all_xuan"
            android:textSize="15sp"
            android:layout_below="@+id/tvv"
            android:layout_marginLeft="20dp"
            />
        <TextView
            android:id="@+id/tv_sum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textSize="20sp"
            android:layout_below="@+id/tvv"
            android:layout_toRightOf="@+id/tvv2"
            />

    </RelativeLayout>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recy_view"
        android:layout_above="@id/rel2"
        android:layout_below="@+id/rel"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>

</RelativeLayout>



//布局文件Item

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:orientation="horizontal">

        <CheckBox
            android:id="@+id/item_cb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical" />

        <ImageView
            android:id="@+id/item_img"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@mipmap/ic_launcher" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_marginLeft="10dp"
            android:layout_weight="5"
            android:orientation="vertical">

            <TextView
                android:id="@+id/item_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="标题"
                android:textSize="20sp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                >
                <TextView
                    android:id="@+id/item_price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10sp"
                    android:textSize="16sp"
                    android:text="单价" />
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    >
                    <Button
                        android:id="@+id/bt_add"
                        android:layout_width="36dp"
                        android:layout_height="36dp"
                        android:focusable="false"
                        android:focusableInTouchMode="false"
                        android:text="+"/>

                    <EditText
                        android:id="@+id/et_num"
                        android:layout_width="70dp"
                        android:layout_height="36dp"
                        android:inputType="number"
                        android:gravity="center"
                        android:focusableInTouchMode="false"
                        android:focusable="false"
                        android:text="1"
                        android:background="@null"
                        />
                    <Button
                        android:id="@+id/bt_reduce"
                        android:layout_width="36dp"
                        android:layout_height="36dp"
                        android:clickable="false"
                        android:focusableInTouchMode="false"
                        android:text="-"/>


                </LinearLayout>
            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="共计:"
                    />
                <TextView
                    android:id="@+id/child_num"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="0"
                    />
                <TextView
                    android:layout_gravity="center_horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="总价"
                    android:layout_marginLeft="10dp"
                    />
                <TextView
                    android:id="@+id/child_sum"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="0.00"
                    />
            </LinearLayout>

        </LinearLayout>
        <Button
            android:layout_weight="1"
            android:id="@+id/item_delete"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="删除"
            android:layout_gravity="center_vertical"
            />
    </LinearLayout>


</RelativeLayout>


权限

<uses-permission android:name="android.permission.INTERNET"></uses-permission>



//依赖

compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'org.greenrobot:eventbus:3.1.1'



猜你喜欢

转载自blog.csdn.net/xxb52306/article/details/78846122