Fragment购物车页面 (快捷键)

例:

Fragment购物车页面 (快捷键)_第1张图片


shopping_cart_checked

shopping_cart_none_check




//Fragment购物车页面  
    //如果没有数据就换一个 uid private int uid=71;public class FrgamenGwcFragment extends Fragment implements IMainActivity {
    private MyExpandable expanableListView;
    private MainPresenter mainPresenter;
    private CheckBox check_all;
    private TextView text_total;
    private TextView text_buy;
    private View view;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0){
                final CountPriceBean countPriceBean = (CountPriceBean) msg.obj;
                text_total.setText("合计:¥"+countPriceBean.getPriceString());
                text_buy.setText("去结算("+countPriceBean.getCount()+")");
            }
        }
    };
    private MyAdapter5 myAdapter;
    private int index;
    private SharedPreferences test;
    private int uid=71;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.frgamen_gwc, container, false);
        expanableListView = view.findViewById(R.id.expanable_list_view);
        check_all = view.findViewById(R.id.check_all);
        text_total = view.findViewById(R.id.text_total);
        text_buy = view.findViewById(R.id.text_buy);
        expanableListView.setGroupIndicator(null);
        //获取数据....MVP
        mainPresenter = new MainPresenter(this);
        //全选设置点击事件
        check_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.check_all:
                        //所有孩子的状态跟着全选的状态进行改变
                        if (myAdapter != null){
                            myAdapter.setAllChildChecked(check_all.isChecked());
                        }
                        break;
                }
            }
        });
        return view;
    }
    @Override
    public void onResume() {
        super.onResume();
        expanableListView.setVisibility(View.VISIBLE);
        //调用获取数据的方法
        mainPresenter.getCartData(ApiUtil.selectCartUrl,uid);
    }
    @Override
    public void onCartSuccess(final CartBean cartBean) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (cartBean == null){
                    //需要更改获取的cartBean数据
                    expanableListView.setVisibility(View.GONE);
                    return;
                    //1.根据某一个组中的二级所有的子条目是否选中,确定当前一级列表是否选中
                }else {
                    for (int i = 0;i<cartBean.getData().size();i++){

                        if(isChildInGroupChecked(i,cartBean)){
                            cartBean.getData().get(i).setGroup_check(true);
                        }
                    }
                    //2.设置是否全选选中...根据所有的一级列表是否选中,确定全选是否选中
                    check_all.setChecked(isAllGroupChecked(cartBean));
                    //设置适配器
                    myAdapter = new MyAdapter5(getContext(), cartBean,handler,mainPresenter,uid);
                    expanableListView.setAdapter(myAdapter);
                    //展开所有的组...expanableListView.expandGroup()
                    for (int i = 0;i<cartBean.getData().size();i++){
                        expanableListView.expandGroup(i);
                    }
                    //3.计算总价和商品的数量
                    myAdapter.sendPriceAndCount();
                }
            }
        });
    }
    private boolean isAllGroupChecked(CartBean cartBean) {
        for (int i =0;iif (! cartBean.getData().get(i).isGroup_check()){//表示有没选中的组
                return false;
            }
        }
        return true;
    }
    private boolean isChildInGroupChecked(int i, CartBean cartBean) {
        //当前组中所有子条目的数据
        List listBeans = cartBean.getData().get(i).getList();
        for (int j = 0;jif (listBeans.get(j).getSelected() == 0){//有未选中的条目
                return false;
            }
        }
        return true;
    }
}

//    
////    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
//            android:layout_height="match_parent">
////        android:layout_above="@+id/linear_bottom"
//                android:layout_width="match_parent"
//                android:layout_height="match_parent">
////            android:orientation="vertical"
//                    android:layout_width="match_parent"
//                    android:layout_height="match_parent">
//
////        android:id="@+id/expanable_list_view"
//        android:layout_width="match_parent"
//        android:layout_height="wrap_content"
//        android:visibility="visible"
//        >
//
//
//
//
////        android:orientation="horizontal"
//                android:id="@+id/linear_bottom"
//                android:layout_alignParentBottom="true"
//                android:layout_width="match_parent"
//                android:layout_height="50dp">
////            android:button="@null"
//                    android:background="@drawable/check_box_selector"
//                    android:layout_marginLeft="10dp"
//                    android:id="@+id/check_all"
//                    android:layout_width="wrap_content"
//                    android:layout_height="wrap_content" />
////            android:id="@+id/text_total"
//                    android:text="合计:¥0.00"
//                    android:layout_gravity="center_vertical"
//                    android:layout_marginLeft="10dp"
//                    android:layout_width="0dp"
//                    android:layout_weight="1"
//                    android:layout_height="wrap_content" />
////            android:id="@+id/text_buy"
//                    android:background="#ff0000"
//                    android:textColor="#ffffff"
//                    android:gravity="center"
//                    android:text="去结算(0)"
//                    android:layout_width="100dp"
//                    android:layout_height="match_parent" />
//
//

