zoukankan      html  css  js  c++  java
  • Eventbus-Rxjava购物车

    依赖

     compile 'com.squareup.retrofit2:retrofit:2.3.0'
        compile 'com.squareup.retrofit2:converter-gson:2.3.0'
        compile 'io.reactivex.rxjava2:rxjava:2.1.7'
        compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
        compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
        compile 'com.facebook.fresco:fresco:1.5.0'
        compile 'org.greenrobot:eventbus:3.1.1'
        compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.4-4'
        compile 'com.dou361.ijkplayer:jjdxm-ijkplayer:1.0.6'
    

      

    eventbus包下
    
    public class Datas {
    
        private String pid;
        private String sellerid;
        private int selected;
        private String num;
    
        public String getPid() {
            return pid;
        }
    
        public void setPid(String pid) {
            this.pid = pid;
        }
    
        public String getSellerid() {
            return sellerid;
        }
    
        public void setSellerid(String sellerid) {
            this.sellerid = sellerid;
        }
    
        public int getSelected() {
            return selected;
        }
    
        public void setSelected(int selected) {
            this.selected = selected;
        }
    
        public String getNum() {
            return num;
        }
    
        public void setNum(String num) {
            this.num = num;
        }
    }
    
    
    ======
    public class MessageEvent {
        private boolean checked;
    
        public boolean isChecked() {
            return checked;
        }
    
        public void setChecked(boolean checked) {
            this.checked = checked;
        }
    }
    
    
    =====
    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;
        }
    }
    

      

    M层

    public class GetCardModel implements IGetCardModel {
        @Override
        public void getCard( String uid, final OnListiner onListiner) {
            Flowable<CartBean> cart = RetrofitHelper.getApi().getCart(uid);
            cart.doOnSubscribe(new Consumer<Subscription>() {
                @Override
                public void accept(Subscription subscription) throws Exception {
                    Log.d("ssss","开始请求数据");
                }
            }).subscribeOn(Schedulers.io())
              .observeOn(AndroidSchedulers.mainThread())
              .subscribe(new Consumer<CartBean>() {
                  @Override
                  public void accept(CartBean cartBean) throws Exception {
                      onListiner.onSuccess(cartBean);
                  }
              })  ;
        }
    }
    
    
    
    public interface IGetCardModel<T> {
        public void getCard(String uid, OnListiner onListiner);
    }
    

      P层

    public class GetCardPresenter {
        private ICartFragment iCartFragment;
        private IGetCardModel iGetCardModel;
    
        public GetCardPresenter(ICartFragment iCartFragment) {
            this.iCartFragment = iCartFragment;
            iGetCardModel = new GetCardModel();
        }
    
        public void getCarts() {
            iGetCardModel.getCard("71", new OnListiner() {
                @Override
                public void onSuccess(Object o) {
                    iCartFragment.onShow((CartBean) o);
                }
    
                @Override
                public void onFailure(Throwable t) {
                    t.getMessage();
                }
            });
        }
    }
    

      V层

    public class MainActivity extends AppCompatActivity implements View.OnClickListener,ICartFragment {
        private GetCardPresenter presenter;
        private MyAdapter adapter;
        private View view;
        /**
         * c
         */
        private TextView mFan;
        /**
         * 编辑
         */
        private TextView mBj;
        private ExpandableListView mElv;
        /**
         * 全选
         */
        private CheckBox mCheckAll;
        /**
         * 分享宝贝
         */
        private Button mShare;
        /**
         * 移到收藏栏
         */
        private Button mFile;
        /**
         * 删除
         */
        private Button mDele;
        private LinearLayout mCaozuo;
        /**
         * 价钱
         */
        private TextView mPriceAll;
        /**
         * 结算
         */
        private Button mJs;
        private RelativeLayout mJiesuan;
        private   List<List<CartBean.DataBean.ListBean>> lists = new ArrayList<>();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
            EventBus.getDefault().register(this);
            presenter = new GetCardPresenter(this);
            presenter.getCarts();
            mCaozuo.setVisibility(View.GONE);
        }
    
        @Override
        public void onShow(CartBean cartBean) {
           Toast.makeText(getApplicationContext(),cartBean.getCode(), Toast.LENGTH_SHORT).show();
            List<CartBean.DataBean> data = cartBean.getData();
    
            for (int i = 0; i < data.size(); i++) {
                data.get(i).setBj("编辑");
                data.get(i).setWc("完成");
                List<CartBean.DataBean.ListBean> list = data.get(i).getList();
                lists.add(list);
            }
          adapter = new MyAdapter(this, data, lists);
            mElv.setAdapter(adapter);
            for (int i=0; i<data.size(); i++)
            {
                mElv.expandGroup(i);
            }
        }
    
        private void initView() {
            mFan = (TextView) findViewById(R.id.fan);
            mBj = (TextView) findViewById(R.id.bj);
            mElv = (ExpandableListView) findViewById(R.id.elv);
            mCheckAll = (CheckBox) findViewById(R.id.check_all);
            mShare = (Button) findViewById(R.id.share);
            mShare.setOnClickListener(this);
            mFile = (Button) findViewById(R.id.file);
            mFile.setOnClickListener(this);
            mDele = (Button) findViewById(R.id.dele);
            mDele.setOnClickListener(this);
            mCaozuo = (LinearLayout) findViewById(R.id.caozuo);
            mPriceAll = (TextView) findViewById(R.id.price_all);
            mJs = (Button) findViewById(R.id.js);
            mJs.setOnClickListener(this);
            mJiesuan = (RelativeLayout) findViewById(R.id.jiesuan);
            mCheckAll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //changeAllListCbState
                    adapter.changeAllListCbState(mCheckAll.isChecked());
                }
            });
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                default:
                    break;
                case R.id.share:
                    break;
                case R.id.file:
                    break;
                case R.id.dele:
                    break;
                case R.id.js:
                    break;
            }
        }
        @Subscribe
        public void onMessageEvent(MessageEvent event) {
            mCheckAll.setChecked(event.isChecked());
        }
    
        @Subscribe
        public void onMessageEvent(PriceAndCountEvent event) {
            mPriceAll.setText("结算(" + event.getCount() + ")"+event.getPrice() + "");
        }
        @Override
        public void onDestroy() {
            super.onDestroy();
            EventBus.getDefault().unregister(this);
        }
    
    
    }
    
    
    
    
    
    
    public interface ICartFragment {
      public void onShow(CartBean cartBean);
    
    }
    

      XML

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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.bwei.wsq.gwuche.MainActivity"
        android:orientation="vertical">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="#f70828"
            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="c"
                android:textSize="26sp"
                android:layout_centerVertical="true"
                android:layout_marginLeft="10dp"
                android:id="@+id/fan"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="购物车"
                android:layout_centerInParent="true"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="编辑"
                android:id="@+id/bj"
                android:layout_centerInParent="true"
                android:layout_alignParentRight="true"
                android:layout_marginRight="10dp"
                />
        </RelativeLayout>
        <ExpandableListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/elv"
            android:layout_weight="2"
            ></ExpandableListView>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="8"
            >
            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="全选"
                android:id="@+id/check_all"
                android:layout_centerVertical="true"
                />
            <LinearLayout
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:orientation="horizontal"
                android:id="@+id/caozuo"
                >
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/share"
                    android:background="#ed1ad8"
                    android:textColor="#ffffff"
                    android:text="分享宝贝"
                    android:layout_margin="5dp"
                    />
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/file"
                    android:background="#ed1ad8"
                    android:textColor="#ffffff"
                    android:text="移到收藏栏"
                    android:layout_margin="5dp"
    
    
                    />
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/dele"
                    android:background="#f40616"
                    android:textColor="#ffffff"
                    android:text="删除"
                    android:layout_margin="5dp"
                    />
            </LinearLayout>
            <RelativeLayout
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:id="@+id/jiesuan"
                android:layout_centerVertical="true"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/price_all"
                    android:layout_marginLeft="100dp"
                    android:text="价钱"
                    android:layout_centerVertical="true"
    
                    />
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/js"
                    android:text="结算"
                    android:layout_margin="5dp"
                    android:background="#f40820"
                    android:textColor="#ffffff"
                    android:layout_alignParentRight="true"
                    />
            </RelativeLayout>
    
        </RelativeLayout>
    
    </LinearLayout>
    
    
    
    
    child_item
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:id="@+id/sel"
        android:layout_gravity="center_vertical"
        />
        <com.facebook.drawee.view.SimpleDraweeView
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:id="@+id/img"
            />
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
    
            >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:id="@+id/show"
                >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/name"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/jie_shao"
                />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/price"
                    android:textColor="#edb519"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/prices"
                    android:layout_marginLeft="20dp"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/num"
                    android:layout_marginLeft="20dp"
                    />
    
    
            </LinearLayout>
            </LinearLayout>
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/hide"
                >
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:layout_centerVertical="true"
                    >
                    <TextView
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:id="@+id/end"
                        android:text="-"
                        android:background="#bac2ec"
                        android:gravity="center"
                        />
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/num01"
                        android:text="111"
                        />
                    <TextView
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:id="@+id/add"
                        android:text="+"
                        android:gravity="center"
                        android:background="#bac2ec"
                        />
                </LinearLayout>
    
    
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:id="@+id/del"
                    android:background="#f70d24"
                    android:layout_alignParentRight="true"
                    android:text="删除"
                    android:textColor="#ffffff"
                    />
            </RelativeLayout>
        </RelativeLayout>
    
    
    
    </LinearLayout>
    
    
    group_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">
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/shop_name"
        android:layout_centerVertical="true"
        />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/item_bj"
            android:text="编辑"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="20dp"
            />
    </RelativeLayout>
    

      Myadapter

    public class MyAdapter extends BaseExpandableListAdapter {
        private Context context;
        private List<CartBean.DataBean> groupList;
        private List<List<CartBean.DataBean.ListBean>> childList;
        private LayoutInflater inflater;
    
        private AlertDialog show;
        private Boolean i01 = false;
    
        public MyAdapter(Context context, List<CartBean.DataBean> groupList, List<List<CartBean.DataBean.ListBean>> childList) {
            this.context = context;
            this.groupList = groupList;
            this.childList = childList;
            inflater = LayoutInflater.from(context);
        }
    
        @Override
        public int getGroupCount() {
            return groupList.size();
        }
    
        @Override
        public int getChildrenCount(int groupPosition) {
            return childList.get(groupPosition).size();
        }
    
        @Override
        public Object getGroup(int groupPosition) {
            return groupList.get(groupPosition);
        }
    
        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return childList.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 true;
        }
    
        @Override
        public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            final GroupViewHolder holder1;
            View view = null;
            if(convertView == null)
            {
                holder1 = new GroupViewHolder();
                view = inflater.inflate(R.layout.group_item, parent, false);
                holder1.shopName = view.findViewById(R.id.shop_name);
                holder1.itemBj = view.findViewById(R.id.item_bj);
                view.setTag(holder1);
            }
            else {
                view = convertView;
                holder1 = (GroupViewHolder) view.getTag();
            }
            final CartBean.DataBean dataBean = groupList.get(groupPosition);
            holder1.shopName.setText(dataBean.getSellerName());
            Boolean check = dataBean.getCheck();
            holder1.shopName.setChecked(dataBean.getCheck());
            holder1.itemBj.setText(dataBean.getBj());
    
            holder1.itemBj.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                  //  List<CartBean.DataBean.ListBean> listBeans = childList.get(groupPosition);
                    List<CartBean.DataBean.ListBean> list = dataBean.getList();
                    if(i01==false)
                    {   i01=true;
                       dataBean.setBj("完成");
                        for(int i = 0;i<list.size();i++)
                        {
                            list.get(i).setShow(false);
    
                        }
                        notifyDataSetChanged();
                    }
                    else {
                        i01=false;
                        dataBean.setBj("编辑");
                        for(int i = 0;i<list.size();i++)
                        {
                            list.get(i).setShow(true);
    
                        }
                        notifyDataSetChanged();
                    }
    
                   notifyDataSetChanged();
                }
            });
            holder1.shopName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dataBean.setCheck(holder1.shopName.isChecked());
                    changeChildCbState(groupPosition, holder1.shopName.isChecked());
                    EventBus.getDefault().post(compute());
                    changeAllCbState(isAllGroupCbSelected());
                    notifyDataSetChanged();
                }
            });
            return view;
        }
    
        @Override
        public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            final ChildViewHolder holder;
            View view = null;
            if(convertView == null)
            {
                holder = new ChildViewHolder();
                view = inflater.inflate(R.layout.child_item, parent, false);
                holder.dele = view.findViewById(R.id.del);
                holder.add = view.findViewById(R.id.add);
                holder.end = view.findViewById(R.id.end);
                holder.sel = view.findViewById(R.id.sel);
                holder.img = view.findViewById(R.id.img);
                holder.name = view.findViewById(R.id.name);
                holder.price = view.findViewById(R.id.price);
                holder.prices = view.findViewById(R.id.prices);
                holder.jieShao = view.findViewById(R.id.jie_shao);
                holder.num = view.findViewById(R.id.num);
                holder.show = view.findViewById(R.id.show);
                holder.hide = view.findViewById(R.id.hide);
                holder.num01 = view.findViewById(R.id.num01);
                view.setTag(holder);
            }
            else {
                view = convertView;
                holder = (ChildViewHolder) view.getTag();
            }
            final CartBean.DataBean.ListBean listBean = childList.get(groupPosition).get(childPosition);
            holder.hide.setVisibility(View.GONE);
            holder.jieShao.setText(listBean.getSubhead());
            holder.num.setText("数量:"+listBean.getNum());
            holder.num01.setText(listBean.getNum()+"");
            holder.price.setText("$"+listBean.getPrice());
            holder.prices.setText("$"+listBean.getBargainPrice());
            holder.sel.setChecked(listBean.getCheck());
            String images = listBean.getImages();
            String[] split = images.split("\|");
            Uri parse = Uri.parse(split[0]);
            holder.img.setImageURI(parse);
            holder.name.setText(listBean.getTitle());
            int pid = listBean.getPid();
            final String pid01  = pid+"";
            final int selected = listBean.getSelected();
            final String sellerid = listBean.getSellerid()+"";
            final String num = listBean.getNum()+"";
            if(listBean.getShow()==false)
            {
                holder.hide.setVisibility(View.VISIBLE);
                holder.show.setVisibility(View.GONE);
            }
            else{
                holder.hide.setVisibility(View.GONE);
                holder.show.setVisibility(View.VISIBLE);
            }
            holder.sel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //设置该条目对象里的checked属性值
                    listBean.setCheck(holder.sel.isChecked());
                    PriceAndCountEvent priceAndCountEvent = compute();
                    EventBus.getDefault().post(priceAndCountEvent);
    
                    if (holder.sel.isChecked()) {
                        //当前checkbox是选中状态
                        if (isAllChildCbSelected(groupPosition)) {
                            changGroupCbState(groupPosition, true);
                            changeAllCbState(isAllGroupCbSelected());
                        }
                    } else {
                        changGroupCbState(groupPosition, false);
                        changeAllCbState(isAllGroupCbSelected());
                    }
                    notifyDataSetChanged();
                }
            });
            //加号
            holder.add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int num1 = listBean.getNum();
                    num1++;
                    holder.num01.setText(num1 + "");
                    holder.num.setText(num1+"");
                    listBean.setNum(num1);
                    if (holder.sel.isChecked()) {
                        PriceAndCountEvent priceAndCountEvent = compute();
                        EventBus.getDefault().post(priceAndCountEvent);
                    }
                }
            });
            //减号
            holder.end.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int num = listBean.getNum();
                    if (num == 1) {
                        return;
                    }
                    holder.num01.setText(--num + "");
                    holder.num.setText(num+"");
                    listBean.setNum(num);
                    if (holder.sel.isChecked()) {
                        PriceAndCountEvent priceAndCountEvent = compute();
                        EventBus.getDefault().post(priceAndCountEvent);
                    }
                }
            });
            //删除
            holder.dele.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
    
                    final AlertDialog.Builder normalDialog =
                            new AlertDialog.Builder(context);
                    normalDialog.setIcon(R.drawable.selectheart);
                    normalDialog.setTitle("确认要删除此商品吗");
                    normalDialog.setPositiveButton("确定",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    //...To-do
                                    RetrofitHelper.getApi().deleteGoodCar("71", childList.get(groupPosition).get(childPosition).getPid() + "")
                                    .subscribeOn(Schedulers.io())
                                            .observeOn(AndroidSchedulers.mainThread())
                                            .subscribe(new Observer<BaseBean>() {
                                                @Override
                                                public void onSubscribe(Disposable d) {
    
                                                }
    
                                                @Override
                                                public void onNext(BaseBean baseBean) {
                                                    if (baseBean.getCode().equals("0")){
                                                        List<CartBean.DataBean.ListBean> datasBeen = childList.get(groupPosition);
                                                        CartBean.DataBean.ListBean remove = datasBeen.remove(childPosition);
                                                        if (datasBeen.size() == 0) {
                                                            childList.remove(groupPosition);
                                                            groupList.remove(groupPosition);
                                                        }
                                                        EventBus.getDefault().post(compute());
                                                        notifyDataSetChanged();
                                                    }else{
                                                        Toast.makeText(context,"删除失败",Toast.LENGTH_SHORT).show();
                                                    }
                                                }
    
                                                @Override
                                                public void onError(Throwable e) {
    
                                                }
    
                                                @Override
                                                public void onComplete() {
    
                                                }
                                            });
    
                                }
                            });
                    normalDialog.setNegativeButton("关闭",
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    //...To-do
                                    show.dismiss();
                                }
                            });
                    // 显示
                    show = normalDialog.show();
    
                }
            });
            notifyDataSetChanged();
    //
            return view;
        }
    
        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
        class GroupViewHolder{
            CheckBox shopName;
            TextView itemBj;
    
        }
        class ChildViewHolder{
            CheckBox sel;
            SimpleDraweeView img;
            TextView name;
            TextView jieShao;
            TextView price;
            TextView prices;
            TextView num;
            TextView num01;
            Button dele;
            TextView add;
            TextView end;
            LinearLayout show;
            RelativeLayout hide;
    
    
        }
        /**
         * 计算列表中,选中的钱和数量
         */
        private PriceAndCountEvent compute() {
            int count = 0;
            int price = 0;
            for (int i = 0; i < childList.size(); i++) {
                List<CartBean.DataBean.ListBean> listBeans = childList.get(i);
                for (int j = 0; j < listBeans.size(); j++) {
                    CartBean.DataBean.ListBean listBean = listBeans.get(j);
                    if (listBean.getCheck()) {
                        price += listBean.getNum() * listBean.getPrice();
                        count += listBean.getNum();
                    }
                }
            }
            PriceAndCountEvent priceAndCountEvent = new PriceAndCountEvent();
            priceAndCountEvent.setCount(count);
            priceAndCountEvent.setPrice(price);
            return priceAndCountEvent;
        }
        /**
         * 设置全选、反选
         *
         * @param flag
         */
        public void changeAllListCbState(boolean flag) {
            for (int i = 0; i < groupList.size(); i++) {
                changGroupCbState(i, flag);
                changeChildCbState(i, flag);
            }
            EventBus.getDefault().post(compute());
            notifyDataSetChanged();
        }
        /**
         * 判断一级列表是否全部选中
         *
         * @return
         */
        private boolean isAllGroupCbSelected() {
            for (int i = 0; i < groupList.size(); i++) {
                CartBean.DataBean dataBean = groupList.get(i);
                if (!dataBean.getCheck()) {
                    return false;
                }
            }
            return true;
        }
    
        /**
         * 判断二级列表是否全部选中
         *
         * @param groupPosition
         * @return
         */
        private boolean isAllChildCbSelected(int groupPosition) {
            List<CartBean.DataBean.ListBean> listBeans = childList.get(groupPosition);
            for (int i = 0; i < listBeans.size(); i++) {
                CartBean.DataBean.ListBean listBean = listBeans.get(i);
                if (!listBean.getCheck()) {
                    return false;
                }
            }
            return true;
        }
        /**
         * 改变全选的状态
         *
         * @param flag
         */
        private void changeAllCbState(boolean flag) {
            MessageEvent messageEvent = new MessageEvent();
            messageEvent.setChecked(flag);
            EventBus.getDefault().post(messageEvent);
        }
    
        /**
         * 改变一级列表checkbox状态
         *
         * @param groupPosition
         */
        private void changGroupCbState(int groupPosition, boolean flag) {
            CartBean.DataBean dataBean = groupList.get(groupPosition);
            dataBean.setCheck(flag);
        }
    
        /**
         * 改变二级列表checkbox状态
         *
         * @param groupPosition
         * @param flag
         */
        private void changeChildCbState(int groupPosition, boolean flag) {
            List<CartBean.DataBean.ListBean> listBeans = childList.get(groupPosition);
            for (int i = 0; i < listBeans.size(); i++) {
                CartBean.DataBean.ListBean listBean = listBeans.get(i);
                listBean.setCheck(flag);
            }
        }
    }
    

      

    UrlUtils
    
    public class UrlUtils {
        public static final String BASE_URL="https://www.zhaoapi.cn/";
        //查询购物车
        public static final String SELECTCAR_PATH_URL = "product/getCarts";
        //删除购物车(新增)
        public static final String DELETECAR_PATH_URL = "product/deleteCart";
    }
    
    
    ServiceApi 
    
    public interface ServiceApi {
       //查询购物车
       @GET(UrlUtils.SELECTCAR_PATH_URL)
       public Flowable<CartBean> getCart(@Query("uid") String uid);
    
       //删除购物车
       @GET(UrlUtils.DELETECAR_PATH_URL)
       Observable<BaseBean> deleteGoodCar(@Query("uid") String uid, @Query("pid") String pid);
    }
    
    
    
    RetrofitHelper 
    
    public class RetrofitHelper {
        private static OkHttpClient client;
        private static ServiceApi api;
        static {
            initOkHttp();
        }
    
        private static void initOkHttp() {
            if (client == null) {
                synchronized (OkHttpClient.class) {
                    if (client == null) {
                        client = new OkHttpClient.Builder()
                                .addInterceptor(new MyInterceptor())
                                .build();
                    }
                }
            }
        }
    
        public static ServiceApi getApi() {
            if (api == null) {
                synchronized (ServiceApi.class) {
                    if (api == null) {
                        api = RetrofitHelper.create(ServiceApi.class, UrlUtils.BASE_URL);
                    }
                }
            }
            return api;
        }
    
        private static <T> T create(Class<T> tClass, String baseUrl) {
            Retrofit re = new Retrofit.Builder()
                    .client(client)
                    .baseUrl(baseUrl)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();
            return re.create(tClass);
        }
    }
    
    OnListiner
    
    public interface OnListiner<T> {
        public void onSuccess(T t);
        public void onFailure(Throwable throwable);
    
    }
    
    
    
    MyInterceptor 
    
    public class MyInterceptor implements Interceptor {
        private String newUri;
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            URI uri = request.url().uri();
            String query = uri.getQuery();
    
            String host = uri.getHost();
            String path = uri.getPath();
            if (query == null){
                newUri = "https://"+host + path +"?source=android";
            }else{
                newUri = "https://"+host + path +"?"+query+"&source=android";
            }
    
            Log.e("tag",newUri);
            Request request1 = request.newBuilder().url(newUri).build();
            Response response = chain.proceed(request1);
    
            return response;
        }
    }
    
    bean类
    BaseBean 
    public class BaseBean {
    
        /**
         * msg : 删除购物车成功
         * code : 0
         */
    
    
    
        private String msg;
        private String code;
    
        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;
        }
    }
    
    
    
    
    CartBean 
    
    public class CartBean {
    
    
        /**
         * msg : 请求成功
         * code : 0
         * data : [{"list":[{"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":7,"pid":1,"price":118,"pscid":1,"selected":0,"sellerid":17,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家17","sellerid":"17"},{"list":[{"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":1,"pid":2,"price":299,"pscid":1,"selected":0,"sellerid":18,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家18","sellerid":"18"},{"list":[{"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","num":1,"pid":3,"price":198,"pscid":1,"selected":0,"sellerid":19,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家19","sellerid":"19"}]
         */
    
        private String msg;
        private String code;
        private List<DataBean> data;
    
    
        public String getMsg() {
            return msg;
        }
    
        public void setMsg(String msg) {
            this.msg = msg;
        }
    
        public String getCode() {
            return code;
        }
    
        public void setCode(String code) {
            this.code = code;
        }
    
        public List<DataBean> getData() {
            return data;
        }
    
        public void setData(List<DataBean> data) {
            this.data = data;
        }
    
        public static class DataBean {
            /**
             * list : [{"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":7,"pid":1,"price":118,"pscid":1,"selected":0,"sellerid":17,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}]
             * sellerName : 商家17
             * sellerid : 17
             */
    
            private String sellerName;
            private String sellerid;
            private List<ListBean> list;
            private String bj="编辑01";
            private Boolean check = false;
            private String wc="完成";
    
            public String getWc() {
    
                return wc;
            }
    
            public void setWc(String wc) {
                this.wc = wc;
            }
    
            public Boolean getCheck() {
                return check;
            }
    
            public void setCheck(Boolean check) {
                this.check = check;
            }
    
            public String getBj() {
                return bj;
            }
    
            public void setBj(String bj) {
                this.bj = bj;
            }
            public String getSellerName() {
                return sellerName;
            }
    
            public void setSellerName(String sellerName) {
                this.sellerName = sellerName;
            }
    
            public String getSellerid() {
                return sellerid;
            }
    
            public void setSellerid(String sellerid) {
                this.sellerid = sellerid;
            }
    
            public List<ListBean> getList() {
                return list;
            }
    
            public void setList(List<ListBean> list) {
                this.list = list;
            }
    
            public static class ListBean {
                /**
                 * bargainPrice : 111.99
                 * createtime : 2017-10-14T21:39:05
                 * detailUrl : https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends
                 * images : https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg
                 * num : 7
                 * pid : 1
                 * price : 118.0
                 * pscid : 1
                 * selected : 0
                 * sellerid : 17
                 * subhead : 每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下
                 * title : 北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g
                 */
    
                private double bargainPrice;
                private String createtime;
                private String detailUrl;
                private String images;
                private int num;
                private int pid;
                private double price;
                private int pscid;
                private int selected;
                private int sellerid;
                private String subhead;
                private String title;
                private Boolean isShow=true;
                private Boolean check = false;
    
                public Boolean getCheck() {
                    return check;
                }
    
                public void setCheck(Boolean check) {
                    this.check = check;
                }
    
                public Boolean getShow() {
                    return isShow;
                }
    
                public void setShow(Boolean show) {
                    isShow = show;
                }
    
                public double getBargainPrice() {
                    return bargainPrice;
                }
    
                public void setBargainPrice(double bargainPrice) {
                    this.bargainPrice = bargainPrice;
                }
    
                public String getCreatetime() {
                    return createtime;
                }
    
                public void setCreatetime(String createtime) {
                    this.createtime = createtime;
                }
    
                public String getDetailUrl() {
                    return detailUrl;
                }
    
                public void setDetailUrl(String detailUrl) {
                    this.detailUrl = detailUrl;
                }
    
                public String getImages() {
                    return images;
                }
    
                public void setImages(String images) {
                    this.images = images;
                }
    
                public int getNum() {
                    return num;
                }
    
                public void setNum(int num) {
                    this.num = num;
                }
    
                public int getPid() {
                    return pid;
                }
    
                public void setPid(int pid) {
                    this.pid = pid;
                }
    
                public 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 getSelected() {
                    return selected;
                }
    
                public void setSelected(int selected) {
                    this.selected = selected;
                }
    
                public int getSellerid() {
                    return sellerid;
                }
    
                public void setSellerid(int sellerid) {
                    this.sellerid = sellerid;
                }
    
                public String getSubhead() {
                    return subhead;
                }
    
                public void setSubhead(String subhead) {
                    this.subhead = subhead;
                }
    
                public String getTitle() {
                    return title;
                }
    
                public void setTitle(String title) {
                    this.title = title;
                }
            }
        }
    }
    

      

  • 相关阅读:
    HTML <form> 标签的 accept-charset 属性
    SpringMVC中Controller跳转到另一个Controller方法
    DIV层+CSS实现锁屏
    工作组模式下专用队列(Private Queue)如何引用远程队列路径
    stl之deque双端队列容器
    百度之星资格赛——Disk Schedule(双调旅行商问题)
    android 无线模式下使用ADB调试
    HDU
    NoSQL 数据库产品学习总结(一)
    ThinkPad E431怎样关闭触摸板
  • 原文地址:https://www.cnblogs.com/wsq110/p/8127486.html
Copyright © 2011-2022 走看看