购物车 Rrtrofit&RXjava

依赖


compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
compile 'io.reactivex.rxjava2:rxjava:2.1.7'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
compile 'com.github.bumptech.glide:glide:4.3.1'

权限

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



activity_main


<?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="match_parent"
    android:orientation="vertical">

    <!--头布局-->
    <LinearLayout
        android:id="@+id/top_bar"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="#f7f7f7"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:background="@android:color/transparent"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/back"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_alignParentLeft="true"
                android:layout_gravity="center_vertical"
                android:padding="12dp"
                android:src="@drawable/back" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:minHeight="48dp"
                android:text="购物车"
                android:textColor="#1a1a1a"
                android:textSize="16sp" />

            <TextView
                android:id="@+id/edit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginRight="40dp"
                android:gravity="center"
                android:minHeight="48dp"
                android:text="编辑"
                android:textColor="#1a1a1a"
                android:textSize="14sp" />
        </RelativeLayout>
    </LinearLayout>

    <ExpandableListView
        android:id="@+id/exListView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:childIndicator="@null"
        android:groupIndicator="@null" >
    </ExpandableListView>

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

        <CheckBox
            android:id="@+id/all_chekbox"
            android:layout_marginLeft="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="全选"/>
        <LinearLayout
            android:id="@+id/ll_info"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_marginRight="20dp"
                android:layout_weight="1"
                >
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:gravity="right"
                    >
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:text="合计:"
                        android:textSize="18sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/total_price"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="0.00"
                        android:textColor="#f23232"
                        android:textSize="16sp"
                        android:textStyle="bold" />
                </LinearLayout>
                <TextView
                    android:id="@+id/total_number"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="共有商品:0"
                    android:gravity="right"
                    android:textSize="16sp"
                    android:textStyle="bold" />
            </LinearLayout>
            <TextView
                android:id="@+id/tv_go_to_pay"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="3"
                android:background="#fd7a05"
                android:clickable="true"
                android:gravity="center"
                android:text="结算"
                android:textColor="#FAFAFA"

                />
        </LinearLayout>

    </LinearLayout>
</LinearLayout>


layout_add_delete


<?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="match_parent"
    android:orientation="horizontal"
    android:weightSum="1">

    <TextView
        android:id="@+id/txt_delete"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:text="-"
        android:gravity="center"
        android:background="#8b948b"/>

    <EditText
        android:id="@+id/et_number"
        android:layout_marginTop="2dp"
        android:layout_width="50dp"
        android:layout_height="30dp"
        android:background="@drawable/edit"
        android:layout_weight="0.00"
        android:gravity="center"
        android:text="1"/>
    <TextView
        android:id="@+id/txt_add"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:text="+"
        android:gravity="center"
        android:background="#8b948b"/>


</LinearLayout>


ex_group_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="match_parent"
    android:background="#cfc3c3"
    android:orientation="horizontal">

    <CheckBox
        android:id="@+id/group_checkbox"
        android:layout_marginLeft="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"/>

    <TextView
        android:id="@+id/shop_name"
        android:layout_marginLeft="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16dp" />

</LinearLayout>


ex_child_item

<?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"
    android:layout_width="match_parent"
    android:layout_height="230dp"
    android:orientation="horizontal">

    <CheckBox
        android:id="@+id/child_checkbox"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="20dp"
        android:layout_marginBottom="50dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <RelativeLayout
        android:layout_marginLeft="20dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/shop_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginLeft="17dp"
            android:layout_marginStart="17dp"
            android:text="TextView"
            android:layout_alignParentTop="true" />

        <ImageView
            android:id="@+id/shop_img"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_marginTop="30dp"
            app:srcCompat="@mipmap/ic_launcher"
            android:layout_below="@+id/shop_name"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <TextView
            android:id="@+id/shop_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/shop_img"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:text="20"
            android:textColor="#f23232"/>

        <com.bwei.heqi20171216shopping.view.AddDeleteView
            android:id="@+id/adv"
            android:layout_width="160dp"
            android:layout_height="30dp"
            android:layout_below="@+id/shop_price"
            android:layout_marginTop="30dp"
            android:layout_marginLeft="140dp"
            app:left_text="-"
            app:right_text="+"
            app:middle_text="1"
            android:focusable="false"
            >

        </com.bwei.heqi20171216shopping.view.AddDeleteView>

        <Button
            android:id="@+id/shop_delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"

            android:text="删除" />
    </RelativeLayout>


