小练习(mvp)

//Model

public class Model implements IModel {
    private NewsPresenter presenter;

    public Model(NewsPresenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void getData(Map<String, String> map) {
        Flowable<ZhuCeBean> flowable = RetrofitUtils.getInstance("http://120.27.23.105/").getAPIservice().getNews(map);
        presenter.get(flowable);
    }

    @Override
    public void getLogin(Map<String, String> map) {
        Flowable<LoginBean> flowable = RetrofitUtils.getInstance("http://120.27.23.105/").getAPIservice().getLogin(map);
        presenter.getLogin(flowable);
    }

    @Override
    public void getGoods(Map<String, String> map) {
        Flowable<Goods> flowable = RetrofitUtils.getInstance("https://www.zhaoapi.cn/").getAPIservice().getGoods(map);
        presenter.goods(flowable);
    }

    @Override
    public void getXiangQing(Map<String, String> map) {
        Flowable<XiangQingBean> flowable = RetrofitUtils.getInstance("https://www.zhaoapi.cn/").getAPIservice().getXiangQing(map);
        presenter.xiangQing(flowable);
    }

    @Override
    public void getGouWuChe(Map<String, String> map) {
        Flowable<ZhuCeBean> flowable = RetrofitUtils.getInstance("http://120.27.23.105/").getAPIservice().getGouWuChe(map);
        presenter.add(flowable);
    }
    @Override
    public void getChaXun(Map<String, String> map) {
        Flowable<ShopCar> flowable = RetrofitUtils.getInstance("http://120.27.23.105/").getAPIservice().getChaXun(map);
        presenter.chaxun(flowable);
    }

    @Override
    public void getDelete(Map<String, String> map) {
        Flowable<ZhuCeBean> flowable = RetrofitUtils.getInstance("http://120.27.23.105/").getAPIservice().getDelete(map);
        presenter.delete(flowable);
    }
}
//Presenter

//NewsPresenter

public class NewsPresenter implements IPresenter {


    private IView iView;
    private DisposableSubscriber<ZhuCeBean> subscriber;
    private DisposableSubscriber<ZhuCeBean> subscriber5;
    private DisposableSubscriber<LoginBean> subscriber2;
    private DisposableSubscriber<Goods> subscriber3;
    private DisposableSubscriber<XiangQingBean> subscriber4;
    private DisposableSubscriber<ShopCar> subscriber6;
    private DisposableSubscriber<ZhuCeBean> subscriber7;

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

    @Override
    public void getData(Map<String, String> map) {
        Model model = new Model(this);
        model.getData(map);
    }

    @Override
    public void getLogin(Map<String, String> map) {
        Model model = new Model(this);
        model.getLogin(map);
    }

    @Override
    public void getGoods(Map<String, String> map) {
        Model model = new Model(this);
        model.getGoods(map);
    }

    @Override
    public void getXiangQing(Map<String, String> map) {
        Model model = new Model(this);
        model.getXiangQing(map);

    }

    @Override
    public void getGouWuChe(Map<String, String> map) {
        Model model = new Model(this);
        model.getGouWuChe(map);
    }

    @Override
    public void getChaXun(Map<String, String> map) {
        Model model = new Model(this);
        model.getChaXun(map);
    }

    @Override
    public void getDelete(Map<String, String> map) {
        Model model = new Model(this);
        model.getDelete(map);
    }

    public void detachView() {
        if (subscriber != null) {
            //如果该资源已被处理,则可一次性返回true。如果没有被处理返回false
            if (!subscriber.isDisposed()) {
                subscriber.dispose();
            }
        }
        if (subscriber2 != null) {
            //如果该资源已被处理,则可一次性返回true。如果没有被处理返回false
            if (!subscriber2.isDisposed()) {
                subscriber2.dispose();
            }
        }
        if (subscriber3 != null) {
            //如果该资源已被处理,则可一次性返回true。如果没有被处理返回false
            if (!subscriber2.isDisposed()) {
                subscriber2.dispose();
            }
        }
        if (subscriber4 != null) {
            //如果该资源已被处理,则可一次性返回true。如果没有被处理返回false
            if (!subscriber4.isDisposed()) {
                subscriber4.dispose();
            }
        }
        if (subscriber5 != null) {
            //如果该资源已被处理,则可一次性返回true。如果没有被处理返回false
            if (!subscriber5.isDisposed()) {
                subscriber5.dispose();
            }
        }
        if (subscriber6 != null) {
            //如果该资源已被处理,则可一次性返回true。如果没有被处理返回false
            if (!subscriber6.isDisposed()) {
                subscriber6.dispose();
            }
        }
        if (subscriber7 != null) {
            //如果该资源已被处理,则可一次性返回true。如果没有被处理返回false
            if (!subscriber7.isDisposed()) {
                subscriber7.dispose();
            }
        }
    }