//@drawable/check_box_selector
//    
//
//
//
//
//

//public interface IMainActivity {
//    void onCartSuccess(CartBean cartBean);
//}

//public class MyExpandable extends ExpandableListView {
//    public MyExpandable(Context context) {
//        super(context);
//    }
//    public MyExpandable(Context context, AttributeSet attrs) {
//        super(context, attrs);
//    }
//    public MyExpandable(Context context, AttributeSet attrs, int defStyleAttr) {
//        super(context, attrs, defStyleAttr);
//    }
//    @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        int height = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST);
//        super.onMeasure(widthMeasureSpec, height);
//    }
//}

//public class CartBean {
//    /**
//     * code : 0
//     * data : [{"list":[{"bargainPrice":99,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/4345173.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6037/35/2944615848/95178/6cd6cff0/594a3a10Na4ec7f39.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6607/258/1025744923/75738/da120a2d/594a3a12Ne3e6bc56.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6370/292/1057025420/64655/f87644e3/594a3a12N5b900606.jpg!q70.jpg","num":1,"pid":45,"price":2999,"pscid":39,"selected":0,"sellerid":1,"subhead":"高清双摄,就是清晰!2000+1600万高清摄像头,6GB大内存+高通骁龙835处理器,性能怪兽!","title":"一加手机5 (A5000) 6GB+64GB 月岩灰 全网通 双卡双待 移动联通电信4G手机"},{"bargainPrice":159,"createtime":"2017-10-14T21:49:15","detailUrl":"https://item.m.jd.com/product/5061723.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8716/197/1271594444/173291/2f40bb4f/59b743bcN8509428e.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8347/264/1286771527/92188/5cf5ec04/59b7420fN65378e9e.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7363/165/3000956253/190883/179a372/59b743bfNd0c79d93.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7399/112/2935531768/183594/b77c7d4a/59b7441aNc3d40133.jpg!q70.jpg","num":1,"pid":90,"price":1233,"pscid":112,"selected":0,"sellerid":1,"subhead":"针织针织闪闪闪亮你的眼","title":"维迩旎 2017秋冬新款长袖针织连衣裙韩版气质中长款名媛包臀A字裙 zx179709 黑色 XL"}],"sellerName":"商家1","sellerid":"1"},{"list":[{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/5025518.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8830/106/1760940277/195595/5cf9412f/59bf2ef5N5ab7dc16.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5428/70/1520969931/274676/b644dd0d/591128e7Nd2f70da0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5566/365/1519564203/36911/620c750c/591128eaN54ac3363.jpg!q70.jpg","num":1,"pid":58,"price":6399,"pscid":40,"selected":0,"sellerid":2,"subhead":"升级4G大显存!Nvme协议Pcie SSD,速度快人一步】GTX1050Ti就选拯救者!专业游戏键盘&新模具全新设计!","title":"联想(Lenovo)拯救者R720 15.6英寸游戏笔记本电脑(i5-7300HQ 8G 1T+128G SSD GTX1050Ti 4G IPS )"},{"bargainPrice":159,"createtime":"2017-10-14T21:49:15","detailUrl":"https://item.m.jd.com/product/5061723.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8716/197/1271594444/173291/2f40bb4f/59b743bcN8509428e.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8347/264/1286771527/92188/5cf5ec04/59b7420fN65378e9e.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7363/165/3000956253/190883/179a372/59b743bfNd0c79d93.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7399/112/2935531768/183594/b77c7d4a/59b7441aNc3d40133.jpg!q70.jpg","num":1,"pid":91,"price":1344,"pscid":112,"selected":0,"sellerid":2,"subhead":"针织针织闪闪闪亮你的眼","title":"维迩旎 2017秋冬新款长袖针织连衣裙韩版气质中长款名媛包臀A字裙 zx179709 黑色 XL"}],"sellerName":"商家2","sellerid":"2"},{"list":[{"bargainPrice":1599,"createtime":"2017-10-14T21:48:08","detailUrl":"https://item.m.jd.com/product/1993026402.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t5863/302/8961270302/97126/41feade1/5981c81cNc1b1fbef.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7003/250/1488538438/195825/53bf31ba/5981c57eN51e95176.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5665/100/8954482513/43454/418611a9/5981c57eNd5fc97ba.jpg!q70.jpg","num":2,"pid":47,"price":111,"pscid":39,"selected":0,"sellerid":3,"subhead":"碳黑色 32GB 全网通 官方标配   1","title":"锤子 坚果Pro 特别版 巧克力色 酒红色 全网通 移动联通电信4G手机 双卡双待 碳黑色 32GB 全网通"},{"bargainPrice":5599,"createtime":"2017-10-10T17:30:32","detailUrl":"https://item.m.jd.com/product/4824715.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n12/jfs/t7768/184/1153704394/148460/f42e1432/599a930fN8a85626b.jpg!q70.jpg","num":1,"pid":59,"price":5599,"pscid":40,"selected":0,"sellerid":3,"subhead":"游戏本选择4G独显,拒绝掉帧】升级版IPS全高清防眩光显示屏,WASD方向键颜色加持,三大出风口立体散热!","title":"戴尔DELL灵越游匣15PR-6648B GTX1050 15.6英寸游戏笔记本电脑(i5-7300HQ 8G 128GSSD+1T 4G独显 IPS)"}],"sellerName":"商家3","sellerid":"3"},{"list":[{"bargainPrice":3455,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/12224420750.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9106/106/1785172479/537280/253bc0ab/59bf78a7N057e5ff7.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t9106/106/1785172479/537280/253bc0ab/59bf78a7N057e5ff7.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8461/5/1492479653/68388/7255e013/59ba5e84N91091843.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8461/5/1492479653/68388/7255e013/59ba5e84N91091843.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8803/356/1478945529/489755/2a163ace/59ba5e84N7bb9a666.jpg!q70.jpg","num":3,"pid":48,"price":222,"pscid":39,"selected":0,"sellerid":4,"subhead":"【现货新品抢购】全面屏2.0震撼来袭,骁龙835处理器,四曲面陶瓷机","title":"小米(MI) 小米MIX2 手机 黑色 全网通 (6GB+64GB)【标配版】"},{"bargainPrice":399,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/1439822107.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t5887/201/859509257/69994/6bde9bf6/59224c24Ne854e14c.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5641/233/853609022/57374/5c73d281/59224c24N3324d5f4.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5641/233/853609022/57374/5c73d281/59224c24N3324d5f4.jpg!q70.jpg","num":1,"pid":83,"price":444,"pscid":85,"selected":0,"sellerid":4,"subhead":"2件,总价打6.50","title":"Gap男装 休闲舒适简约水洗五袋直筒长裤紧身牛仔裤941825 深灰色 33/32(175/84A)"}],"sellerName":"商家4","sellerid":"4"},{"list":[{"bargainPrice":1999,"createtime":"2017-10-10T16:09:02","detailUrl":"https://item.m.jd.com/product/5025971.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t7210/232/3738666823/232298/9004583e/59c3a9a7N8de42e15.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8356/82/2107423621/109733/c019b8c6/59c3a9a6Ne9a4bdd7.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t10219/74/25356012/171379/7d55e296/59c3a9a8N82fa6e02.jpg!q70.jpg","num":2,"pid":49,"price":333,"pscid":39,"selected":0,"sellerid":5,"subhead":"vivo X20 带你开启全面屏时代!逆光也清晰,照亮你的美!","title":"vivo X20 全面屏手机 全网通 4GB+64GB 金色 移动联通电信4G手机 双卡双待"}],"sellerName":"商家5","sellerid":"5"},{"list":[{"bargainPrice":3455,"createtime":"2017-10-14T21:48:08","detailUrl":"https://item.m.jd.com/product/12224420750.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9106/106/1785172479/537280/253bc0ab/59bf78a7N057e5ff7.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t9106/106/1785172479/537280/253bc0ab/59bf78a7N057e5ff7.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8461/5/1492479653/68388/7255e013/59ba5e84N91091843.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8461/5/1492479653/68388/7255e013/59ba5e84N91091843.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8803/356/1478945529/489755/2a163ace/59ba5e84N7bb9a666.jpg!q70.jpg","num":2,"pid":50,"price":444,"pscid":39,"selected":0,"sellerid":6,"subhead":"【现货新品抢购】全面屏2.0震撼来袭,骁龙835处理器,四曲面陶瓷机","title":"小米(MI) 小米MIX2 手机 黑色 全网通 (6GB+64GB)【标配版】"}],"sellerName":"商家6","sellerid":"6"},{"list":[{"bargainPrice":3455,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/12224420750.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9106/106/1785172479/537280/253bc0ab/59bf78a7N057e5ff7.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t9106/106/1785172479/537280/253bc0ab/59bf78a7N057e5ff7.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8461/5/1492479653/68388/7255e013/59ba5e84N91091843.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8461/5/1492479653/68388/7255e013/59ba5e84N91091843.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8803/356/1478945529/489755/2a163ace/59ba5e84N7bb9a666.jpg!q70.jpg","num":1,"pid":51,"price":555,"pscid":39,"selected":0,"sellerid":7,"subhead":"【现货新品抢购】全面屏2.0震撼来袭,骁龙835处理器,四曲面陶瓷机","title":"小米(MI) 小米MIX2 手机 黑色 全网通 (6GB+64GB)【标配版】"}],"sellerName":"商家7","sellerid":"7"},{"list":[{"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":1,"pid":1,"price":118,"pscid":1,"selected":0,"sellerid":17,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家17","sellerid":"17"}]
//     * msg : 请求成功
//     */
//    private String code;
//    private String msg;
//    private List 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 List getData() {
//        return data;
//    }
//    public void setData(List data) {
//        this.data = data;
//    }
//    public static class DataBean {
//        /**
//         * list : [{"bargainPrice":99,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/4345173.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6037/35/2944615848/95178/6cd6cff0/594a3a10Na4ec7f39.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6607/258/1025744923/75738/da120a2d/594a3a12Ne3e6bc56.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6370/292/1057025420/64655/f87644e3/594a3a12N5b900606.jpg!q70.jpg","num":1,"pid":45,"price":2999,"pscid":39,"selected":0,"sellerid":1,"subhead":"高清双摄,就是清晰!2000+1600万高清摄像头,6GB大内存+高通骁龙835处理器,性能怪兽!","title":"一加手机5 (A5000) 6GB+64GB 月岩灰 全网通 双卡双待 移动联通电信4G手机"},{"bargainPrice":159,"createtime":"2017-10-14T21:49:15","detailUrl":"https://item.m.jd.com/product/5061723.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8716/197/1271594444/173291/2f40bb4f/59b743bcN8509428e.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8347/264/1286771527/92188/5cf5ec04/59b7420fN65378e9e.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7363/165/3000956253/190883/179a372/59b743bfNd0c79d93.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7399/112/2935531768/183594/b77c7d4a/59b7441aNc3d40133.jpg!q70.jpg","num":1,"pid":90,"price":1233,"pscid":112,"selected":0,"sellerid":1,"subhead":"针织针织闪闪闪亮你的眼","title":"维迩旎 2017秋冬新款长袖针织连衣裙韩版气质中长款名媛包臀A字裙 zx179709 黑色 XL"}]
//         * sellerName : 商家1
//         * sellerid : 1
//         */
//        //注意...需要自己添加一个booelan类型的字段,,,表示商家(一级列表是否选中)
//        private boolean group_check;
//        public boolean isGroup_check() {
//            return group_check;
//        }
//        public void setGroup_check(boolean group_check) {
//            this.group_check = group_check;
//        }
//        private String sellerName;
//        private String sellerid;
//        private List list;
//        public String getSellerName() {
//            return sellerName;
//        }
//        public void setSellerName(String sellerName) {
//            this.sellerName = sellerName;
//        }
//        public String getSellerid() {
//            return sellerid;
//        }
//        public void setSellerid(String sellerid) {
//            this.sellerid = sellerid;
//        }
//        public List getList() {
//            return list;
//        }
//        public void setList(List list) {
//            this.list = list;
//        }
//        public static class ListBean {
//            /**
//             * bargainPrice : 99.0
//             * createtime : 2017-10-14T21:38:26
//             * detailUrl : https://item.m.jd.com/product/4345173.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends
//             * images : https://m.360buyimg.com/n0/jfs/t6037/35/2944615848/95178/6cd6cff0/594a3a10Na4ec7f39.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6607/258/1025744923/75738/da120a2d/594a3a12Ne3e6bc56.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6370/292/1057025420/64655/f87644e3/594a3a12N5b900606.jpg!q70.jpg
//             * num : 1
//             * pid : 45
//             * price : 2999.0
//             * pscid : 39
//             * selected : 0
//             * sellerid : 1
//             * subhead : 高清双摄,就是清晰!2000+1600万高清摄像头,6GB大内存+高通骁龙835处理器,性能怪兽!
//             * title : 一加手机5 (A5000) 6GB+64GB 月岩灰 全网通 双卡双待 移动联通电信4G手机
//             */
//            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;//当前的商品子条目是否选中....1表示选中,0未选中
//            private int sellerid;
//            private String subhead;
//            private String title;
//            public double getBargainPrice() {
//                return bargainPrice;
//            }
//            public void setBargainPrice(double bargainPrice) {
//                this.bargainPrice = bargainPrice;
//            }
//            public String getCreatetime() {
//                return createtime;
//            }
//            public void setCreatetime(String createtime) {
//                this.createtime = createtime;
//            }
//            public String getDetailUrl() {
//                return detailUrl;
//            }
//            public void setDetailUrl(String detailUrl) {
//                this.detailUrl = detailUrl;
//            }
//            public String getImages() {
//                return images;
//            }
//            public void setImages(String images) {
//                this.images = images;
//            }
//            public int getNum() {
//                return num;
//            }
//            public void setNum(int num) {
//                this.num = num;
//            }
//            public int getPid() {
//                return pid;
//            }
//            public void setPid(int pid) {
//                this.pid = pid;
//            }
//            public 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;
//            }
//        }
//    }
//}


