Retrofit和Rxjava配合使用加载购物车

来源:互联网 发布:c语言do while 编辑:程序博客网 时间:2024/05/24 05:08

main的XML:

<RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"    android:layout_height="match_parent" tools:context="com.example.shoppingcar.View.MainActivity">    <com.example.shoppingcar.MyView.CarTitleView        android:id="@+id/ctv_title"        android:layout_width="match_parent"        android:layout_height="40dp"/>    <ExpandableListView        android:id="@+id/pt_lv"        android:layout_below="@id/ctv_title"        android:descendantFocusability = "blocksDescendants"        android:divider="@null"        android:scrollbars="none"        android:layout_width="match_parent"        android:layout_height="wrap_content">    </ExpandableListView>    <LinearLayout        android:visibility="gone"        android:orientation="vertical"        android:id="@+id/ll_toBuy"        android:layout_width="match_parent"        android:layout_height="wrap_content">        <ImageView            android:src="@drawable/shop_car"            android:layout_width="match_parent"            android:layout_height="wrap_content" />        <Button            android:layout_gravity="center"            android:id="@+id/btn_toBuy"            android:text="去逛逛"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />    </LinearLayout>    <LinearLayout        android:id="@+id/lll_one"        android:orientation="horizontal"        android:visibility="visible"        android:background="#FFFFFF"        android:layout_alignParentBottom="true"        android:layout_width="match_parent"        android:layout_height="50dp">        <CheckBox            android:layout_gravity="center"            android:id="@+id/cb_all"            android:text="全选"            android:textSize="16dp"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="wrap_content" />        <TextView            android:id="@+id/tv_rental"            android:layout_marginLeft="20dp"            android:gravity="center_vertical"            android:text="合计:¥0"            android:textSize="20dp"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="match_parent" />        <TextView            android:id="@+id/tv_num"            android:layout_marginLeft="10dp"            android:gravity="center_vertical"            android:text="数量:0"            android:textSize="20dp"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="match_parent" />        <Button            android:id="@+id/btn_goto"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_gravity="right"            android:gravity="center"            android:layout_height="match_parent"            android:background="#cc0000"            android:text="去结算"            android:textColor="#FFFF"            android:textSize="20dp" />    </LinearLayout>    <LinearLayout        android:id="@+id/l_two"        android:orientation="horizontal"        android:visibility="gone"        android:background="#FFFFFF"        android:layout_alignParentBottom="true"        android:layout_width="match_parent"        android:layout_height="50dp">        <CheckBox            android:layout_gravity="center"            android:id="@+id/cb_alldel"            android:text="全选"            android:textSize="16dp"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="wrap_content" />        <TextView            android:layout_marginLeft="20dp"            android:gravity="center_vertical"            android:text="分享"            android:textSize="20dp"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="match_parent" />        <TextView            android:layout_marginLeft="10dp"            android:gravity="center_vertical"            android:text="移出关注"            android:textSize="20dp"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="match_parent" />        <Button            android:id="@+id/btn_delete_all"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_gravity="right"            android:gravity="center"            android:layout_height="match_parent"            android:text="删除"            android:textColor="#FFFF"            android:textSize="20dp" />    </LinearLayout></RelativeLayout>

M层代码:

public class Model implements ModelInterface {    private Persent persent;    private Flowable<SelBean> selFlowable;    private Flowable<DelBean> delFlowable;    public Model(Persent persent){        this.persent = persent;    }    @Override    public void getData(Map<String, String> map, String uri, int tag) {        Log.e("-------M层的TAG值:",tag+"----");        if(tag==0){            selFlowable = RetrofitUtils.getInstance(map, uri, tag).getSelFlowable();            persent.getSelCar(selFlowable,tag);        }else        if(tag==1){            delFlowable = RetrofitUtils.getInstance(map, uri, tag).getDelFlowable();            if(delFlowable!=null) {                persent.getDelCar(delFlowable, tag);            }        }    }}

