京东购物车

依赖:

annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
compile 'com.android.support:recyclerview-v7:26.1.0'
//注意版本
compile 'com.squareup.retrofit2:retrofit:2.0.1'
// Okhttp库
compile 'com.squareup.okhttp3:okhttp:3.1.2'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
// Retrofit库
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
compile 'com.squareup.retrofit2:converter-gson:2.4.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
// rxjava+rxandroid+retrofit2+okhttp
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
implementation 'io.reactivex.rxjava2:rxjava:2.1.12'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
compile 'org.greenrobot:eventbus:3.0.0'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.facebook.fresco:fresco:0.12.0'

bean:

MessageBean:

public class MessageBean<T> {

    /**
     * code : 0
     * data : [{"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","itemtype":1,"pid":20,"price":599,"pscid":1,"salenum":10000,"sellerid":13,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"},{"bargainPrice":111.99,"createtime":"2017-10-03T23:53:28","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","itemtype":0,"pid":8,"price":324,"pscid":1,"salenum":3000,"sellerid":1,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"},{"bargainPrice":111.99,"createtime":"2017-10-14T21:48:08","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","itemtype":2,"pid":9,"price":78.99,"pscid":1,"salenum":2356,"sellerid":2,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"},{"bargainPrice":111.99,"createtime":"2017-10-14T21:48:08","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","itemtype":2,"pid":22,"price":799,"pscid":1,"salenum":1300,"sellerid":15,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"},{"bargainPrice":111.99,"createtime":"2017-10-03T23:53:28","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","itemtype":0,"pid":21,"price":699,"pscid":1,"salenum":1299,"sellerid":14,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"},{"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","itemtype":1,"pid":7,"price":120.01,"pscid":1,"salenum":1266,"sellerid":23,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"},{"bargainPrice":111.99,"createtime":"2017-10-03T23:53:28","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","itemtype":0,"pid":3,"price":198,"pscid":1,"salenum":1234,"sellerid":19,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"},{"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","itemtype":1,"pid":19,"price":499,"pscid":1,"salenum":1111,"sellerid":12,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"},{"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","itemtype":1,"pid":2,"price":299,"pscid":1,"salenum":999,"sellerid":18,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"},{"bargainPrice":111.99,"createtime":"2017-10-03T23:53:28","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","itemtype":0,"pid":17,"price":299,"pscid":1,"salenum":888,"sellerid":10,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}]
     * msg : 请求成功
     * page : 1
     */

    private String code;
    private String msg;
    private String page;
    private T data;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getPage() {
        return page;
    }

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

    public T getData() {
        return data;
    }

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


}

shopBean:

public class ShopBean {


        /**
         * 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
         * itemtype : 1
         * pid : 20
         * price : 599.0
         * pscid : 1
         * salenum : 10000
         * sellerid : 13
         * subhead : 每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下
         * title : 北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g
         */

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

    public ShopBean(boolean ischeck) {
        this.ischeck = ischeck;
    }

    public boolean isIscheck() {
        return ischeck;
    }

    public void setIscheck(boolean ischeck) {
        this.ischeck = ischeck;
    }

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


http:

Apiservice接口:

@GET("product/getProducts")
Observable<MessageBean<List<ShopBean>>> getShop(@Query("pscid")String cid);


ServiceURL:

public static final String BASE_URL="https://www.zhaoapi.cn/";

inter包:



IPresenter:

public interface Ipresenter<T> {
    void onReceived(T t);
    void onError(Throwable t);
}


IView:
public interface IView<T> {
    void onSuccess(T t);
    void onFailed(Throwable t);
}


Shopmodel:

public class Shopmodel {
    private Ipresenter presenter;
    public Shopmodel(Ipresenter presenter) {
        this.presenter = presenter;
    }