//二购物车的适配器
public class MyAdapter5 extends BaseExpandableListAdapter {
    private MainPresenter mainPresenter;
    private Handler handler;
    private CartBean cartBean;
    private Context context;
    private int childIndex;
    private int allIndex;
    private int uid;
    public MyAdapter5(Context context, CartBean cartBean, Handler handler, MainPresenter mainPresenter,int uid) {
        this.context = context;
        this.cartBean = cartBean;
        this.handler = handler;
        this.mainPresenter = mainPresenter;
        this.uid = uid;
    }
    @Override
    public int getGroupCount() {
        return cartBean.getData().size();
    }
    @Override
    public int getChildrenCount(int groupPosition) {
        return cartBean.getData().get(groupPosition).getList().size();
    }
    @Override
    public Object getGroup(int groupPosition) {
        return cartBean.getData().get(groupPosition);
    }
    @Override
    public Object getChild(int groupPosition, int childPosition) {

        return cartBean.getData().get(groupPosition).getList().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(int groupPosition, boolean b, View view, ViewGroup viewGroup) {
        final GroupHolder holder;
        if (view == null){
            view = View.inflate(context, R.layout.group_layout,null);
            holder = new GroupHolder();
            holder.checkBox = view.findViewById(R.id.group_check);
            holder.textView = view.findViewById(R.id.group_text);
            view.setTag(holder);
        }else {
            holder = (GroupHolder) view.getTag();
        }
        final CartBean.DataBean dataBean = cartBean.getData().get(groupPosition);
        //赋值
        holder.textView.setText(dataBean.getSellerName());
        holder.checkBox.setChecked(dataBean.isGroup_check());
        //一级列表的点击事件
        holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击一级列表的时候,子条目需要一个个的去执行更新,,,,全都执行完成之后再去请求查询购物车
                //使用递归的形式...例如一组里面3个孩子,,,0,1,2
                childIndex = 0;
                //更新
                updateChildCheckedInGroup(holder.checkBox.isChecked(),dataBean);
            }
        });
        return view;
    }
    /**
     * 点击以及列表的时候,去改变所有子条目的状态
     * @param
     * @param checked
     * @param dataBean
     */
    private void updateChildCheckedInGroup(final boolean checked, final CartBean.DataBean dataBean) {
        CartBean.DataBean.ListBean listBean = dataBean.getList().get(childIndex);
        //请求更新购物车的接口...更新成功之后,再次请求查询购物车的接口,进行数据的展示
        //?uid=71&sellerid=1&pid=1&selected=0&num=10
        Map params = new HashMap<>();
        params.put("uid", String.valueOf(uid));
        params.put("sellerid", String.valueOf(listBean.getSellerid()));
        params.put("pid", String.valueOf(listBean.getPid()));
        params.put("selected", String.valueOf(checked ? 1:0));
        params.put("num", String.valueOf(listBean.getNum()));
        OkHttp3Util_03.doPost(ApiUtil.updateCartUrl, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()){
                    childIndex ++;//0,1,2....size()
                    if (childIndex < dataBean.getList().size()){
                        //继续更新
                        updateChildCheckedInGroup(checked,dataBean);
                    }else {
                        //请求查询购物车的操作...重新展示数据
                        mainPresenter.getCartData(ApiUtil.selectCartUrl,uid);
                    }
                }
            }
        });
    }
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) {
        ChildHolder holder;
        if (view == null){
            view = View.inflate(context, R.layout.child_layout,null);
            holder = new ChildHolder();
            holder.checkBox = view.findViewById(R.id.child_check);
            holder.text_title = view.findViewById(R.id.child_title);
            holder.imageView = view.findViewById(R.id.child_image);
            holder.text_price = view.findViewById(R.id.child_price);
            holder.text_jian = view.findViewById(R.id.text_jian);
            holder.text_num = view.findViewById(R.id.text_num);
            holder.text_add = view.findViewById(R.id.text_add);
            view.setTag(holder);
        }else {
            holder = (ChildHolder) view.getTag();
        }
        final CartBean.DataBean.ListBean listBean = cartBean.getData().get(groupPosition).getList().get(childPosition);
        //赋值
        holder.text_title.setText(listBean.getTitle());
        holder.text_price.setText("¥"+listBean.getBargainPrice());
        String[] strings = listBean.getImages().split("\\|");
        holder.imageView.setImageURI(strings[0]);
        holder.checkBox.setChecked(listBean.getSelected() == 0? false:true);//根据0,1进行设置是否选中
        //setText()我们使用一定是设置字符串
        holder.text_num.setText(listBean.getNum()+"");
        //点击进行更新购物车的操作
        //checkBox设置点击事件,,,,更新是否选中
        holder.checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //请求更新购物车的接口...更新成功之后,再次请求查询购物车的接口,进行数据的展示
                //?uid=71&sellerid=1&pid=1&selected=0&num=10
                Map params = new HashMap<>();
                params.put("uid", String.valueOf(uid));
                params.put("sellerid", String.valueOf(listBean.getSellerid()));
                params.put("pid", String.valueOf(listBean.getPid()));
                params.put("selected", String.valueOf(listBean.getSelected() == 0?1:0));//listBean.getSelected()...0--->1,,,1--->0
                params.put("num", String.valueOf(listBean.getNum()));
                OkHttp3Util_03.doPost(ApiUtil.updateCartUrl, params, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                    }
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        //更新成功之后,再次请求查询购物车的接口,进行数据的展示
                        if (response.isSuccessful()){
                            mainPresenter.getCartData(ApiUtil.selectCartUrl,uid);
                        }
                    }
                });
            }
        });
        //        holder.text_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //请求更新购物车的接口...更新成功之后,再次请求查询购物车的接口,进行数据的展示
                //?uid=71&sellerid=1&pid=1&selected=0&num=10
                Map params = new HashMap<>();
                params.put("uid", String.valueOf(uid));
                params.put("sellerid", String.valueOf(listBean.getSellerid()));
                params.put("pid", String.valueOf(listBean.getPid()));
                params.put("selected", String.valueOf(listBean.getSelected()));//listBean.getSelected()...0--->1,,,1--->0
                params.put("num", String.valueOf(listBean.getNum() +1 ));
                OkHttp3Util_03.doPost(ApiUtil.updateCartUrl, params, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                    }
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        //更新成功之后,再次请求查询购物车的接口,进行数据的展示
                        if (response.isSuccessful()){
                            mainPresenter.getCartData(ApiUtil.selectCartUrl,uid);
                        }
                    }
                });
            }
        });
        //        holder.text_jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                int num = listBean.getNum();
                if (num == 1){
                    return;
                }
                //请求更新购物车的接口...更新成功之后,再次请求查询购物车的接口,进行数据的展示
                //?uid=71&sellerid=1&pid=1&selected=0&num=10
                Map params = new HashMap<>();
                params.put("uid", String.valueOf(uid));
                params.put("sellerid", String.valueOf(listBean.getSellerid()));
                params.put("pid", String.valueOf(listBean.getPid()));
                params.put("selected", String.valueOf(listBean.getSelected()));//listBean.getSelected()...0--->1,,,1--->0
                params.put("num", String.valueOf(num - 1));
                OkHttp3Util_03.doPost(ApiUtil.updateCartUrl, params, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                    }
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        //更新成功之后,再次请求查询购物车的接口,进行数据的展示
                        if (response.isSuccessful()){
                            mainPresenter.getCartData(ApiUtil.selectCartUrl,uid);
                        }
                    }
                });
            }
        });
        view.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Map params = new HashMap<>();
                params.put("uid",String.valueOf(uid));
                params.put("pid", String.valueOf(listBean.getPid()));
                OkHttp3Util_03.doPost(ApiUtil.deleteCartUrl, params, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                    }
                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        //更新成功之后,再次请求查询购物车的接口,进行数据的展示
                        if (response.isSuccessful()){
                            mainPresenter.getCartData(ApiUtil.selectCartUrl,uid);
                        }
                    }
                });
                return false;
            }
        });
        view.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                final AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("提示");
                builder.setMessage("确定要删除吗");
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
//                        https://www.zhaoapi.cn/product/deleteCart?uid=72&pid=1
                        String DelPid = String.valueOf(listBean.getPid());
                        Map params = new HashMap<>();
                        params.put("uid",String.valueOf(uid));
                        params.put("pid",DelPid);

                        OkHttp3Util_03.doPost(ApiUtil.deleteCartUrl, params, new Callback() {
                            @Override
                            public void onFailure(Call call, IOException e) {
                            }
                            @Override
                            public void onResponse(Call call, Response response) throws IOException {
                                mainPresenter.getCartData(ApiUtil.selectCartUrl,uid);
                                Log.d("test","-----------------删除");
                            }
                        });
                    }
                });
                AlertDialog dialog = builder.create();
                dialog.show();
                return false;
            }
        });
        return view;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
    /**
     * 计算数量和价格,,,并发送到activity显示
     */
    public void sendPriceAndCount() {
        double price = 0;
        int count = 0;
        for (int i=0;i<cartBean.getData().size();i++){
            List listBeans = cartBean.getData().get(i).getList();
            for (int j = 0; j< listBeans.size(); j++){
                CartBean.DataBean.ListBean listBean = listBeans.get(j);
                //选中的时候计算价格和数量
                if (listBean.getSelected() == 1){
                    price += listBean.getBargainPrice() * listBean.getNum();
                    count += listBean.getNum();
                }
            }
        }

        String priceString = new DecimalFormat("0.00").format(price);//java.text时间
        //封装一下
        CountPriceBean countPriceBean = new CountPriceBean(priceString, count);
        //发送给activity/fragment进行显示
        Message msg = Message.obtain();
        msg.what = 0;
        msg.obj = countPriceBean;
        handler.sendMessage(msg);
    }
    /**
     * 更新所有的子孩子的状态...跟随全选状态改变
     * @param checked
     */
    public void setAllChildChecked(boolean checked) {
        //通过遍历,把所有的孩子装到一个大的集合中
        List allList = new ArrayList<>();
        for (int i=0;i<cartBean.getData().size();i++){
            for (int j = 0;j<cartBean.getData().get(i).getList().size();j++){
                allList.add(cartBean.getData().get(i).getList().get(j));
            }
        }

        //更新每一个子孩子的状态...递归
        allIndex = 0;
        updateAllChild(checked,allList);
    }
    /**
     * 更新所有的孩子
     * @param checked
     * @param allList
     */
    private void updateAllChild(final boolean checked, final List allList) {
        CartBean.DataBean.ListBean listBean = allList.get(allIndex);

        //请求更新购物车的接口...更新成功之后,再次请求查询购物车的接口,进行数据的展示
        //?uid=71&sellerid=1&pid=1&selected=0&num=10
        Map params = new HashMap<>();
        params.put("uid",String.valueOf(uid));
        params.put("sellerid", String.valueOf(listBean.getSellerid()));
        params.put("pid", String.valueOf(listBean.getPid()));
        params.put("selected", String.valueOf(checked ? 1:0));
        params.put("num", String.valueOf(listBean.getNum()));

        OkHttp3Util_03.doPost(ApiUtil.updateCartUrl, params, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()){
                    allIndex ++;
                    //判断
                    if (allIndex < allList.size()){
                        //继续更新
                        updateAllChild(checked,allList);
                    }else {
                        //查询购物车
                        mainPresenter.getCartData(ApiUtil.selectCartUrl,uid);
                    }
                }
            }
        });
    }
    private class GroupHolder{
        CheckBox checkBox;
        TextView textView;
    }
    private class ChildHolder{
        CheckBox checkBox;
        SimpleDraweeView imageView;
        TextView text_title;
        TextView text_price;
        TextView text_num;
        TextView text_jian;
        TextView text_add;
    }
}