</LinearLayout>


model包

IModel接口
import java.util.HashMap;

/**
 * Created by HQ on 2017/12/16.
 */

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

}

NewsModel

import android.util.Log;

import com.bwei.heqi20171216shopping.bean.GoodsBean;
import com.bwei.heqi20171216shopping.http.RetrofitUtils;
import com.bwei.heqi20171216shopping.presenter.PresenterTwo;

import java.util.HashMap;

import io.reactivex.Flowable;

/**
 * Created by HQ on 2017/12/7.
 */
//网络请求

public class NewsModel implements IModel {

    private PresenterTwo presenter;
    public NewsModel(PresenterTwo presenter){
        this.presenter=presenter;
    }

    @Override
    public void getData(HashMap<String,String> map) {

        Flowable<GoodsBean> flowable = RetrofitUtils.getInstance().getApiService().getHeqi(map);
        Log.i("fff",flowable+"");
        presenter.getNews(flowable);


    }
}

view包
MainActivity

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;

import com.bwei.heqi20171216shopping.R;
import com.bwei.heqi20171216shopping.adapter.Goods;
import com.bwei.heqi20171216shopping.bean.GoodsChilde;
import com.bwei.heqi20171216shopping.bean.GoodsGroup;
import com.bwei.heqi20171216shopping.presenter.PresenterTwo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends AppCompatActivity implements GoodsViews {

    private ExpandableListView exListView;
    public CheckBox all_checKbox;
    private TextView total_price;
    private TextView total_number;
    private TextView pay;
    //新建两个集合
    List<GoodsGroup> list=new ArrayList<GoodsGroup>();
    List<List<GoodsChilde>> list2=new ArrayList<List<GoodsChilde>>();
    private PresenterTwo two;
    private Goods goods;
    private TextView edit;

    private boolean flagg=true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取控件,获取完直接去p层解析数据
        exListView = (ExpandableListView) findViewById(R.id.exListView);
        all_checKbox = (CheckBox) findViewById(R.id.all_chekbox);
        total_price = (TextView) findViewById(R.id.total_price);
        total_number = (TextView) findViewById(R.id.total_number);
        pay = (TextView) findViewById(R.id.tv_go_to_pay);
        edit = (TextView) findViewById(R.id.edit);

                    //实例化p                    two = new PresenterTwo(this,list,list2);
                    HashMap<String, String> map = new HashMap<>();
                    map.put("source","android");
                    map.put("uid","110");
                    two.getData(map);
                    two.attachView(this);


                    //全选按钮的点击事件
                    all_checKbox.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            //获取到全选按钮的状态
                            boolean checked = all_checKbox.isChecked();
                            for (GoodsGroup i : list) {
                    //把状态赋给父级
                    i.setCheckbox(checked);
                }
                //子级的状态
                for (List<GoodsChilde> i1 : list2) {
                    for (int j = 0; j < i1.size(); j++) {
                        i1.get(j).setCheckchild(checked);
                    }
                }
                //刷新适配器
                goods.notifyDataSetChanged();
                //计算的方法
                changesum(list2);
            }

        });
        //删除条目的逻辑
        edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //遍历子级的集合
                for (List<GoodsChilde> i1:list2){
                    for (int i=0;i<i1.size();i++){
                        i1.get(i).setBtn(flagg);
                    }
                }
                flagg=!flagg;
                goods.notifyDataSetChanged();
            }
        });

    }
    //计算的逻辑
    public void changesum(List<List<GoodsChilde>> childBeen) {
        double sum = 0;
        int count = 0;
        for (List<GoodsChilde> i1 : childBeen) {
            for (int r = 0; r < i1.size(); r++) {
                boolean childCb1 = i1.get(r).isCheckchild();
                if (childCb1) {
                    double price = i1.get(r).getPrice();
                    int num = i1.get(r).getNum();
                    sum += num * price;
                    count+=num;
                }
            }
        }
        total_price.setText("¥:" + sum);
        total_number.setText("共计有"+count+"件商品");

    }




    @Override
    public void onSuccess(List<GoodsGroup> list, List<List<GoodsChilde>> list2) {
        //创建适配器
        goods = new Goods(this,list,list2);
        Log.i("lll",list2+"");
        //设置适配器
        exListView.setAdapter(goods);
        //二级列表默认展开
        for (int i = 0; i< goods.getGroupCount(); i++){
            exListView.expandGroup(i);
        }
        //刷新适配器
        goods.notifyDataSetChanged();

    }

    @Override
    public void onFiled(Exception e) {

    }
}


 AddDeleteView

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bwei.heqi20171216shopping.R;