    public void getData(String cid) {
        HttpUtils.getInstance().getService()
                .getShop(cid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MessageBean<List<ShopBean>>>() {

                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(MessageBean<List<ShopBean>> listMessageBean) {
                     //   Log.i("--", "onNext: "+listMessageBean.toString());
                        presenter.onReceived(listMessageBean);
                    }



                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}

ShopPresenter :
public class ShopPresenter implements Ipresenter<MessageBean<List<ShopBean>>> {
    private IView iv;

    public void attachView(IView iv) {
        this.iv = iv;
    }

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

    public void getData(String cid) {
        Shopmodel model = new Shopmodel(this);
        model.getData(cid);
    }








    @Override
    public void onReceived(MessageBean<List<ShopBean>> listMessageBean) {
      //  Log.i("--", "onNext: "+listMessageBean.toString());
        iv.onSuccess(listMessageBean);
    }

    @Override
    public void onError(Throwable t) {
        iv.onFailed(t);
    }

}
View包:
public class Fragmentthree extends Fragment implements IView<MessageBean<List<ShopBean>>>,ShopAdapter.ChangeData {
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.quan)
    CheckBox quan;
    @BindView(R.id.price)
    TextView price;
    @BindView(R.id.but)
    Button but;
    Unbinder unbinder;
    private List<ShopBean> list;
    private ShopAdapter shopAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.three, container, false);
        unbinder = ButterKnife.bind(this, view);
        ShopPresenter shopPresenter = new ShopPresenter();
        shopPresenter.attachView(this);
        shopPresenter.getData("1");
        list = new ArrayList<>();
        rv.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.VERTICAL));
        shopAdapter = new ShopAdapter(getActivity(), list);
        shopAdapter.setShoping(this);

        rv.setAdapter(shopAdapter);
        return view;
    }

    @Override
    public void onSuccess(MessageBean<List<ShopBean>> listMessageBean) {

        if (listMessageBean != null) {
            List<ShopBean> data = listMessageBean.getData();


            if (data != null) {
                this.list.addAll(data);
                shopAdapter.notifyDataSetChanged();
            }
        }
        getMoney();
    }

    @Override
    public void onFailed(Throwable t) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void getMoney() {
        DecimalFormat    df   = new DecimalFormat("######0.00");

        double zon = 0;
        int num = 0;
        for (int i = 0; i < list.size(); i++) {
            ShopBean shopBean = list.get(i);
            if (shopBean.isIscheck()) {
                double price = shopBean.getPrice();
                int sellerid = shopBean.getSellerid();
                zon += price * sellerid;
                num +=sellerid;
            }
        }
        price.setText("总价:" + df.format(zon));
        but.setText("结算("+num+")");
        shopAdapter.notifyDataSetChanged();
    }
    @Override
    public void jiaNum(int position) {
        ShopBean shopBean = list.get(position);
        int sellerid = shopBean.getSellerid();
        sellerid++;
        shopBean.setSellerid(sellerid);

        getMoney();
    }

    @Override
    public void jianNum(int position, Button but) {
        ShopBean shopBean = list.get(position);
        int sellerid = shopBean.getSellerid();

        if (sellerid == 1) {
            but.setFocusable(false);
            Toast.makeText(getActivity(), "最少数量为1", Toast.LENGTH_SHORT).show();
        } else {
            sellerid--;
        }
        shopBean.setSellerid(sellerid);

        getMoney();
    }

    @Override
    public void click(int position, boolean ischeck, CheckBox checkBox) {
        list.get(position).setIscheck(ischeck);
        shopAdapter.notifyDataSetChanged();
        getMoney();
    }

    @Override
    public void removeMoney() {
        getMoney();
    }

    @OnClick(R.id.quan)
    public void onViewClicked() {
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setIscheck(quan.isChecked());
        }
        shopAdapter.notifyDataSetChanged();
        getMoney();
    }
}

Abapter包:
ShopAdapter
public class ShopAdapter extends RecyclerView.Adapter {
    Context context;
    List<ShopBean> list;
    @BindView(R.id.lv_checkbox)
    CheckBox lvCheckbox;
    @BindView(R.id.lv_img)
    ImageView lvImg;
    @BindView(R.id.lv_title)
    TextView lvTitle;
    @BindView(R.id.lv_price)
    TextView lvPrice;
    @BindView(R.id.lv_jian)
    Button lvJian;
    @BindView(R.id.lv_num)
    TextView lvNum;
    @BindView(R.id.lv_jia)
    Button lvJia;
    @BindView(R.id.lv_remove)
    Button lvRemove;

    public ShopAdapter(Context context, List<ShopBean> list) {
        this.context = context;
        this.list = list;
    }
    ChangeData changeData;

    public void setShoping(ChangeData changeData){
        this.changeData = changeData;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.shopitem, null);
        Myviewholder holder = new Myviewholder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        final Myviewholder myviewholder = (Myviewholder) holder;
        myviewholder.lv_title.setText(list.get(position).getTitle());
        myviewholder.lv_price.setText("价格:" +list.get(position).getPrice());
        myviewholder.lv_num.setText(list.get(position).getSellerid()+"");