//group_layout
//    
////        android:orientation="horizontal"
//        android:padding="10dp"
//        android:layout_width="match_parent"
//        android:layout_height="match_parent">
////        android:button="@null"
//                android:background="@drawable/check_box_selector"
//                android:id="@+id/group_check"
//                android:layout_gravity="center_vertical"
//                android:layout_width="wrap_content"
//                android:layout_height="wrap_content" />
////        android:id="@+id/group_text"
//                android:layout_marginLeft="10dp"
//                android:layout_gravity="center_vertical"
//                android:layout_width="wrap_content"
//                android:layout_height="wrap_content" />
//

//child_layout
//    
////        android:padding="10dp"
//        android:layout_width="match_parent"
//        android:layout_height="wrap_content">
////        android:button="@null"
//                android:background="@drawable/check_box_selector"
//                android:layout_centerVertical="true"
//                android:id="@+id/child_check"
//                android:layout_width="wrap_content"
//                android:layout_height="wrap_content" />
////        android:layout_marginLeft="10dp"
//        android:layout_toRightOf="@+id/child_check"
//        android:id="@+id/child_image"
//        android:layout_width="100dp"
//        android:layout_height="100dp" />
////        android:maxLines="2"
//                android:minLines="2"
//                android:id="@+id/child_title"
//                android:layout_marginLeft="5dp"
//                android:layout_toRightOf="@+id/child_image"
//                android:layout_width="wrap_content"
//                android:layout_height="wrap_content" />
////        android:id="@+id/child_price"
//                android:textColor="#ff0000"
//                android:text="¥0.00"
//                android:layout_toRightOf="@+id/child_image"
//                android:layout_alignBottom="@+id/child_image"
//                android:layout_marginLeft="5dp"
//                android:layout_width="wrap_content"
//                android:layout_height="wrap_content" />
////        android:layout_alignParentRight="true"
//                android:layout_alignBottom="@+id/child_image"
//                android:orientation="horizontal"
//                android:layout_width="wrap_content"
//                android:layout_height="wrap_content">
////            android:padding="5dp"
//                    android:background="@drawable/bian_kuang"
//                    android:text=""
//                    android:id="@+id/text_jian"
//                    android:layout_width="wrap_content"
//                    android:layout_height="wrap_content" />
////            android:paddingLeft="10dp"
//                    android:paddingRight="10dp"
//                    android:paddingBottom="5dp"
//                    android:paddingTop="5dp"
//                    android:background="@drawable/bian_kuang"
//                    android:text="1"
//                    android:id="@+id/text_num"
//                    android:layout_width="wrap_content"
//                    android:layout_height="wrap_content" />
////            android:padding="5dp"
//                    android:background="@drawable/bian_kuang"
//                    android:text=""
//                    android:id="@+id/text_add"
//                    android:layout_width="wrap_content"
//                    android:layout_height="wrap_content" />
//
//