M层接口:

public interface ModelInterface {    void getData(Map<String, String> map, String uri, int tag);}

P层代码:

public class Persent implements PersentInterface{    private IVInterface ivInterface;    public Persent(IVInterface ivInterface){        this.ivInterface = ivInterface;    }    @Override    public void getData(Map<String, String> map, String uri, int tag) {        ModelInterface modelInterface = new Model(this);        modelInterface.getData(map,uri,tag);    }    //得到主页的网络请求    public void getSelCar(Flowable<SelBean> builder, final int tag){        builder.subscribeOn(Schedulers.io())                .observeOn(AndroidSchedulers.mainThread())                .subscribe(new DefaultSubscriber<SelBean>() {                    @Override                    public void onNext(SelBean selBean) {                        if(selBean!=null){                            ivInterface.Success(selBean,tag);                        }                    }                    @Override                    public void onError(Throwable t) {                        ivInterface.Failed(t.getMessage());                    }                    @Override                    public void onComplete() {                    }                });    }    //得到详情的网络请求    public void getDelCar(Flowable<DelBean> delBeanFlowable, final int tag){        delBeanFlowable.subscribeOn(Schedulers.io())        .observeOn(AndroidSchedulers.mainThread())        .subscribe(new DefaultSubscriber<DelBean>() {            @Override            public void onNext(DelBean delBean) {                if (delBean!=null){                    ivInterface.Success(delBean,tag);                }            }            @Override            public void onError(Throwable t) {                ivInterface.Failed(t.getMessage());            }            @Override            public void onComplete() {            }        });    }}

P层接口:

public interface PersentInterface {    void getData(Map<String, String> map, String uri, int tag);}

自定义网路请求工具类:

public class RetrofitUtils {    //单例模式    private static volatile RetrofitUtils instance;    private Flowable<SelBean> selBeanFlowable;    private Flowable<DelBean> delBeanFlowable;    private RetrofitUtils(Map<String,String> map, String uri, int tag){        OkHttpClient build = new OkHttpClient.Builder()                .addInterceptor(new Logger())                .build();        Retrofit build1 = new Retrofit.Builder()                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())                .addConverterFactory(GsonConverterFactory.create(new Gson()))                .client(build)                .baseUrl(uri)                .build();        RxJava rxJava = build1.create(RxJava.class);        Log.e("---TAG值:",tag+"");        if(tag==0){            selBeanFlowable = rxJava.Sel_Car(map);        }else        if(tag==1){            delBeanFlowable = rxJava.Del_Car(map);        }    };    public static RetrofitUtils getInstance(Map<String,String> map,String uri,int tag){        instance = new RetrofitUtils(map,uri,tag);        return instance;    }    public Flowable<SelBean> getSelFlowable(){        return selBeanFlowable;    }    public Flowable<DelBean> getDelFlowable(){        return delBeanFlowable;    }}

自定义拦截器:

public class Logger implements Interceptor {    @Override    public Response intercept(Chain chain) throws IOException {        Request original = chain.request();        HttpUrl url=original.url().newBuilder()                .addQueryParameter("source","android")                .build();        //添加请求头        Request request = original.newBuilder()                .url(url)                .build();        return chain.proceed(request);    }}

Retrofit的网络请求:

public interface RxJava {    @GET("product/getCarts")    Flowable<SelBean> Sel_Car(@QueryMap Map<String, String> map);    @GET("product/deleteCart")    Flowable<DelBean> Del_Car(@QueryMap Map<String, String> map);}

M层代码:

public class MainActivity extends AppCompatActivity implements IVInterface{    @BindView(R.id.ctv_title)    CarTitleView ctv_title;    @BindView(R.id.pt_lv)    ExpandableListView pt_lv;    @BindView(R.id.btn_toBuy)    Button btn_toBuy;    @BindView(R.id.ll_toBuy)    LinearLayout ll_toBuy;    @BindView(R.id.cb_all)    CheckBox cb_all;    @BindView(R.id.tv_rental)    TextView tv_rental;    @BindView(R.id.tv_num)    TextView tv_num;    @BindView(R.id.btn_goto)    Button btn_goto;    @BindView(R.id.lll_one)    LinearLayout lll_one;    @BindView(R.id.cb_alldel)    CheckBox cb_alldel;    @BindView(R.id.btn_delete_all)    Button btn_delete_all;    @BindView(R.id.l_two)    LinearLayout l_two;    private ArrayList<Car_shop_Bean> list_shop;    private ArrayList<List<Car_child_shop_Bean>> list_shop_chiled;    private CarShopAdapter carShopAdapter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ButterKnife.bind(this);        initTitle();        initSelCar();        initDelCar();    }    private void initDelCar() {        Persent persent = new Persent(MainActivity.this);        Map<String, String> map = new HashMap<>();        map.put("uid",1299+"");        persent.getData(map, "http://120.27.23.105/product/", 0);    }    private void initSelCar() {    }    //头布局的点击事件    private void initTitle() {        ctv_title.setCarTitleView(new CarTitleInterface() {            boolean b = true;            public TextView tv_compile;            //点击编辑----            @Override            public void compileClick(View v) {                b = !b;                tv_compile = (TextView) v.findViewById(R.id.tv_compile);                if (b) {                    tv_compile.setText("编辑");                    lll_one.setVisibility(View.VISIBLE);                    l_two.setVisibility(View.GONE);                    carShopAdapter.notifyDataSetChanged();                } else {                    tv_compile.setText("完成");                    lll_one.setVisibility(View.GONE);                    l_two.setVisibility(View.VISIBLE);                    carShopAdapter.notifyDataSetChanged();                }            }        });    }    @OnClick({R.id.btn_toBuy, R.id.cb_all, R.id.btn_goto, R.id.cb_alldel, R.id.btn_delete_all})    public void onViewClicked(View view) {        switch (view.getId()) {            case R.id.btn_toBuy:                break;            case R.id.cb_all:                boolean checked = cb_all.isChecked();                for (int i = 0; i < list_shop.size(); i++) {                    list_shop.get(i).setFlag(checked);                }                for (List<Car_child_shop_Bean> i : list_shop_chiled) {                    for (int j = 0; j < i.size(); j++) {                        i.get(j).setChildCheck(checked);                    }                }                carShopAdapter.notifyDataSetChanged();                foreach(list_shop_chiled);                break;            case R.id.btn_goto:                break;            case R.id.cb_alldel:                boolean checkeds = cb_alldel.isChecked();                for (int i = 0; i < list_shop.size(); i++) {                    list_shop.get(i).setFlag(checkeds);                }                for (List<Car_child_shop_Bean> i : list_shop_chiled) {                    for (int j = 0; j < i.size(); j++) {                        i.get(j).setChildCheck(checkeds);                    }                }                carShopAdapter.notifyDataSetChanged();                break;            //点击删除            case R.id.btn_delete_all:                ArrayList<Car_shop_Bean> list_new_shop = new ArrayList<Car_shop_Bean>();                ArrayList<List<Car_child_shop_Bean>> list_new_child = new ArrayList<List<Car_child_shop_Bean>>();                for (int i = 0; i < list_shop.size(); i++) {                    Car_shop_Bean car_shop_bean = list_shop.get(i);                    List<Car_child_shop_Bean> car_child_shop_been = list_shop_chiled.get(i);                    Boolean flag = list_shop.get(i).getFlag();                    if (flag) {                        list_new_shop.add(car_shop_bean);                        list_new_child.add(car_child_shop_been);                    }                    ArrayList<Car_child_shop_Bean> list_child_new = new ArrayList<Car_child_shop_Bean>();                    for (int j = 0; j < list_shop_chiled.get(i).size(); j++) {                        Car_child_shop_Bean car_child_shop_bean = list_shop_chiled.get(i).get(j);                        boolean childCheck = list_shop_chiled.get(i).get(j).isChildCheck();                        if (childCheck) {                            list_child_new.add(car_child_shop_bean);                            int pid = car_child_shop_bean.getPid();                            Map<String, String> map = new HashMap<String, String>();                            map.put("pid", pid + "");                            map.put("uid", 1299 + "");                            Persent persent = new Persent(MainActivity.this);                            persent.getData(map, "http://120.27.23.105/product/", 1);                        }                    }                    list_shop_chiled.get(i).removeAll(list_child_new);                }                tv_rental.setText("合计:¥" + 0);                tv_num.setText("数量:" + 0);                list_shop.removeAll(list_new_shop);                list_shop_chiled.removeAll(list_new_child);                carShopAdapter.notifyDataSetChanged();                initAllData(list_shop);                break;        }    }    //请求成功    @Override    public void Success(Object o,int tag) {        //查询购物车        if(tag==0){            SelBean selBean = (SelBean)o;            if(selBean!=null){                List<SelBean.DataBean> data = selBean.getData();                list_shop = new ArrayList<Car_shop_Bean>();                list_shop_chiled = new ArrayList<List<Car_child_shop_Bean>>();                //设置选择状态                for (int i = 0; i < data.size(); i++) {                    String sellerName = data.get(i).getSellerName();                    list_shop.add(new Car_shop_Bean(sellerName, false));                    List<SelBean.DataBean.ListBean> list = data.get(i).getList();                    ArrayList<Car_child_shop_Bean> car_shop_been = new ArrayList<>();                    for (SelBean.DataBean.ListBean childList : list) {                        int num = childList.getNum();                        float price = childList.getPrice();                        String images = childList.getImages();                        String title = childList.getTitle();                        int pid = childList.getPid();                        String[] split = images.toString().split("\\|");                        car_shop_been.add(new Car_child_shop_Bean(pid, title, num, price, split[0], false));                    }                    list_shop_chiled.add(car_shop_been);                }                initAllData(list_shop);                //设置适配器                carShopAdapter = new CarShopAdapter(MainActivity.this, list_shop, list_shop_chiled);                cb_all.setOnClickListener(new View.OnClickListener() {                    @Override                    public void onClick(View view) {                        boolean checked = cb_all.isChecked();                        for (int i = 0; i < list_shop.size(); i++) {                            list_shop.get(i).setFlag(checked);                        }                        for (List<Car_child_shop_Bean> i : list_shop_chiled) {                            for (int j = 0; j < i.size(); j++) {                                i.get(j).setChildCheck(checked);                            }                        }                        carShopAdapter.notifyDataSetChanged();                        foreach(list_shop_chiled);                    }                });                cb_alldel.setOnClickListener(new View.OnClickListener() {                    @Override                    public void onClick(View view) {                        boolean checked = cb_alldel.isChecked();                        for (int i = 0; i < list_shop.size(); i++) {                            list_shop.get(i).setFlag(checked);                        }                        for (List<Car_child_shop_Bean> i : list_shop_chiled) {                            for (int j = 0; j < i.size(); j++) {                                i.get(j).setChildCheck(checked);                            }                        }                        carShopAdapter.notifyDataSetChanged();                    }                });                carShopAdapter.setCarShop(new CarShopCheckedInterface() {                    @Override                    public void AllChecked(boolean ischeck) {                        if (!ischeck) {                            cb_all.setChecked(false);                            cb_alldel.setChecked(false);                        } else {                            cb_all.setChecked(true);                            cb_alldel.setChecked(true);                        }                    }                });                carShopAdapter.setNumberAndPrice(new NumberAndPrice() {                    @Override                    public void AllNumAndPriceClick(final ArrayList<List<Car_child_shop_Bean>> list_shop_child) {                        int max = 0;                        int nums = 0;                        for (List<Car_child_shop_Bean> list_child : list_shop_child) {                            for (int i = 0; i < list_child.size(); i++) {                                boolean childCheck = list_child.get(i).isChildCheck();                                Car_child_shop_Bean car_child_shop_bean = list_child.get(i);                                if (childCheck) {                                    int num = car_child_shop_bean.getNum();                                    double price = car_child_shop_bean.getPrice();                                    int prices = (int) price;                                    max += prices * num;                                    nums++;                                }                            }                        }                        tv_rental.setText("合计:¥" + max);                        tv_num.setText("数量:" + nums);                    }                });                pt_lv.setAdapter(carShopAdapter);                //让他默认收回                int count = pt_lv.getCount();                for (int i = 0; i < count; i++) {                    pt_lv.expandGroup(i);                }                pt_lv.setGroupIndicator(null);            }        }else        //删除购物车        if(tag==1){        }    }    //请求失败    @Override    public void Failed(String message) {    }    //判断集合中有没有数据    private void initAllData(ArrayList<Car_shop_Bean> list_shop) {        if(list_shop.size()==0){            ll_toBuy.setVisibility(View.VISIBLE);        }else{            ll_toBuy.setVisibility(View.GONE);            pt_lv.setVisibility(View.VISIBLE);        }    }    //计算总价和数量    public void foreach(ArrayList<List<Car_child_shop_Bean>> list_shop_child) {        carShopAdapter.notifyDataSetChanged();        int max = 0;        int nums = 0;        for (List<Car_child_shop_Bean> list_child : list_shop_child) {            for (int i = 0; i < list_child.size(); i++) {                boolean childCheck = list_child.get(i).isChildCheck();                Car_child_shop_Bean car_child_shop_bean = list_child.get(i);                if (childCheck) {                    int num = car_child_shop_bean.getNum();                    double price = car_child_shop_bean.getPrice();                    int prices = (int) price;                    max += prices * num;                    nums++;                }            }        }        tv_rental.setText("合计:¥" + max);        tv_num.setText("数量:" + nums);    }}

M层接口:

public interface IVInterface {    void Success(Object o,int tag);    void Failed(String message);}

适配器:

public class CarShopAdapter extends BaseExpandableListAdapter{    private Context context;    private ArrayList<Car_shop_Bean> list_shop;    private ArrayList<List<Car_child_shop_Bean>> list_shop_child;    private CarShopCheckedInterface carShopCheckedInterface;    private ShopNumInterface shopNumInterface;    private NumberAndPrice numberAndPrice;    private TextView tv_shopName;    private ImageView iv_commodityImg;    private TextView tv_commodityName;    private TextView tv_commodityPrice;    private Button btn_delete;    private Button btn_add;    private EditText et_num;    private int shopNum = 0;    private int allNum = 0;    private int allPrice = 0;    private Button btn_delete_only;    public CarShopAdapter(Context context, ArrayList<Car_shop_Bean> list_shop, ArrayList<List<Car_child_shop_Bean>> list_shop_child) {        this.context = context;        this.list_shop = list_shop;        this.list_shop_child = list_shop_child;    }    public void setCarShop(CarShopCheckedInterface carShopCheckedInterface){        this.carShopCheckedInterface = carShopCheckedInterface;    }    public void setShopList(ShopNumInterface shopNumInterface){        this.shopNumInterface = shopNumInterface;    }    public void setNumberAndPrice(NumberAndPrice numberAndPrice){        this.numberAndPrice = numberAndPrice;    }    @Override    public int getGroupCount() {        return list_shop.size();    }    @Override    public int getChildrenCount(int i) {        return list_shop_child.get(i).size();    }    @Override    public Object getGroup(int i) {        return list_shop.get(i);    }    @Override    public Object getChild(int i, int i1) {        return list_shop_child.get(i).get(i1);    }    @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 Car_shop_Bean car_shop_bean1 = list_shop.get(i);        final List<Car_child_shop_Bean> car_child_shop_been = list_shop_child.get(i);            view = View.inflate(context, R.layout.car_shop_one_fragment, null);            final CheckBox rb_shop = (CheckBox)view.findViewById(R.id.rb_shop);            tv_shopName = (TextView)view.findViewById(R.id.tv_shopName);            rb_shop.setChecked(car_shop_bean1.getFlag());            tv_shopName.setText(car_shop_bean1.getName());            rb_shop.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                Log.e("---------点击:",rb_shop.isChecked()+"");                    list_shop.get(i).setFlag(rb_shop.isChecked());                    boolean checked = rb_shop.isChecked();                    car_shop_bean1.setFlag(checked);                    boolean a = true;                        for (int j = 0; j < list_shop.size(); j++) {                            Boolean flag = list_shop.get(j).getFlag();                            if(flag==false){                                a = false;                                carShopCheckedInterface.AllChecked(a);                                break;                            }else{                                a = true;                                carShopCheckedInterface.AllChecked(a);                            }                        }                        for (int j = 0; j < car_child_shop_been.size(); j++) {                            car_child_shop_been.get(j).setChildCheck(rb_shop.isChecked());                        }                        notifyDataSetChanged();                        for (Car_child_shop_Bean list_child:car_child_shop_been){                            numberAndPrice.AllNumAndPriceClick(list_shop_child);                        }                    }                });        return view;    }    @Override    public View getChildView(final int i, final int i1, final boolean b, View view, ViewGroup viewGroup) {        final Car_shop_Bean car_shop_bean = list_shop.get(i);        final Car_child_shop_Bean car_child_shop_bean = list_shop_child.get(i).get(i1);        view = View.inflate(context, R.layout.car_shop_fragment, null);        final CheckBox rb_commodity = (CheckBox)view.findViewById(R.id.rb_commodity);        iv_commodityImg = (ImageView)view.findViewById(R.id.iv_commodityImg);        tv_commodityName = (TextView)view.findViewById(R.id.tv_commodityName);        tv_commodityPrice = (TextView)view.findViewById(R.id.tv_commodityPrice);        btn_delete = (Button)view.findViewById(R.id.btn_delete);        btn_add = (Button)view.findViewById(R.id.btn_add);        et_num = (EditText)view.findViewById(R.id.et_num);        et_num.setText(car_child_shop_bean.getNum()+"");        rb_commodity.setChecked(car_child_shop_bean.isChildCheck());        tv_commodityName.setText(car_child_shop_bean.getTitle());        Glide.with(context).load(car_child_shop_bean.getImages()).into(iv_commodityImg);        tv_commodityPrice.setText("¥:"+car_child_shop_bean.getPrice());        btn_delete.setText("—");        btn_add.setText("+");        notifyDataSetChanged();        rb_commodity.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                car_child_shop_bean.setChildCheck(rb_commodity.isChecked());                boolean flag = true;                for (int j = 0; j < list_shop_child.get(i).size(); j++) {                    if(list_shop_child.get(i).get(j).isChildCheck()==false) {                        flag = false;                        break;                    }                }                if(flag){                    list_shop.get(i).setFlag(true);                }else{                    list_shop.get(i).setFlag(false);                }                boolean c = true;                for (int j = 0; j < list_shop.size(); j++) {                    if(list_shop.get(j).getFlag() == false){                        c = false;                        break;                    }else{                        c = true;                    }                }                if(c){                    carShopCheckedInterface.AllChecked(true);                }else{                    carShopCheckedInterface.AllChecked(false);                }                Log.e("-----取到的索引:",i1+"");                numberAndPrice.AllNumAndPriceClick(list_shop_child);                notifyDataSetChanged();            }        });        btn_add.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                boolean childCheck = car_child_shop_bean.isChildCheck();                int num1 = car_child_shop_bean.getNum();                num1++;                car_child_shop_bean.setNum(num1);                numberAndPrice.AllNumAndPriceClick(list_shop_child);                notifyDataSetChanged();            }        });        final boolean childCheck = car_child_shop_bean.isChildCheck();        btn_delete.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                int num1 = car_child_shop_bean.getNum();                num1--;                if(num1<1){                    car_child_shop_bean.setNum(1);                    Toast.makeText(context,"不能再少了···",Toast.LENGTH_SHORT).show();                }else{                    car_child_shop_bean.setNum(num1);                }                numberAndPrice.AllNumAndPriceClick(list_shop_child);                notifyDataSetChanged();            }        });        return view;    }    @Override    public boolean isChildSelectable(int i, int i1) {        return true;    }}