/**
 * 作者
 * on 2017/11/18 10:49
 */

public class AddDeleteView extends LinearLayout {
    private OnAddDelClickListener listener;
    private EditText etNumber;

    //对外提供一个点击的回调接口
    public interface OnAddDelClickListener{
        void onAddClick(View v);
        void onDelClick(View v);
    }

    public void setOnAddDelClickListener(OnAddDelClickListener listener){
        if(listener!=null){
            this.listener=listener;
        }
    }

    public AddDeleteView(Context context) {
        this(context,null);
    }

    public AddDeleteView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public AddDeleteView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs, defStyleAttr);
    }

    private void initView(Context context, AttributeSet attrs, int defStyleAttr) {
        View.inflate(context, R.layout.layout_add_delete,this);

        //获取控件
        TextView txtDelete=findViewById(R.id.txt_delete);
        TextView txtAdd=findViewById(R.id.txt_add);
        etNumber = findViewById(R.id.et_number);


        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AddDeleteViewStyle);

        String leftText = typedArray.getString(R.styleable.AddDeleteViewStyle_left_text);
        String rightText = typedArray.getString(R.styleable.AddDeleteViewStyle_right_text);
        String middleText = typedArray.getString(R.styleable.AddDeleteViewStyle_middle_text);
        int color = typedArray.getColor(R.styleable.AddDeleteViewStyle_left_text_color, Color.BLACK);

        txtDelete.setText(leftText);
        txtAdd.setText(rightText);
        etNumber.setText(middleText);
        txtDelete.setTextColor(color);

        //回收
        typedArray.recycle();


        txtDelete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onDelClick(view);
            }
        });

        txtAdd.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.onAddClick(view);
            }
        });

    }
    //对外提供一个修改数字的方法
    public void setNumber(int number){
        if(number>0){
            etNumber.setText(number+"");
        }
    }
    //对外提供一个获取当前数字的方法
    public int getNumber(){
        String string = etNumber.getText().toString();
        int i = Integer.parseInt(string);
        return i;
    }



}

GoodsView接口
import com.bwei.heqi20171216shopping.bean.GoodsChilde;
import com.bwei.heqi20171216shopping.bean.GoodsGroup;

import java.util.List;

/**
 * Created by HQ on 2017/11/21.
 */

public interface GoodsViews {
    void onSuccess(List<GoodsGroup> list, List<List<GoodsChilde>> list2);
    void onFiled( Exception e);
}


presenter包

BasePresenter接口

import java.util.HashMap;

/**
 * Created by HQ on 2017/12/16.
 */

public interface BasePresenter {
    void getData(HashMap<String,String> map);
}


PresenterTwo

import android.util.Log;

import com.bwei.heqi20171216shopping.bean.GoodsBean;
import com.bwei.heqi20171216shopping.bean.GoodsChilde;
import com.bwei.heqi20171216shopping.bean.GoodsGroup;
import com.bwei.heqi20171216shopping.model.NewsModel;
import com.bwei.heqi20171216shopping.view.GoodsViews;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;

/**
 * Created by WuXirui
 * Create Time: 2017/12/6
 * Description:
 */

public class PresenterTwo implements BasePresenter {
    private GoodsViews iv;
    private DisposableSubscriber<GoodsBean> subscriber;
    private List<GoodsGroup> list;//给父级的集合不new出来、
    private List<List<GoodsChilde>> list2;//给子级的集合

    public PresenterTwo(GoodsViews iv, List<GoodsGroup> list, List<List<GoodsChilde>> list2) {
        this.iv = iv;
        this.list = list;
        this.list2 = list2;
    }

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

    public void detachView() {
        if (iv != null) {
            iv = null;
        }
        if (subscriber != null) {
            if (!subscriber.isDisposed()) {
                subscriber.dispose();
            }
        }
    }

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