//@drawable/bian_kuang
//    
//
//
//
//

//public class ApiUtil {
//    //查询购物车的路径 https://www.zhaoapi.cn/product/getCarts?uid=71&source=android
//    public static String selectCartUrl = "https://www.zhaoapi.cn/product/getCarts";
//    //删除购物车
//    public static String deleteCartUrl = "https://www.zhaoapi.cn/product/deleteCart";
//    //更新购物车....?uid=71&sellerid=1&pid=1&selected=0&num=10
//    public static String updateCartUrl = "https://www.zhaoapi.cn/product/updateCarts";
//    //商品详情.....
//    public static  String selectDing = "https://www.zhaoapi.cn/product/getOrders";
//}

//public class CountPriceBean {
//    private String priceString;
//    private int count;
//    public CountPriceBean(String priceString, int count) {
//        this.priceString = priceString;
//        this.count = count;
//    }
//    public String getPriceString() {
//        return priceString;
//    }
//    public void setPriceString(String priceString) {
//        this.priceString = priceString;
//    }
//    public int getCount() {
//        return count;
//    }
//    public void setCount(int count) {
//        this.count = count;
//    }
//}

/**
 * 网络权限适配
 */
//public class MyApp extends Application {
//    @Override
//    public void onCreate() {
//        super.onCreate();
//        Fresco.initialize(this);
//    }
//}
//