接口类:

public interface CarShopCheckedInterface {    void AllChecked(boolean ischeck);}public interface CarTitleInterface {    void compileClick(View v);}public interface NumberAndPrice {    void AllNumAndPriceClick(ArrayList<List<Car_child_shop_Bean>> list_shop_child);}public interface ShopNumInterface {    void ListClick(List<Car_child_shop_Bean> car_child_shop_been);}

Car_child_shop_Bean类:

public class Car_child_shop_Bean {    private int pid;    private String title;    private int num;    private double price;    private String images;    private boolean childCheck;    public Car_child_shop_Bean(int pid, String title, int num, double price, String images, boolean childCheck) {        this.pid = pid;        this.title = title;        this.num = num;        this.price = price;        this.images = images;        this.childCheck = childCheck;    }    public int getPid() {        return pid;    }    public void setPid(int pid) {        this.pid = pid;    }    public String getTitle() {        return title;    }    public void setTitle(String title) {        this.title = title;    }    public int getNum() {        return num;    }    public void setNum(int num) {        this.num = num;    }    public double getPrice() {        return price;    }    public void setPrice(double price) {        this.price = price;    }    public String getImages() {        return images;    }    public void setImages(String images) {        this.images = images;    }    public boolean isChildCheck() {        return childCheck;    }    public void setChildCheck(boolean childCheck) {        this.childCheck = childCheck;    }}