    public void getNews(Flowable<GoodsBean> flowable) {
        subscriber = flowable.subscribeOn(Schedulers.io())

                .observeOn(AndroidSchedulers.mainThread())

                .subscribeWith(new DisposableSubscriber<GoodsBean>() {
                    @Override
                    public void onNext(GoodsBean listMessageBean) {
                        Log.i("aaa", listMessageBean + "");
                        if(listMessageBean!=null){
                            List<GoodsBean.DataBean> data = listMessageBean.getData();
                            Log.e("ddd",data+"");
                            if(data!=null){
                                for (int i=0;i<data.size();i++){
                                    String sellerName = data.get(i).getSellerName();
                                    list.add(new GoodsGroup(false,sellerName));
                                    //解析子集集合
                                    List<GoodsBean.DataBean.ListBean> lists = data.get(i).getList();
                                    //因为集合里面套集合,要放一个集合,所以新new一个集合,是子集
                                    List<GoodsChilde> list1=new ArrayList<GoodsChilde>();
                                    for (int j=0;j<lists.size();j++){
                                        String title = lists.get(j).getTitle();
                                        String images = lists.get(j).getImages();
                                        double price = lists.get(j).getBargainPrice();
                                        list1.add(new GoodsChilde(title,false,images,price,1,false));
                                    }
                                    list2.add(list1);
                                    Log.e("hhh", list2.toString());
                                }
                                //通过新建的view接口,传给mainActivity
                                iv.onSuccess(list,list2);
                            }

                        }else{
                            Log.e("iii", "===============没有东西!!!===============");
                        }

                    }

                    @Override
                    public void onError(Throwable t) {
                        iv.onFiled(new Exception(t));
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


}

bean包

GoodsBean

public class GoodsBean {


    /**
     * msg : 请求成功
     * code : 0
     * data : [{"list":[{"bargainPrice":11800,"createtime":"2017-10-10T17:33:37","detailUrl":"https://item.m.jd.com/product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg","num":19,"pid":57,"price":5199,"pscid":40,"selected":0,"sellerid":1,"subhead":"i5 MX150 2G显存】全高清窄边框 8G内存 256固态硬盘 支持指纹识别 预装WIN10系统","title":"小米(MI)Air 13.3英寸全金属轻薄笔记本(i5-7200U 8G 256G PCle SSD MX150 2G独显 FHD 指纹识别 Win10)银\r\n"}],"sellerName":"商家1","sellerid":"1"},{"list":[{"bargainPrice":111.99,"createtime":"2017-10-14T21:48:08","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":1,"pid":9,"price":78.99,"pscid":1,"selected":0,"sellerid":2,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家2","sellerid":"2"},{"list":[{"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)"},{"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":1,"pid":47,"price":111,"pscid":39,"selected":0,"sellerid":3,"subhead":"碳黑色 32GB 全网通 官方标配   1","title":"锤子 坚果Pro 特别版 巧克力色 酒红色 全网通 移动联通电信4G手机 双卡双待 碳黑色 32GB 全网通"}],"sellerName":"商家3","sellerid":"3"},{"list":[{"bargainPrice":11800,"createtime":"2017-10-14T21:48:08","detailUrl":"https://mitem.jd.hk/ware/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1","images":"https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5815/178/2614671118/51656/7f52d137/593276c7N107b725a.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5878/60/2557817477/30873/4502b606/593276caN5a7d6357.jpg!q70.jpg","num":1,"pid":60,"price":13888,"pscid":40,"selected":0,"sellerid":4,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP213英寸Bar i5/8G/256G"}],"sellerName":"商家4","sellerid":"4"},{"list":[{"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":100,"price":2200,"pscid":112,"selected":0,"sellerid":11,"subhead":"针织针织闪闪闪亮你的眼","title":"维迩旎 2017秋冬新款长袖针织连衣裙韩版气质中长款名媛包臀A字裙 zx179709 黑色 XL"}],"sellerName":"商家11","sellerid":"11"},{"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-14T21:48:08","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":1,"pid":4,"price":999,"pscid":1,"selected":0,"sellerid":20,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家20","sellerid":"20"},{"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":7,"price":120.01,"pscid":1,"selected":0,"sellerid":23,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家23","sellerid":"23"}]
     */

    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":11800,"createtime":"2017-10-10T17:33:37","detailUrl":"https://item.m.jd.com/product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg","num":19,"pid":57,"price":5199,"pscid":40,"selected":0,"sellerid":1,"subhead":"i5 MX150 2G显存】全高清窄边框 8G内存 256固态硬盘 支持指纹识别 预装WIN10系统","title":"小米(MI)Air 13.3英寸全金属轻薄笔记本(i5-7200U 8G 256G PCle SSD MX150 2G独显 FHD 指纹识别 Win10)银\r\n"}]
         * sellerName : 商家1
         * sellerid : 1
         */

        private String sellerName;
        private String sellerid;
        private List<ListBean> 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<ListBean> getList() {
            return list;
        }

        public void setList(List<ListBean> list) {
            this.list = list;
        }

        public static class ListBean {
            /**
             * bargainPrice : 11800.0
             * createtime : 2017-10-10T17:33:37
             * detailUrl : https://item.m.jd.com/product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends
             * images : https://m.360buyimg.com/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg
             * num : 19
             * pid : 57
             * price : 5199.0
             * pscid : 40
             * selected : 0
             * sellerid : 1
             * subhead : i5 MX150 2G显存】全高清窄边框 8G内存 256固态硬盘 支持指纹识别 预装WIN10系统
             * title : 小米(MI)Air 13.3英寸全金属轻薄笔记本(i5-7200U 8G 256G PCle SSD MX150 2G独显 FHD 指纹识别 Win10)银

             */

            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;

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


GoodsGrpoup

public class GoodsGroup {
    private boolean checkbox;
    private String grouptitle;

    public GoodsGroup(boolean checkbox, String grouptitle) {
        this.checkbox = checkbox;
        this.grouptitle = grouptitle;
    }

    public boolean isCheckbox() {
        return checkbox;
    }

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

    public String getGrouptitle() {
        return grouptitle;
    }

    public void setGrouptitle(String grouptitle) {
        this.grouptitle = grouptitle;
    }

    @Override
    public String toString() {
        return "GoodsGroup{" +
                "checkbox=" + checkbox +
                ", grouptitle='" + grouptitle + '\'' +
                '}';
    }
}
 
GoodsChilde

public class GoodsChilde {

    private String childTitle;
    private boolean checkchild;
    private String img;
    private double price;
    private int num;
    private boolean btn;

    public GoodsChilde(String childTitle, boolean checkchild, String img, double price, int num, boolean btn) {
        this.childTitle = childTitle;
        this.checkchild = checkchild;
        this.img = img;
        this.price = price;
        this.num = num;
        this.btn = btn;
    }

    public String getChildTitle() {
        return childTitle;
    }

    public void setChildTitle(String childTitle) {
        this.childTitle = childTitle;
    }

    public boolean isCheckchild() {
        return checkchild;
    }

    public void setCheckchild(boolean checkchild) {
        this.checkchild = checkchild;
    }

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    public double getPrice() {
        return price;
    }

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

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public boolean isBtn() {
        return btn;
    }

    public void setBtn(boolean btn) {
        this.btn = btn;
    }

    @Override
    public String toString() {
        return "GoodsChilde{" +
                "childTitle='" + childTitle + '\'' +
                ", checkchild=" + checkchild +
                ", img='" + img + '\'' +
                ", price=" + price +
                ", num=" + num +
                ", btn=" + btn +
                '}';
    }
}



http包

ApiService接口

import com.bwei.heqi20171216shopping.bean.GoodsBean;

import java.util.Map;

import io.reactivex.Flowable;
import retrofit2.http.GET;
import retrofit2.http.QueryMap;

/**
 * Created by HQ on 2017/12/15.
 */

public interface ApiService {
    @GET("/product/getCarts")
    Flowable<GoodsBean> getHeqi(@QueryMap Map<String,String> map);

}


RetrofitUtils

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;


public class RetrofitUtils {
    private static volatile RetrofitUtils instance;

    private ApiService apiService;

    private RetrofitUtils() {
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .baseUrl("http://120.27.23.105")
                .build();
        apiService = retrofit.create(ApiService.class);
    }

    //单例模式
    public static RetrofitUtils getInstance() {
        if (instance == null) {
            synchronized (RetrofitUtils.class) {
                if (null == instance) {
                    instance = new RetrofitUtils();
                }
            }
        }
        return instance;
    }

    public ApiService getApiService() {
        return apiService;
    }
}


adapter包

Goods

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bwei.heqi20171216shopping.view.MainActivity;
import com.bwei.heqi20171216shopping.R;
import com.bwei.heqi20171216shopping.bean.GoodsChilde;
import com.bwei.heqi20171216shopping.bean.GoodsGroup;
import com.bwei.heqi20171216shopping.view.AddDeleteView;

import java.util.List;

/**
 * Created by HQ on 2017/11/21.
 */

public class Goods extends BaseExpandableListAdapter {
    private Context context;
    //传过来两个集合
    private List<GoodsGroup> list;
    private List<List<GoodsChilde>> list2;
    MainActivity con;
    //有参构造
    public Goods(Context context, List<GoodsGroup> list, List<List<GoodsChilde>> list2) {
        this.context = context;
        this.list = list;
        this.list2 = list2;
        con=(MainActivity)context;
    }

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

    @Override
    public int getChildrenCount(int i) {
        return list2.get(i).size();
    }

    @Override
    public Object getGroup(int i) {
        return list.size();
    }

    @Override
    public Object getChild(int i, int i1) {
        return list2.get(i).get(i1).getChildTitle();
    }

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

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

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

    //父级的展示
    @Override
    public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
        final GroupViewHolder holder;
        if(view==null){
            holder=new GroupViewHolder();
            //绘制父级视图
            view=View.inflate(context, R.layout.ex_group_item,null);
            holder.cb=(CheckBox)view.findViewById(R.id.group_checkbox);
            holder.name=(TextView)view.findViewById(R.id.shop_name);

            view.setTag(holder);

        }else{
            holder=(GroupViewHolder)view.getTag();
        }
        //设置父级的选中状态
        holder.cb.setChecked(list.get(i).isCheckbox());
        //得到名字
        holder.name.setText(list.get(i).getGrouptitle());
        //设置父级选择框的点击事件
        holder.cb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //父级当前框的选择状态,默认为未选中
                boolean checked = holder.cb.isChecked();
                //设置显示
                list.get(i).setCheckbox(checked);
                MainActivity con=(MainActivity)context;//获取到Main2Activity
                //遍历父级的集合
                for (GoodsGroup ii:list){
                    boolean checkbox = ii.isCheckbox();
                    if(!checkbox){//如果父级没有勾选
                        con.all_checKbox.setChecked(false);//设置全选按钮也不勾选
                        break;
                    }else{
                        con.all_checKbox.setChecked(true);
                    }
                }
                //勾选执行计算的方法
                con.changesum(list2);
                notifyDataSetChanged();
                //获取到我们子级的集合长度
                int size = list2.get(i).size();
                //如果我们的父级全部被选中
                if(checked){
                    //那么你就遍历子级的长度
                    for (int j=0;j<size;j++){
                        //将我们的子级全部都选中
                        list2.get(i).get(j).setCheckchild(true);
                    }
                }else{
                    for (int j=0;j<size;j++){
                        list2.get(i).get(j).setCheckchild(false);
                    }
                }
                //刷新我们的适配器
                con.changesum(list2);
                notifyDataSetChanged();
            }
        });


        return view;
    }
    //子级的展示
    @Override
    public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
        final ChildViewHolder holder;
        if(view==null){
            holder=new ChildViewHolder();
            //绘制子集视图
            view=View.inflate(context, R.layout.ex_child_item,null);
            holder.ch2=view.findViewById(R.id.child_checkbox);//子级的选择框
            holder.title=view.findViewById(R.id.shop_title);//子级的标题
            holder.img=view.findViewById(R.id.shop_img);//图片
            holder.et_number=view.findViewById(R.id.et_number);//加减中间的数量
            holder.price=view.findViewById(R.id.shop_price);//价格
            holder.txt_delete=view.findViewById(R.id.txt_delete);//加减器的减
            holder.txt_add=view.findViewById(R.id.txt_add);//加减器的加
            holder.txt_delete.setText("-");//设置显示减号
            holder.txt_add.setText("+");//设置显示加号
            holder.jiajia=view.findViewById(R.id.adv);//设置显示加减器
            holder.jiajia.setNumber(list2.get(i).get(i1).getNum());//自定义view修改数字的方法
            holder.btn=view.findViewById(R.id.shop_delete);//删除的按钮
            view.setTag(holder);
        }else{
            holder=(ChildViewHolder)view.getTag();
        }
        //执行删除的方法
        holder.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int size = list2.get(i).size();
                if(size==1){
                    list2.remove(i);
                    list.remove(i);
                }else{
                    list2.get(i).remove(i1);
                }
                //点击删除后隐藏所有删除按钮
                for (List<GoodsChilde> i1:list2){
                    for(int r=0;r<i1.size();r++) {
                        i1.get(r).setBtn(false);
                    }
                }
                notifyDataSetChanged();
                con.changesum(list2);
            }
        });
        //必须要设置,不然点击加减会崩,加减的点击事件
        holder.jiajia.setOnAddDelClickListener(new AddDeleteView.OnAddDelClickListener() {
            @Override
            public void onAddClick(View v) {
                int number = holder.jiajia.getNumber();
                number++;
                holder.jiajia.setNumber(number);
                list2.get(i).get(i1).setNum(number);
                con.changesum(list2);//计算的方法

            }

            @Override
            public void onDelClick(View v) {
                int number = holder.jiajia.getNumber();
                number--;
                holder.jiajia.setNumber(number);
                list2.get(i).get(i1).setNum(number);
                con.changesum(list2);

            }
        });


        //子级的选择框
        holder.ch2.setChecked(list2.get(i).get(i1).isCheckchild());
        holder.title.setText(list2.get(i).get(i1).getChildTitle());
        //图片路径拆分
        String img = list2.get(i).get(i1).getImg();
        String[] split = img.split("\\!");
        Glide.with(context).load(split[0]).into(holder.img);
        holder.price.setText(list2.get(i).get(i1).getPrice()+"");
        //子级的选择框逻辑
        holder.ch2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //定义一个flag用于跳出循环
                boolean flag=false;
                //获取我们子级的选中状态
                boolean checked = holder.ch2.isChecked();
                //将选中状态返回给我们存放子级的集合中
                list2.get(i).get(i1).setCheckchild(checked);
                //获取到Activity
                MainActivity con=(MainActivity)context;
                //循环我们的子级集合
                for (List<GoodsChilde> i1:list2){
                    //循环第二层的集合
                    for (int j=0;j<i1.size();j++){
                        //获取到子级条目数的选中状态
                        boolean chokx = i1.get(j).isCheckchild();
                        //如果我们的子级条目有一条未被选中
                        if(!chokx){
                            //那么就让我们的全选按钮为未选中
                            con.all_checKbox.setChecked(false);
                            //同时将父级的按钮也变为未选中
                            list.get(i).setCheckbox(false);
                            //flagtrue,跳出本层的循环
                            flag=true;
                            break;
                        }else{
                            con.all_checKbox.setChecked(true);
                            list.get(i).setCheckbox(true);
                        }
                    }
                    if (flag){
                        break;
                    }
                }
                con.changesum(list2);
                //遍历每一个父级下面的子级集合判断
                //获取到子级的集合长度
                int size = list2.get(i).size();
                //遍历子级集合
                for (int e=0;e<size;e++){
                    //获取到子级的选中状态
                    boolean chokx = list2.get(i).get(e).isCheckchild();
                    //如果子级有一条未被选中
                    if(!chokx){
                        //那么我们的父级按钮就不会被选中
                        list.get(i).setCheckbox(false);
                        break;
                    }else{
                        list.get(i).setCheckbox(true);
                    }
                }
                notifyDataSetChanged();
                con.changesum(list2);
            }
        });

        //控制删除按钮的显隐
        if(list2.get(i).get(i1).isBtn()){
            holder.btn.setVisibility(View.VISIBLE);
        }else{
            holder.btn.setVisibility(View.INVISIBLE);
        }
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    //两个ViewHolder内部类

    class GroupViewHolder{
        //控件
        CheckBox cb;
        TextView name;
    }
    class ChildViewHolder{
        CheckBox ch2;
        TextView title;
        ImageView img;
        TextView price;
        TextView txt_delete;
        TextView et_number;
        TextView txt_add;
        AddDeleteView jiajia;
        Button btn;
    }
}




 


 

 





 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值