//
    implementation 'com.android.support:recyclerview-v7:27.1.1'
            implementation 'com.squareup.okhttp3:okhttp:3.10.0'
            implementation 'com.google.code.gson:gson:2.2.4'
            compile 'com.facebook.fresco:fresco:0.14.1'



//三购物车ppublic class MainPresenter implements IMainPresenter {
    private MainModel mainModel;
    private IMainActivity iMainActivity;
    public MainPresenter(IMainActivity iMainActivity) {
        this.iMainActivity = iMainActivity;
        //创建model
        mainModel = new MainModel(this);
    }
    //当前中间者不去直接获取网络数据,,,需要让model获取,,,创建presenter的时候就去创建出model,,,构造方法中
    public void getCartData(String selectCartUrl,int uid) {
        //需要让model获取,
        mainModel.getCartData(selectCartUrl,uid);
    }
    @Override
    public void onSuccess(CartBean cartBean) {
        //回调给view...activity
        iMainActivity.onCartSuccess(cartBean);
    }
}

//public interface IMainPresenter {
//    void onSuccess(CartBean cartBean);
//}

//public class MainModel {
//    private IMainPresenter iMainPresenter;
//    public MainModel(IMainPresenter iMainPresenter) {
//        this.iMainPresenter = iMainPresenter;
//    }
//    //在这里真正获取购物车的数据
//    public void getCartData(String selectCartUrl,int uid) {
//        Map params = new HashMap<>();
//        params.put("uid", String.valueOf(uid));
//        params.put("source","android");
//        OkHttp3Util_03.doPost(selectCartUrl, params, new Callback() {
//            @Override
//            public void onFailure(Call call, IOException e) {
//            }
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                if (response.isSuccessful()){
//                    String json = response.body().string();
//                    if ("null".equals(json)){
//                        iMainPresenter.onSuccess(null);
//                    }else {
//                        //解析
//                        CartBean cartBean = new Gson().fromJson(json,CartBean.class);
//                        //接口回调...presenter//                        iMainPresenter.onSuccess(cartBean);
//                    }
//                }
//            }
//        });
//    }
//}


你可能感兴趣的:(shopping购物车)