Car_shop_Bean类:

public class Car_shop_Bean {    private String name;    private boolean flag;    public Car_shop_Bean(String name, Boolean flag) {        this.name = name;        this.flag = flag;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Boolean getFlag() {        return flag;    }    public void setFlag(Boolean flag) {        this.flag = flag;    }}

DelBean类:

public class DelBean {    private String msg;    private String code;    public String getMsg() {        return msg;    }    public void setMsg(String msg) {        this.msg = msg;    }    public String getCode() {        return code;    }    public void setCode(String code) {        this.code = code;    }}

SelBean类:

public class SelBean {    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 {        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 {            private double bargainPrice;            private String createtime;            private String detailUrl;            private String images;            private int num;            private int pid;            private float 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 float getPrice() {                return price;            }            public void setPrice(float 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;            }        }    }}

自定义View的头布局:

public class CarTitleView  extends LinearLayout implements View.OnClickListener{    private CarTitleInterface carTitleInterface;    private ImageView title_address;    private TextView tv_compile;    private ImageView iv_message;    public void setCarTitleView(CarTitleInterface carTitleInterface){        this.carTitleInterface = carTitleInterface;    }    public CarTitleView(Context context) {        this(context,null);    }    public CarTitleView(Context context, @Nullable AttributeSet attrs) {        this(context, attrs,0);    }    public CarTitleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        View.inflate(context, R.layout.car_title_fragment,this);        initView();    }    private void initView() {        tv_compile = (TextView)findViewById(R.id.tv_compile);        tv_compile.setOnClickListener(this);    }    @Override    public void onClick(View view) {        switch (view.getId()){            case R.id.tv_compile:                Log.e("-------点击事件:","--------");                carTitleInterface.compileClick(view);                break;        }    }}