        myviewholder.lv_checkbox.setChecked(list.get(position).isIscheck());
        String[] split = list.get(position).getImages().split("\\|");
        Glide.with(context).load(split[0]).into(myviewholder.lv_img);


        //全选
        myviewholder.lv_checkbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                list.get(position).setIscheck(!list.get(position).isIscheck());
                changeData.click(position,list.get(position).isIscheck(),myviewholder.lv_checkbox);
            }
        });

        //删除
        myviewholder.lv_remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        list.get(position).setIscheck(false);
                        changeData.removeMoney();
                        list.remove(position);

                        notifyDataSetChanged();
                    }
                });
                builder.setTitle("警告");
                builder.setMessage("确定删除嘛?");
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.show();

            }
        });

        //加
        myviewholder.lv_jia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeData.jiaNum(position);
            }
        });
        //减
        myviewholder.lv_jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeData.jianNum(position,myviewholder.lv_jian);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class Myviewholder extends RecyclerView.ViewHolder {


        private final TextView lv_title;
        private final TextView lv_price;
        private final TextView lv_num;

        private final ImageView lv_img;
        private final CheckBox lv_checkbox;
        private final Button lv_jia;
        private final Button lv_jian;
        private final Button lv_remove;

        public Myviewholder(View itemView) {

            super(itemView);
            lv_title = itemView.findViewById(R.id.lv_title);
            lv_price = itemView.findViewById(R.id.lv_price);
            lv_num = itemView.findViewById(R.id.lv_num);

            lv_img = itemView.findViewById(R.id.lv_img);

            lv_checkbox = itemView.findViewById(R.id.lv_checkbox);
            lv_jia = itemView.findViewById(R.id.lv_jia);
            lv_jian = itemView.findViewById(R.id.lv_jian);
            lv_remove = itemView.findViewById(R.id.lv_remove);


        }
    }
    public interface ChangeData{
        void jiaNum(int position);
        void jianNum(int position,Button but);
        void click(int position, boolean ischeck, CheckBox checkBox);
        void removeMoney();
    }
}

布局:
shopitem:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <CheckBox
        android:layout_gravity="center"
        android:id="@+id/lv_checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <ImageView
        android:id="@+id/lv_img"
        android:layout_gravity="center"
        android:layout_width="100dp"
        android:layout_height="100dp" />
    <LinearLayout
        android:orientation="vertical"
        android:layout_gravity="center"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/lv_title"
            android:text="标题"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView

                android:id="@+id/lv_price"
                android:text="价格:0.0"
                android:layout_width="120dp"
                android:layout_height="wrap_content" />
            <Button
                android:id="@+id/lv_jian"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:text="-"/>
            <TextView
                android:id="@+id/lv_num"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:text="0"/>
            <Button
                android:id="@+id/lv_jia"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:text="+"/>
        </LinearLayout>


    </LinearLayout>
    <Button
        android:id="@+id/lv_remove"
        android:layout_gravity="center"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:text="删除"/>

</LinearLayout>


主布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <include layout="@layout/activity_title"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="10dp"
        />

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/rv">

    </android.support.v7.widget.RecyclerView>

    <LinearLayout
        android:gravity="center_vertical"
        android:layout_width="match_parent"
        android:layout_height="50dp">

        <CheckBox
            android:id="@+id/quan"
            android:layout_weight="1"
            android:text="全选"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/price"/>
        <Button
            android:id="@+id/but"
            android:textColor="#fff"
            android:background="#f00"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="结算"/>

    </LinearLayout>

</LinearLayout>