    public void get(Flowable<ZhuCeBean> flowable) {
        subscriber = flowable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber<ZhuCeBean>() {
                    @Override
                    public void onNext(ZhuCeBean listMessageBean) {
                        if (listMessageBean != null) {
                            iView.OnSuccess(listMessageBean);
                        }
                    }

                    @Override
                    public void onError(Throwable t) {
                        iView.OnFailed(new Exception(t));
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void getLogin(Flowable<LoginBean> flowable) {
        subscriber2 = flowable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber<LoginBean>() {
                    @Override
                    public void onNext(LoginBean listMessageBean) {
                        if (listMessageBean != null) {
                            iView.OnSuccess2(listMessageBean);
                        }
                    }

                    @Override
                    public void onError(Throwable t) {
                        iView.OnFailed(new Exception(t));
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void goods(Flowable<Goods> flowable) {
        subscriber3 = flowable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber<Goods>() {
                    @Override
                    public void onNext(Goods listMessageBean) {
                        if (listMessageBean != null) {
                            List<Goods.DataBean> data = listMessageBean.getData();
                            if (data != null) {
                                iView.OnSuccess3(data);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable t) {
                        iView.OnFailed(new Exception(t));
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void xiangQing(Flowable<XiangQingBean> flowable) {
        subscriber4 = flowable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber<XiangQingBean>() {
                    @Override
                    public void onNext(XiangQingBean listMessageBean) {
                        if (listMessageBean != null) {
                            iView.OnSuccess4(listMessageBean);
                        }
                    }

                    @Override
                    public void onError(Throwable t) {
                        iView.OnFailed(new Exception(t));
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void add(Flowable<ZhuCeBean> flowable) {
        subscriber5 = flowable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber<ZhuCeBean>() {
                    @Override
                    public void onNext(ZhuCeBean listMessageBean) {
                        if (listMessageBean != null) {
                            iView.OnSuccess5(listMessageBean);
                        }
                    }

                    @Override
                    public void onError(Throwable t) {
                        iView.OnFailed(new Exception(t));
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void chaxun(Flowable<ShopCar> flowable) {
        subscriber6 = flowable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber<ShopCar>() {
                    @Override
                    public void onNext(ShopCar listMessageBean) {
                        if (listMessageBean != null) {
                            iView.OnSuccess6(listMessageBean);
                        }
                    }

                    @Override
                    public void onError(Throwable t) {
                        iView.OnFailed(new Exception(t));
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void delete(Flowable<ZhuCeBean> flowable) {
        subscriber7 = flowable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber<ZhuCeBean>() {
                    @Override
                    public void onNext(ZhuCeBean listMessageBean) {
                        if (listMessageBean != null) {
                            iView.OnSuccess7(listMessageBean);
                        }
                    }

                    @Override
                    public void onError(Throwable t) {
                        iView.OnFailed(new Exception(t));
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
//Interfaces

//IModel

public interface IModel {
    void getData(Map<String, String> map);

    void getLogin(Map<String, String> map);

    void getGoods(Map<String, String> map);

    void getXiangQing(Map<String, String> map);

    void getGouWuChe(Map<String, String> map);

    void getChaXun(Map<String, String> map);

    void getDelete(Map<String, String> map);

}
//IPresenter

public interface IPresenter {
    void getData(Map<String, String> map);

    void getLogin(Map<String, String> map);

    void getGoods(Map<String, String> map);

    void getXiangQing(Map<String, String> map);

    void getGouWuChe(Map<String, String> map);

    void getChaXun(Map<String, String> map);

    void getDelete(Map<String, String> map);
}
//IView

public interface IView {
    void OnSuccess(Object o);

    void OnSuccess2(Object o);

    void OnSuccess3(Object o);

    void OnSuccess4(Object o);

    void OnSuccess5(Object o);

    void OnSuccess6(Object o);

    void OnSuccess7(Object o);

    void OnFailed(Exception e);
}
//MainActivity

public class MainActivity extends AppCompatActivity implements IView {

    @BindView(R.id.ed_phone)
    EditText edPhone;
    @BindView(R.id.ed_password)
    EditText edPassword;
    @BindView(R.id.tv_zhuce)
    TextView tvZhuce;
    @BindView(R.id.btn_login)
    Button btnLogin;
    private NewsPresenter presenter;
    private SharedPreferences.Editor edit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
//        http://120.27.23.105/user/login
        presenter = new NewsPresenter();
        presenter.attachView(this);
        SharedPreferences user = getSharedPreferences("user", MODE_PRIVATE);
        edit = user.edit();
    }

    @OnClick({R.id.tv_zhuce, R.id.btn_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_zhuce:
                Intent intent = new Intent(MainActivity.this, ZhuCeActivity.class);
                startActivity(intent);
                break;
            case R.id.btn_login:
                String password = edPassword.getText().toString().trim();
                String mobile = edPhone.getText().toString().trim();
                if (TextUtils.isEmpty(mobile) || TextUtils.isEmpty(password)) {
                    Toast.makeText(getApplicationContext(), "请输入用户名或密码", Toast.LENGTH_SHORT).show();
                } else {
                    Log.e("-----", password + "----" + mobile);
                    Map<String, String> map = new HashMap<>();
                    map.put("mobile", mobile);
                    map.put("password ", password);
                    presenter.getLogin(map);
                }
                break;
        }
    }

    @Override
    public void OnSuccess(Object o) {

    }

    @Override
    public void OnSuccess2(Object o) {
        if (o != null) {
            LoginBean loginBean = (LoginBean) o;
            Intent intent = new Intent(this, GoodsActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @Override
    public void OnSuccess3(Object o) {

    }

    @Override
    public void OnSuccess4(Object o) {

    }

    @Override
    public void OnSuccess5(Object o) {

    }

    @Override
    public void OnSuccess6(Object o) {

    }

    @Override
    public void OnSuccess7(Object o) {

    }

    @Override
    public void OnFailed(Exception e) {

    }
}
//ZhuCeActivity

public class ZhuCeActivity extends AppCompatActivity implements IView {

    @BindView(R.id.tv_zc_close)
    TextView tvZcClose;
    @BindView(R.id.ed_zc_name)
    EditText edZcName;
    @BindView(R.id.ed_zc_password)
    EditText edZcPassword;
    @BindView(R.id.ed_zc_password2)
    EditText edZcPassword2;
    @BindView(R.id.btn_ze)
    Button btnZe;
    private NewsPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_zhu_ce);
        ButterKnife.bind(this);
        presenter = new NewsPresenter();
        presenter.attachView(this);
    }

    @OnClick({R.id.tv_zc_close, R.id.btn_ze})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_zc_close:
                finish();
                break;
            case R.id.btn_ze:
                String phone = edZcName.getText().toString().trim();
                String password = edZcPassword.getText().toString().trim();
                String password2 = edZcPassword2.getText().toString().trim();
                Log.e("-----", phone + "--" + password + "--" + password2);
                if (TextUtils.isEmpty(phone) || TextUtils.isEmpty(password)) {
                    Toast.makeText(getApplicationContext(), "请输入用户名或密码", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(password2)) {
                    Toast.makeText(getApplicationContext(), "请确认密码", Toast.LENGTH_SHORT).show();
                } else if (!password.equals(password2)) {
                    Toast.makeText(getApplicationContext(), "密码确认错误", Toast.LENGTH_SHORT).show();
                } else if (password.length() < 6 || password2.length() < 6) {
                    Toast.makeText(getApplicationContext(), "密码必须大于6", Toast.LENGTH_SHORT).show();
                } else {
                    Map<String, String> map = new HashMap<>();
                    map.put("mobile", phone);
                    map.put("password", password);
                    presenter.getData(map);
                }
                break;
        }
    }

    @Override
    public void OnSuccess(Object o) {
        if (o != null) {
            ZhuCeBean zhuCeBean = (ZhuCeBean) o;
            Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    @Override
    public void OnSuccess2(Object o) {

    }

    @Override
    public void OnSuccess3(Object o) {

    }

    @Override
    public void OnSuccess4(Object o) {

    }

    @Override
    public void OnSuccess5(Object o) {

    }

    @Override
    public void OnSuccess6(Object o) {

    }

    @Override
    public void OnSuccess7(Object o) {

    }

    @Override
    public void OnFailed(Exception e) {
        Toast.makeText(getApplicationContext(), "注册失败", Toast.LENGTH_SHORT).show();
    }
}
//GoodsActivity

public class GoodsActivity extends AppCompatActivity implements IView {

    @BindView(R.id.recycle)
    RecyclerView recycle;
    private NewsPresenter presenter;
    List<Goods.DataBean> data = new ArrayList<>();
    private MyGoodsAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods);
        ButterKnife.bind(this);
        recycle.setLayoutManager(new LinearLayoutManager(this));
        presenter = new NewsPresenter();
        presenter.attachView(this);
        Map<String, String> map = new HashMap<>();
        map.put("pscid", "39");
        map.put("page", "1");
        presenter.getGoods(map);
        adapter = new MyGoodsAdapter(this, data, this);
    }

    @Override
    public void OnSuccess(Object o) {

    }

    @Override
    public void OnSuccess2(Object o) {

    }

    @Override
    public void OnSuccess3(Object o) {
        if (o != null) {
            List<Goods.DataBean> dataBeen = (List<Goods.DataBean>) o;
            data.addAll(dataBeen);
            recycle.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void OnSuccess4(Object o) {

    }

    @Override
    public void OnSuccess5(Object o) {

    }

    @Override
    public void OnSuccess6(Object o) {

    }

    @Override
    public void OnSuccess7(Object o) {

    }

    @Override
    public void OnFailed(Exception e) {

    }

    public void getGoodsXiang(int pid) {
        Intent intent = new Intent(GoodsActivity.this, XiangQingActivity.class);
        intent.putExtra("pid", pid);
        startActivity(intent);
    }
}

//XiangQingActivity

public class XiangQingActivity extends AppCompatActivity implements IView {

    @BindView(R.id.img_xq)
    ImageView imgXq;
    @BindView(R.id.tv_xq_title)
    TextView tvXqTitle;
    @BindView(R.id.tv_xq_price)
    TextView tvXqPrice;
    @BindView(R.id.btn_xq_add)
    Button btnXqAdd;
    private NewsPresenter presenter;
    private int pid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xiang_qing);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        pid = intent.getIntExtra("pid", 0);
        presenter = new NewsPresenter();
        presenter.attachView(this);
        Map<String, String> map = new HashMap<>();
        map.put("pid", pid + "");
        presenter.getXiangQing(map);
    }

    @OnClick(R.id.btn_xq_add)
    public void onViewClicked() {
        Map<String, String> map = new HashMap<>();
        map.put("uid", "2606");
        map.put("pid", pid + "");
        presenter.getGouWuChe(map);
    }

    @Override
    public void OnSuccess(Object o) {

    }

    @Override
    public void OnSuccess2(Object o) {

    }

    @Override
    public void OnSuccess3(Object o) {

    }

    @Override
    public void OnSuccess4(Object o) {
        if (o != null) {
            XiangQingBean xiangQingBean = (XiangQingBean) o;
            double bargainPrice = xiangQingBean.getData().getBargainPrice();
            String title = xiangQingBean.getData().getTitle();
            String images = xiangQingBean.getData().getImages().split("\\|")[0];
            Glide.with(this).load(images).into(imgXq);
            tvXqPrice.setText(bargainPrice + "");
            tvXqTitle.setText(title);
        }
    }

    @Override
    public void OnSuccess5(Object o) {
        if (o != null) {
            ZhuCeBean zhuCeBean = (ZhuCeBean) o;
            if (zhuCeBean.getCode().equals("0")) {
                Toast.makeText(getApplicationContext(), "加入购物车成功", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(this, GouWuCheActivity.class);
                startActivity(intent);
            }
        }
    }

    @Override
    public void OnSuccess6(Object o) {

    }

    @Override
    public void OnSuccess7(Object o) {

    }

    @Override
    public void OnFailed(Exception e) {
        Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
    }
}
//GouWuCheActivity

public class GouWuCheActivity extends AppCompatActivity implements IView {

    @BindView(R.id.gwc)
    TextView gwc;
    @BindView(R.id.img_gwc_message)
    TextView imgGwcMessage;
    @BindView(R.id.exlist)
    ExpandableListView exlist;
    @BindView(R.id.check_gwc_qx)
    public CheckBox checkGwcQx;
    @BindView(R.id.tv_gwc_zprice)
    TextView tvGwcZprice;
    @BindView(R.id.textView)
    TextView textView;
    @BindView(R.id.tv_gwc_count)
    TextView tvGwcCount;
    @BindView(R.id.btn_gwc_js)
    Button btnGwcJs;
    private NewsPresenter presenter;
    List<GroupBean> groupBeen = new ArrayList<>();
    List<List<GoodsBean>> goods = new ArrayList<>();
    private MyShopAdapter adapter;
    boolean flagedit = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gou_wu_che);
        ButterKnife.bind(this);
        presenter = new NewsPresenter();
        presenter.attachView(this);
        Map<String, String> map = new HashMap<>();
        map.put("uid", "2606");
        presenter.getChaXun(map);
        adapter = new MyShopAdapter(this, groupBeen, goods, this);
        exlist.setAdapter(adapter);
        for (int i = 0; i < adapter.getGroupCount(); i++) {
            exlist.expandGroup(i);
        }
    }


    @OnClick({R.id.img_gwc_message, R.id.check_gwc_qx, R.id.btn_gwc_js})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_gwc_message:
                String trim = imgGwcMessage.getText().toString().trim();
                if (trim.equals("编辑")) {
                    imgGwcMessage.setText("完成");
                } else {
                    imgGwcMessage.setText("编辑");
                }
                for (List<GoodsBean> i1 : goods) {
                    for (int r = 0; r < i1.size(); r++) {
                        i1.get(r).setBtn(flagedit);
                    }
                }
                flagedit = !flagedit;
                adapter.notifyDataSetChanged();
                break;
            case R.id.check_gwc_qx:
                boolean checked = checkGwcQx.isChecked();
                for (int i = 0; i < groupBeen.size(); i++) {
                    groupBeen.get(i).setGroupcheck(checked);
                }
                for (int q = 0; q < goods.size(); q++) {
                    List<GoodsBean> goodsBeen = goods.get(q);
                    for (int j = 0; j < goodsBeen.size(); j++) {
                        goodsBeen.get(j).setGoodscheck(checked);
                    }
                }
                changesum(goods);
                adapter.notifyDataSetChanged();
                break;
            case R.id.btn_gwc_js:
                int index = 0;
                for (int q = 0; q < goods.size(); q++) {
                    List<GoodsBean> goodsBeen = goods.get(q);
                    for (int j = 0; j < goodsBeen.size(); j++) {
                        boolean goodscheck = goodsBeen.get(j).isGoodscheck();
                        if (goodscheck) {
                            index++;
                        }
                    }
                }
                if (index == 0) {
                    Toast.makeText(getApplicationContext(), "请选择商品,谢谢", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(), "钱就是另一回事了", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    @Override
    public void OnSuccess(Object o) {

    }

    @Override
    public void OnSuccess2(Object o) {

    }

    @Override
    public void OnSuccess3(Object o) {

    }

    @Override
    public void OnSuccess4(Object o) {

    }

    @Override
    public void OnSuccess5(Object o) {

    }

    @Override
    public void OnSuccess6(Object o) {
        if (o != null) {
            ShopCar shopCar = (ShopCar) o;
            List<ShopCar.DataBean> data = shopCar.getData();
            for (int i = 0; i < data.size(); i++) {
                groupBeen.add(new GroupBean(false, data.get(i).getSellerName(), data.get(i).getSellerid()));
                List<ShopCar.DataBean.ListBean> list = data.get(i).getList();
                List<GoodsBean> goodsBeen = new ArrayList<>();
                for (int j = 0; j < list.size(); j++) {
                    goodsBeen.add(new GoodsBean(false, list.get(j).getBargainPrice(), list.get(j).getCreatetime(), list.get(j).getImages(), list.get(j).getNum(), list.get(j).getPrice(), list.get(j).getTitle(), list.get(j).getPid(), false));
                }
                goods.add(goodsBeen);
            }
            for (int i = 0; i < adapter.getGroupCount(); i++) {
                exlist.expandGroup(i);
            }
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void OnSuccess7(Object o) {
        if (o != null) {
            ZhuCeBean zhuCeBean = (ZhuCeBean) o;
            if (zhuCeBean.getCode().equals("0")) {
                Toast.makeText(getApplicationContext(), "删除成功", Toast.LENGTH_LONG).show();
            }
        }
    }

    @Override
    public void OnFailed(Exception e) {

    }

    DecimalFormat df = new DecimalFormat("######0.00");

    //计算和数量总价
    public void changesum(List<List<GoodsBean>> childBeen) {
        int count = 0;
        double sum = 0;
        for (List<GoodsBean> i1 : childBeen) {
            for (int r = 0; r < i1.size(); r++) {
                boolean childCb1 = i1.get(r).isGoodscheck();
                if (childCb1) {
                    double price = i1.get(r).getBargainPrice();
                    int num = i1.get(r).getNum();
                    sum += price * num;
                    count++;
                }
            }
        }
        tvGwcZprice.setText("" + df.format(sum));
        tvGwcCount.setText(count + "");
    }

    public void deleteShop(int pid) {
        HashMap<String, String> map = new HashMap<>();
        map.put("uid", "2606");
        map.put("pid", pid + "");
        presenter.getDelete(map);
    }
}
//MyGoodsAdapter

public class MyGoodsAdapter extends RecyclerView.Adapter<MyGoodsAdapter.ViewHolder> {
    Context context;
    List<Goods.DataBean> list;
    GoodsActivity goodsActivity;

    public MyGoodsAdapter(Context context, List<Goods.DataBean> list, GoodsActivity goodsActivity) {
        this.context = context;
        this.list = list;
        this.goodsActivity = goodsActivity;
    }

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

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.price.setText(list.get(position).getPrice() + "");
        holder.title.setText(list.get(position).getTitle());
        Glide.with(holder.images.getContext()).load(list.get(position).getImages().split("\\|")[0]).into(holder.images);
        final int pid = list.get(position).getPid();
        holder.title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goodsActivity.getGoodsXiang(pid);
            }
        });
        holder.price.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goodsActivity.getGoodsXiang(pid);
            }
        });
        holder.images.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goodsActivity.getGoodsXiang(pid);
            }
        });
    }

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

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView images;
        TextView title, price;

        public ViewHolder(View itemView) {
            super(itemView);
            images = itemView.findViewById(R.id.images);
            price = itemView.findViewById(R.id.goods_title);
            title = itemView.findViewById(R.id.goods_price);
        }
    }
}
//MyShopAdapter
public class MyShopAdapter extends BaseExpandableListAdapter {
    Context context;
    List<GroupBean> groupBeen;
    List<List<GoodsBean>> goods;
    GouWuCheActivity gouWuCheActivity;

    public MyShopAdapter(Context context, List<GroupBean> groupBeen, List<List<GoodsBean>> goods, GouWuCheActivity gouWuCheActivity) {
        this.context = context;
        this.groupBeen = groupBeen;
        this.goods = goods;
        this.gouWuCheActivity = gouWuCheActivity;
    }

    @Override
    public int getGroupCount() {
        return groupBeen.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return goods.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupBeen.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return goods.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View view = View.inflate(context, R.layout.group_item, null);
        final CheckBox check_group = view.findViewById(R.id.check_gwc_group);
        TextView group = view.findViewById(R.id.tv_gwc_group);
        check_group.setChecked(groupBeen.get(groupPosition).isGroupcheck());
        group.setText(groupBeen.get(groupPosition).getSellerName());
        check_group.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GroupBean group = groupBeen.get(groupPosition);
                group.setGroupcheck(check_group.isChecked());
                for (int j = 0; j < groupBeen.size(); j++) {
                    boolean groupCheck = groupBeen.get(j).isGroupcheck();
                    if (!groupCheck) {
                        gouWuCheActivity.checkGwcQx.setChecked(false);
                        break;
                    } else {
                        gouWuCheActivity.checkGwcQx.setChecked(true);
                    }
                }
                List<GoodsBean> goodsBeen = goods.get(groupPosition);
                for (int i = 0; i < goodsBeen.size(); i++) {
                    goodsBeen.get(i).setGoodscheck(check_group.isChecked());
                }
                //计算价格
                gouWuCheActivity.changesum(goods);
                notifyDataSetChanged();
            }
        });
        return view;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View view = View.inflate(context, R.layout.goods_item, null);
        TextView tv_goods = view.findViewById(R.id.tv_gwc_goods);
        TextView price = view.findViewById(R.id.tv_gwc_price);
        final CheckBox check_goods = view.findViewById(R.id.check_gwc_goods);
        ImageView img_goods = view.findViewById(R.id.img_gwc_goods);
        Button btn_delete = view.findViewById(R.id.btn_gwc_delete);
        final AddDeleteView adv = view.findViewById(R.id.add_delete);
        tv_goods.setText(goods.get(groupPosition).get(childPosition).getTitle());
        price.setText(goods.get(groupPosition).get(childPosition).getBargainPrice() + "");
        check_goods.setChecked(goods.get(groupPosition).get(childPosition).isGoodscheck());
        String images = goods.get(groupPosition).get(childPosition).getImages();
        String[] split = images.split("\\|");
        Glide.with(img_goods.getContext()).load(split[0]).into(img_goods);
        adv.setNumber(goods.get(groupPosition).get(childPosition).getNum());
        if (goods.get(groupPosition).get(childPosition).isBtn()) {
            btn_delete.setVisibility(View.VISIBLE);
        } else {
            btn_delete.setVisibility(View.INVISIBLE);
        }
        adv.setOnAddDelClickListener(new AddDeleteView.OnAddDelClickListener() {
            @Override
            public void onAddClick(View v) {
                int number = adv.getNumber();
                number++;
                adv.setNumber(number);
                goods.get(groupPosition).get(childPosition).setNum(number);
                gouWuCheActivity.changesum(goods);
            }

            @Override
            public void onDelClick(View v) {
                int number = adv.getNumber();
                if (number > 1) {
                    number--;
                }
                adv.setNumber(number);
                goods.get(groupPosition).get(childPosition).setNum(number);
                gouWuCheActivity.changesum(goods);
            }
        });
        btn_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gouWuCheActivity.deleteShop(goods.get(groupPosition).get(childPosition).getPid());
                int size = goods.get(groupPosition).size();
                if (goods.get(groupPosition).get(childPosition).isGoodscheck()) {
                    if (size == 1) {
                        goods.remove(groupPosition);
                        groupBeen.remove(groupPosition);
                    } else {
                        goods.get(groupPosition).remove(childPosition);
                    }
                    gouWuCheActivity.changesum(goods);
                    notifyDataSetChanged();
                } else {
                    Toast.makeText(context, "请选择商品。。。", Toast.LENGTH_SHORT).show();
                }
            }
        });
        check_goods.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //定义一个默认flag
                boolean flag = false;
                //获得当前二级列表复选框的状态
                boolean cchecked = check_goods.isChecked();
                //把当前状态存储到二级列表List集合里面
                goods.get(groupPosition).get(childPosition).setGoodscheck(cchecked);

                //循环遍历二级列表List集合
                for (List<GoodsBean> i1 : goods) {
                    // 按照所有二级列表的item的个数进行循环遍历
                    for (int r = 0; r < i1.size(); r++) {
                        //获取当前二级列表的状态
                        boolean childCb1 = i1.get(r).isGoodscheck();
                        //取反设置 如果为true改为false 如果为false改为true
                        if (!childCb1) {
                            //如果当前二级列表中有一条复选框是false  全选为false
                            gouWuCheActivity.checkGwcQx.setChecked(false);
                            //如果当前二级列表中有一条复选框是false  一级列表为false
                            groupBeen.get(groupPosition).setGroupcheck(false);
                            //flagtrue跳出循环
                            flag = true;
                            break;
                        } else {
                            //如果所有的二级列表都为true  全选为true
                            gouWuCheActivity.checkGwcQx.setChecked(true);
                            //如果所有的二级列表都为true  一级列表为true
                            groupBeen.get(groupPosition).setGroupcheck(true);
                        }
                    }
                    //falgtrue时跳出循环
                    if (flag) {
                        break;
                    }
                }
                //当前二级列表的总长度
                int size = goods.get(groupPosition).size();
                //按照当前二级列表的总长度循环
                for (int x = 0; x < size; x++) {
                    //获得当前二级列表中每一个item的选中状态
                    boolean childCb1 = goods.get(groupPosition).get(x).isGoodscheck();
                    //判断
                    if (!childCb1) {
                        //有一个flase 一级列表就设置false  跳出循环
                        groupBeen.get(groupPosition).setGroupcheck(false);
                        break;
                    } else {
                        groupBeen.get(groupPosition).setGroupcheck(true);
                    }
                }
                //计算价格
                gouWuCheActivity.changesum(goods);
                //刷新适配器
                notifyDataSetChanged();
            }
        });
        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值