Car_title_fragment的布局:

<LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">            <TextView                android:text="购物车"                android:textSize="24dp"                android:gravity="center"                android:paddingLeft="80dp"                android:layout_width="0dp"                android:layout_weight="7"                android:layout_height="wrap_content" />            <TextView                android:layout_marginLeft="50dp"                android:id="@+id/tv_compile"                android:text="编辑"                android:layout_width="wrap_content"                android:layout_height="match_parent"                android:gravity="right"                android:textSize="18dp" /></LinearLayout>

Car_shop_one_fragment布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <LinearLayout        android:layout_marginTop="20dp"        android:background="#F8F8F8"        android:layout_width="match_parent"        android:layout_height="wrap_content">        <CheckBox            android:id="@+id/rb_shop"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="wrap_content" />        <TextView            android:id="@+id/tv_shopName"            android:layout_marginLeft="10dp"            android:textSize="20dp"            android:layout_width="0dp"            android:layout_weight="9"            android:layout_height="wrap_content">        </TextView>    </LinearLayout></LinearLayout>

Car_shop_fragment代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout    android:background="#F0F2F5"    android:orientation="vertical"    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <LinearLayout        android:background="#F7FBF7"        android:layout_width="match_parent"        android:layout_height="200dp">        <CheckBox            android:id="@+id/rb_commodity"            android:layout_gravity="center"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="wrap_content" />        <LinearLayout            android:layout_marginLeft="10dp"            android:orientation="horizontal"            android:layout_width="0dp"            android:layout_weight="9"            android:layout_height="300dp">            <ImageView                android:gravity="center"                android:id="@+id/iv_commodityImg"                android:layout_width="80dp"                android:layout_height="80dp" />            <LinearLayout                android:orientation="vertical"                android:layout_width="match_parent"                android:layout_height="match_parent">                <TextView                    android:id="@+id/tv_commodityName"                    android:gravity="center"                    android:layout_width="match_parent"                    android:layout_weight="1"                    android:layout_height="0dp" />                <LinearLayout                    android:layout_width="match_parent"                    android:layout_height="0dp"                    android:layout_weight="1">                    <TextView                        android:textSize="20dp"                        android:id="@+id/tv_commodityPrice"                        android:layout_width="0dp"                        android:layout_weight="1"                        android:layout_marginTop="20dp"                        android:layout_height="wrap_content"                        android:text="ooooooo" />                    <LinearLayout                        android:id="@+id/ll_num"                        android:layout_width="0dp"                        android:layout_weight="1"                        android:gravity="right"                        android:layout_height="match_parent"                        >                        <Button                            android:id="@+id/btn_delete"                            android:layout_width="0dp"                            android:layout_weight="1"                            android:layout_height="wrap_content"/>                        <EditText                            android:id="@+id/et_num"                            android:gravity="center"                            android:layout_width="0dp"                            android:layout_weight="2"                            android:layout_height="wrap_content" />                        <Button                            android:id="@+id/btn_add"                            android:layout_width="0dp"                            android:layout_weight="1"                            android:layout_height="wrap_content" />                    </LinearLayout>                </LinearLayout>            </LinearLayout>        </LinearLayout>    </LinearLayout></LinearLayout>

所用依赖:

    compile 'com.squareup.okhttp3:okhttp:3.9.0'    compile 'com.google.code.gson:gson:2.8.2'    compile 'com.github.bumptech.glide:glide:3.7.0'    compile 'com.jakewharton:butterknife:8.8.1'    annotationProcessor'com.jakewharton:butterknife-compiler:8.8.1'    compile 'com.facebook.fresco:fresco:0.13.0'    compile 'com.squareup.retrofit2:retrofit:2.3.0'    compile 'com.squareup.retrofit2:converter-gson:2.0.2'    compile 'io.reactivex.rxjava2:rxjava:2.1.6'    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'    compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'

沉浸式:

Window window = getWindow();        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {        //透明状态栏            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);        //透明导航栏            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);        }

ViewPager的预加载页数:

vpFragment.setOffscreenPageLimit(3);
原创粉丝点击