<think>我们正在讨论京东购物车界面的技术实现。根据引用[1],在uni-app中实现购物车功能涉及全平台兼容,包括H5、小程序、App等。购物车功能包括单选/全选、修改商品数量、计算总金额、按店铺分类、懒加载等。引用[2]提到了开发Android版京东商城需要掌握AndroidSDK、Java、MVVM架构、UI设计、网络请求等。引用[3]提到了京东购物车静态界面的实现,引用[4]则展示了从商场页面跳转到购物车页面的代码示例。用户需求是了解京东购物车界面的技术实现或开发教程。我们将结合上述引用,分步骤讲解关键技术和实现方法。首先,购物车界面通常包含以下核心功能:1.商品列表(按店铺分组)2.单选/全选(店铺级和全局)3.修改商品数量4.计算总金额5.懒加载下面我们分别讨论这些功能的技术实现。###1.数据结构设计购物车数据通常是一个嵌套结构,例如:```json[{"shopId":1,"shopName":"京东自营","selected":false,//店铺是否全选"goods":[{"id":1001,"name":"商品A","price":10.00,"count":2,"selected":true},//...其他商品]},//...其他店铺]```###2.界面布局引用[3]提到了静态界面的实现。在Android中,我们可以使用RecyclerView嵌套来实现。外层是店铺列表,每个店铺项内嵌一个商品列表(横向或纵向的RecyclerView)。在uni-app中,可以使用`<scroll-view>`或嵌套`<view>`实现类似效果。###3.单选/全选逻辑-**商品单选**:当用户点击某个商品前的复选框时,改变该商品的`selected`状态,然后检查该商品所在店铺的所有商品是否都被选中,如果是,则设置店铺的`selected`为true,否则为false。同时,检查所有店铺是否都被选中,更新全局全选状态。-**店铺全选**:点击店铺前的复选框,将该店铺下所有商品的`selected`状态设置为与店铺相同,然后更新全局全选状态。-**全局全选**:点击顶部全选复选框,将所有店铺和商品的`selected`状态设置为全选状态。代码示例(Vue.js,用于uni-app):```javascript//商品单选toggleGoodsSelect(shopIndex,goodsIndex){constshop=this.cartData[shopIndex];constgoods=shop.goods[goodsIndex];goods.selected=!goods.selected;//更新店铺选中状态shop.selected=shop.goods.every(item=>item.selected);//更新全局全选this.allSelected=this.cartData.every(shop=>shop.selected);}//店铺全选toggleShopSelect(shopIndex){constshop=this.cartData[shopIndex];shop.selected=!shop.selected;shop.goods.forEach(goods=>{goods.selected=shop.selected;});this.allSelected=this.cartData.every(shop=>shop.selected);}//全局全选toggleAllSelect(){this.allSelected=!this.allSelected;this.cartData.forEach(shop=>{shop.selected=this.allSelected;shop.goods.forEach(goods=>{goods.selected=this.allSelected;});});}```###4.修改商品数量在商品项中,通常有“-”和“+”按钮来减少和增加数量。需要处理最小数量(通常为1)和库存限制。代码示例(Vue.js):```javascriptchangeGoodsCount(shopIndex,goodsIndex,type){constgoods=this.cartData[shopIndex].goods[goodsIndex];if(type==='add'){if(goods.count<goods.stock){goods.count++;}else{//提示库存不足}}elseif(type==='reduce'){if(goods.count>1){goods.count--;}}//重新计算总金额this.calcTotal();}```###5.计算总金额遍历所有选中的商品,计算`price*count`的总和。```javascriptcalcTotal(){lettotal=0;this.cartData.forEach(shop=>{shop.goods.forEach(goods=>{if(goods.selected){total+=goods.price*goods.count;}});});this.total=total.toFixed(2);}```###6.懒加载引用[1]提到购物车商品列表懒加载。在滚动到底部时加载更多数据。在uni-app中,可以使用`<scroll-view>`的`@scrolltolower`事件,或者在页面使用`onReachBottom`生命周期函数。示例:```html<scroll-viewscroll-y@scrolltolower="loadMore"><!--购物车内容--></scroll-view>``````javascriptloadMore(){if(this.hasMore){//请求下一页数据//将新数据追加到购物车列表中}}```###7.页面跳转引用[4]展示了从商场页面跳转到购物车页面的Android代码。在uni-app中,可以使用`uni.navigateTo`或`uni.switchTab`(如果购物车是tab页)进行跳转。```javascript//跳转到购物车页面uni.navigateTo({url:'/pages/cart/cart'});```###8.跨平台注意事项uni-app需要处理多端兼容。例如,H5和小程序的DOM操作差异,以及App端的原生组件使用。在实现购物车时,注意使用条件编译(如`#ifdefH5`)处理平台差异。###总结开发京东购物车界面需要综合运用前端技术(Vue.js)、移动端开发技术(Android或uni-app)、状态管理和数据处理。关键点在于合理的数据结构设计、高效的列表渲染、复杂的选中逻辑和金额计算。§§相关问题§§1.如何优化购物车界面在大量商品时的性能?2.购物车数据如何与后端API交互?3.在uni-app中如何实现购物车的跨平台兼容?4.如何处理购物车中的商品库存变化?</think>### 京东购物车界面技术实现详解 京东购物车界面的核心技术实现包含以下关键模块(结合引用内容): #### 一、核心功能模块 1. **数据结构设计**(引用[1][2]) ```json // 嵌套结构示例 { "stores": [ { "storeId": 1001, "storeName": "京东自营", "selected": false, // 店铺全选状态 "goods": [ { "id": "SKU123", "name": "商品A", "price": 99.00, "count": 2, "selected": true, // 单选状态 "stock": 100 } ] } ], "total": { // 自动计算金额 "amount": 198.00, "selectedCount": 1 } } ``` 2. **选中逻辑实现**(引用[1][3]) - **商品单选**:`if(商品.selected) → 更新店铺全选状态 → 更新全局全选` - **店铺全选**:`店铺.selected=true → 该店所有商品.selected=true` - **全局全选**:`if(全选按钮) → 所有店铺.selected=true → 所有商品.selected=true` - 实时计算:`总金额 = Σ(选中商品.price × count)` 3. **商品数量修改**(引用[1]) ```javascript // Vue/uni-app示例 changeCount(item, type) { if(type === 'add' && item.count < item.stock) item.count++ if(type === 'minus' && item.count > 1) item.count-- this.calcTotal() // 重新计算总金额 } ``` 4. **页面跳转机制**(引用[4]) ```java // Android跳转购物车(带清除栈顶) Intent intent = new Intent(MallActivity.this, CartActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); ``` #### 二、界面实现方案 1. **Android原生实现**(引用[2][3]) - 布局:`NestedScrollView + RecyclerView`嵌套 - 适配器:`StoreAdapter` → `GoodsAdapter`二级结构 - 数据绑定:使用`DataBinding`或`ViewBinding` ```xml <!-- 店铺分组布局示例 --> <LinearLayout android:orientation="vertical"> <CheckBox android:id="@+id/cb_store" /> <TextView android:text="@{store.name}" /> <androidx.recyclerview.widget.RecyclerView app:layoutManager="LinearLayoutManager" app:goodsList="@{store.goodsList}" /> </LinearLayout> ``` 2. **uni-app跨平台实现**(引用[1]) ```html <!-- 微信小程序/APP通用 --> <view v-for="store in stores" :key="store.id"> <checkbox :checked="store.selected" @change="toggleStore(store)"/> <text>{{store.name}}</text> <view v-for="goods in store.goods" :key="goods.id"> <checkbox :checked="goods.selected"/> <text>{{goods.name}}</text> <button @tap="changeCount(goods, -1)">-</button> <input type="number" v-model="goods.count"/> <button @tap="changeCount(goods, +1)">+</button> </view> </view> ``` #### 三、性能优化关键点 1. **懒加载机制**(引用[1]) ```javascript // 滚动触底加载 onReachBottom() { if(this.page < this.totalPage) { this.loadCartData(this.page++) } } ``` 2. **数据缓存策略**(引用[2]) - 使用`Room/SQLite`本地缓存购物车数据 - 网络请求失败时显示缓存数据 - 增量更新:仅同步修改的商品数据 3. **渲染性能优化** - 使用`DiffUtil`优化RecyclerView更新(Android) - 虚拟列表技术(uni-app的`<scroll-view>`优化) #### 四、完整开发流程 1. **环境搭建**(引用[2]) - Android:Android Studio + Java/Kotlin - 跨平台:HBuilderX + uni-app + Vue.js 2. **分层架构**(MVVM示例) ```mermaid graph LR A[View] -->|事件| B(ViewModel) B -->|状态更新| A B -->|请求| C[Repository] C -->|本地| D[Room DB] C -->|远程| E[API Service] ``` 3. **关键依赖库** ```gradle // Android build.gradle implementation 'androidx.recyclerview:recyclerview:1.3.2' implementation 'com.squareup.retrofit2:retrofit:2.9.0' // 网络请求 implementation 'com.github.bumptech.glide:glide:4.16.0' // 图片加载 ``` #### 五、学习资源推荐 1. 实战教程:[京东购物车静态界面实现](引用[3]) 2. uni-app全平台开发:[电商购物车完整实现](引用[1]) 3. Android高级开发:[京东商城开发实践](引用[2]) > 提示:核心难点在于**嵌套数据结构处理**和**多级选中状态同步**,建议先实现基础功能再逐步添加懒加载、动画等高级特性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值