详情页跳到购物车

需要添加的依赖

 compile 'com.github.bumptech.glide:glide:3.7.0'
    compile 'com.squareup.okhttp3:okhttp:3.9.0'
    compile 'com.google.code.gson:gson:2.8.1'
    compile 'com.jakewharton:butterknife:8.5.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
    compile 'com.android.support:recyclerview-v7:25.0.0'
    compile 'com.squareup.retrofit2:retrofit:2.3.0'//retrofit依赖
    compile 'com.squareup.retrofit2:converter-gson:2.3.0'//retrofit内部封装的GSON
    compile "io.reactivex.rxjava2:rxjava:2.1.7"
    compile "com.squareup.retrofit2:adapter-rxjava2:2.3.0"
    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    compile 'com.facebook.fresco:fresco:0.12.0'
    compile 'com.facebook.fresco:animated-base-support:0.12.0'
    compile 'com.facebook.fresco:animated-webp:0.12.0'
    compile 'com.facebook.fresco:webpsupport:0.12.0'

项目清单里添加的

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

value下的attrs

  <declare-styleable name="AddDeleteViewStyle">
        <attr name="left_text" format="string"></attr>
        <attr name="right_text" format="string"></attr>
        <attr name="middle_text" format="string"></attr>
        <attr name="left_text_color" format="color"></attr>
    </declare-styleable>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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.bawie.www.gw_che.view.MainActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/my_image_view"
            android:layout_width="match_parent"
            android:layout_height="450dp"
            />

        <TextView
            android:id="@+id/goods_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="a啊啊是你的"
            android:textSize="20dp" />

        <TextView
            android:id="@+id/goods_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="¥ 18132"
            android:textColor="#dc5f2e"
            android:textSize="18dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="我是商家19"
            android:textSize="16dp" />


    </LinearLayout>

    <LinearLayout
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_addcart"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#ffc207"
            android:text="加入购物车"/>

        <Button
            android:id="@+id/btn_shopping"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="#ff6b06"
            android:text="立即购买"/>

    </LinearLayout>



</RelativeLayout>

activity_main2

<?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"
                android:visibility="visible" />
        </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>

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"
    xmlns:fresco="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" />-->
        <com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/shop_img"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:layout_marginTop="30dp"
            fresco:placeholderImage="@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.bawie.www.gw_che.myview.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.bawie.www.gw_che.myview.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:visibility="invisible"
            android:text="删除" />
    </RelativeLayout>


</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>

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>

view层下面的类MainActivity

package com.bawie.www.gw_che.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.bawie.www.gw_che.R;
import com.bawie.www.gw_che.entity.GoodsShowBean;
import com.bawie.www.gw_che.presenter.NewsPresenter;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity implements IView {

    @BindView(R.id.my_image_view)
    SimpleDraweeView myImageView;
    @BindView(R.id.goods_title)
    TextView goodsTitle;
    @BindView(R.id.goods_price)
    TextView goodsPrice;
    private NewsPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        presenter = new NewsPresenter();
        presenter.attachView(this);
        //http://120.27.23.105/product/getProductDetail?pid=75&source=android
        Map<String, String> map = new HashMap<>();
        map.put("pid", "75");
        presenter.getData(map, "goods");


    }

    @Override
    public void OnSuccess(Object o, String tag) {
        if (tag.equals("goods")) {
            if (o != null) {
                GoodsShowBean data = (GoodsShowBean) o;
                //Log.e("Gp", "OnSuccess: " + data.toString());

                myImageView.setImageURI(data.getImages().split("\\|")[0]);
                goodsTitle.setText(data.getTitle());
                goodsPrice.setText(""+data.getPrice());
            }
        }
    }

    @Override
    public void OnFailed(Exception e, String tag) {

    }

    @OnClick({R.id.btn_addcart, R.id.btn_shopping})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_addcart:
                Toast.makeText(MainActivity.this,"添加成功",Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_shopping:
                Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);
                break;
        }
    }
}

Main2Activity

package com.bawie.www.gw_che.view;

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

import com.bawie.www.gw_che.R;
import com.bawie.www.gw_che.adapter.ExpandableAdapter;
import com.bawie.www.gw_che.entity.CartBean;
import com.bawie.www.gw_che.entity.ChildBean;
import com.bawie.www.gw_che.entity.GroupBean;
import com.bawie.www.gw_che.presenter.NewsPresenter;

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

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Main2Activity extends AppCompatActivity implements IView {

    @BindView(R.id.exListView)
    ExpandableListView exListView;
    @BindView(R.id.all_chekbox)
    public CheckBox allCheckbox;
    @BindView(R.id.total_price)
    TextView totalPrice;
    @BindView(R.id.total_number)
    TextView totalnumber;

    private ExpandableAdapter expandableAdapter;

    private boolean flagedit = true;


    private NewsPresenter presenter;

    List<GroupBean> groupBeen = new ArrayList<>();
    List<List<ChildBean>> childBeen = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        ButterKnife.bind(this);

        //获取二级列表适配器
        expandableAdapter = new ExpandableAdapter(Main2Activity.this, groupBeen, childBeen);
        exListView.setAdapter(expandableAdapter);

        exListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
                return true;
            }
        });


        presenter = new NewsPresenter();
        presenter.attachView(this);
        //http://120.27.23.105/product/getCarts?uid=3802&source=android
        Map<String, String> map = new HashMap<>();
        map.put("uid", "3802");
        presenter.getData(map, "cart");


    }


    @Override
    public void OnSuccess(Object o, String tag) {
        if (tag.equals("cart")) {
            if (o != null) {
                List<CartBean> data = (List<CartBean>) o;
                //Log.e("Gp", "OnSuccess: " + data.toString());
                for (CartBean i : data) {
                    GroupBean groupBean = new GroupBean(i.getSellerName(), false);
                    this.groupBeen.add(groupBean);
                    List<CartBean.ListBean> list = i.getList();
                    List<ChildBean> ls = new ArrayList<>();
                    for (CartBean.ListBean w : list) {
                        String[] split = w.getImages().split("\\|");
                        ChildBean childBean = new ChildBean(w.getTitle(), split[0], w.getPrice(), 1, false, false);
                        ls.add(childBean);
                    }
                    this.childBeen.add(ls);

                }
                for(int i = 0; i < expandableAdapter.getGroupCount(); i++){
                    exListView.expandGroup(i);
                }
                expandableAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void OnFailed(Exception e, String tag) {

    }

    @OnClick({R.id.back, R.id.edit,R.id.all_chekbox})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.edit:
                for (List<ChildBean> i1:childBeen){
                    for(int r=0;r<i1.size();r++) {
                        i1.get(r).setBtn(flagedit);
                    }
                }
                flagedit=!flagedit;
                expandableAdapter.notifyDataSetChanged();
                break;
            case R.id.all_chekbox:
                boolean checked = allCheckbox.isChecked();
                //改变一级item复选框
                for (GroupBean i:groupBeen){
                    i.setGropuCb(checked);
                }
                //改变二级item复选框
                for (List<ChildBean> i1:childBeen){
                    for(int r=0;r<i1.size();r++) {
                        i1.get(r).setChildCb(checked);
                    }
                }
                expandableAdapter.notifyDataSetChanged();
                changesum(childBeen);
                break;
        }
    }

    //计算和数量总价
    public void changesum(List<List<ChildBean>> childBeen){
        int count=0;
        double sum=0;
        for (List<ChildBean> i1:childBeen){
            for(int r=0;r<i1.size();r++) {
                boolean childCb1 = i1.get(r).isChildCb();
                if(childCb1){
                    double price = i1.get(r).getPrice();
                    int num = i1.get(r).getNum();
                    sum+=price*num;
                    count++;
                }
            }
        }
        totalPrice.setText(""+sum);
        totalnumber.setText("共有商品:"+count+"");
    }

}

IView

package com.bawie.www.gw_che.view;

/**
 * 作者:戈鹏
 * on 2017/12/19 09:19
 */

public interface IView {
        void OnSuccess(Object o, String tag);
        void OnFailed(Exception e, String tag);

}

presenter层IPresenter

package com.bawie.www.gw_che.presenter;

import java.util.Map;

/**
 * 作者:戈鹏
 * on 2017/12/19 09:17
 */

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

NewsPresenter

package com.bawie.www.gw_che.presenter;
import com.bawie.www.gw_che.entity.CartBean;
import com.bawie.www.gw_che.entity.GoodsShowBean;
import com.bawie.www.gw_che.entity.MessageBean;
import com.bawie.www.gw_che.model.Model;
import com.bawie.www.gw_che.view.IView;

import java.util.List;
import java.util.Map;

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


/**
 * 作者:戈鹏
 * on 2017/12/19 09:20
 */

public class NewsPresenter implements IPresenter{

    private IView iView;
    private DisposableSubscriber<MessageBean<GoodsShowBean>> subscriber;
    private DisposableSubscriber<MessageBean<List<CartBean>>> subscriber2;

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


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

    public void detachView(){

        if(iView!=null){
            iView=null;
        }

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

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

    }


    public void get(Flowable<MessageBean<GoodsShowBean>> flowable , final String tag) {
        subscriber = flowable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber<MessageBean<GoodsShowBean>>() {
                    @Override
                    public void onNext(MessageBean<GoodsShowBean> listMessageBean) {
                        if (listMessageBean != null) {
                            GoodsShowBean data = listMessageBean.getData();
                            iView.OnSuccess(data,tag);
                        }
                    }

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

                    @Override
                    public void onComplete() {

                    }
                });

    }

    public void get2(Flowable<MessageBean<List<CartBean>>> flowable, final String tag) {
        subscriber2 = flowable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new DisposableSubscriber<MessageBean<List<CartBean>>>() {
                    @Override
                    public void onNext(MessageBean<List<CartBean>> list) {
                        if (list != null) {
                            List<CartBean> data = list.getData();
                            if(data!=null){
                                iView.OnSuccess(data,tag);
                            }
                        }
                    }

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

                    @Override
                    public void onComplete() {

                    }
                });
    }


}

myview层AddDeleteView

package com.bawie.www.gw_che.myview;

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.bawie.www.gw_che.R;


/**
 * 作者:戈鹏
 * on 2017/12/19 10:12
 */

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= (TextView) findViewById(R.id.txt_delete);
        TextView txtAdd= (TextView) findViewById(R.id.txt_add);
        etNumber = (EditText) 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.RED);

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



}

model层的IModel类

package com.bawie.www.gw_che.model;

import java.util.Map;

/**
 * 作者:戈鹏
 * on 2017/12/19 09:18
 */

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

Model类

package com.bawie.www.gw_che.model;


import com.bawie.www.gw_che.entity.CartBean;
import com.bawie.www.gw_che.entity.GoodsShowBean;
import com.bawie.www.gw_che.entity.MessageBean;
import com.bawie.www.gw_che.http.RetrofitUtils;
import com.bawie.www.gw_che.presenter.NewsPresenter;

import java.util.List;
import java.util.Map;

import io.reactivex.Flowable;


public class Model implements IModel{

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

    /*@Override
    public void getData(Map<String, String> map) {
        Flowable<MessageBean<List<Newslist>>> flowable = RetrofitUtils.getInstance().getApiService().getNews(map);
        presenter.get(flowable);
    }*/

    @Override
    public void getData(Map<String, String> map, String tag) {
        if(tag.equals("cart")){
            Flowable<MessageBean<List<CartBean>>> flowable = RetrofitUtils.getInstance().getApiService().getCart(map);
            presenter.get2(flowable,tag);
        }else if(tag.equals("goods")){
            Flowable<MessageBean<GoodsShowBean>> flowable = RetrofitUtils.getInstance().getApiService().getNews(map);
            presenter.get(flowable,tag);
        }

    }
}

http层的ApiService

package com.bawie.www.gw_che.http;


import com.bawie.www.gw_che.entity.CartBean;
import com.bawie.www.gw_che.entity.GoodsShowBean;
import com.bawie.www.gw_che.entity.MessageBean;

import java.util.List;
import java.util.Map;

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

public interface ApiService {
    //http://120.27.23.105/product/getProductDetail?pid=75&source=android
    @GET("product/getProductDetail")
    Flowable<MessageBean<GoodsShowBean>> getNews(@QueryMap Map<String, String> map);

    //查询购物车
    //http://120.27.23.105/product/getCarts?uid=3802&source=android
    @GET("product/getCarts")
    Flowable<MessageBean<List<CartBean>>> getCart(@QueryMap Map<String, String> map);

}

RetrofitUtils

package com.bawie.www.gw_che.http;

import java.io.IOException;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;


public class RetrofitUtils {
    private static volatile RetrofitUtils instance;
    private final Retrofit retrofit;

    private RetrofitUtils(){

        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Logger()).build();

        retrofit = new Retrofit.Builder()
                .client(client)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl("http://120.27.23.105/")
                .build();
    }

    public static RetrofitUtils getInstance(){
        if(instance==null){
            synchronized (RetrofitUtils.class){
                if(instance==null){
                    instance=new RetrofitUtils();
                }
            }
        }
        return instance;
    }

    public ApiService getApiService(){
        ApiService apiService = retrofit.create(ApiService.class);
        return apiService;
    }

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

}

entity下的bean类CartBean

package com.bawie.www.gw_che.entity;

import java.util.List;

/**
 * 作者:戈鹏
 * on 2017/12/19 09:24
 */

public class CartBean {
    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 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;
        }
    }

    @Override
    public String toString() {
        return "CartBean{" +
                "sellerName='" + sellerName + '\'' +
                ", sellerid='" + sellerid + '\'' +
                ", list=" + list +
                '}';
    }
}

ChildBean

package com.bawie.www.gw_che.entity;

/**
 * 作者:戈鹏
 * on 2017/12/19 10:09
 */

public class ChildBean {
    private String title;
    private String images;
    private double price;
    private int num;
    private boolean childCb;
    private boolean btn;


    public ChildBean(String title, String images, double price, int num, boolean childCb, boolean btn) {
        this.title = title;
        this.images = images;
        this.price = price;
        this.num = num;
        this.childCb = childCb;
        this.btn = btn;
    }

    public boolean isBtn() {
        return btn;
    }

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


    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getImages() {
        return images;
    }

    public void setImages(String images) {
        this.images = images;
    }

    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 isChildCb() {
        return childCb;
    }

    public void setChildCb(boolean childCb) {
        this.childCb = childCb;
    }

    @Override
    public String toString() {
        return "ChildBean{" +
                "title='" + title + '\'' +
                ", images='" + images + '\'' +
                ", price=" + price +
                ", num=" + num +
                ", childCb=" + childCb +
                '}';
    }
}

GoodsShowBean

package com.bawie.www.gw_che.entity;

/**
 * 作者:戈鹏
 * on 2017/12/19 09:07
 */

public class GoodsShowBean {

        private double bargainPrice;
        private String createtime;
        private String detailUrl;
        private String images;
        private int itemtype;
        private int pid;
        private double price;
        private int pscid;
        private int salenum;
        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 getItemtype() {
            return itemtype;
        }

        public void setItemtype(int itemtype) {
            this.itemtype = itemtype;
        }

        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 getSalenum() {
            return salenum;
        }

        public void setSalenum(int salenum) {
            this.salenum = salenum;
        }

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

    @Override
    public String toString() {
        return "GoodsShowBean{" +
                "bargainPrice=" + bargainPrice +
                ", createtime='" + createtime + '\'' +
                ", detailUrl='" + detailUrl + '\'' +
                ", images='" + images + '\'' +
                ", itemtype=" + itemtype +
                ", pid=" + pid +
                ", price=" + price +
                ", pscid=" + pscid +
                ", salenum=" + salenum +
                ", sellerid=" + sellerid +
                ", subhead='" + subhead + '\'' +
                ", title='" + title + '\'' +
                '}';
    }
}

GroupBean

package com.bawie.www.gw_che.entity;

/**
 * 作者:戈鹏
 * on 2017/12/19 10:08
 */

public class GroupBean {
    private String sellerName;
    private boolean gropuCb;

    public GroupBean(String sellerName, boolean gropuCb) {
        this.sellerName = sellerName;
        this.gropuCb = gropuCb;
    }

    public String getSellerName() {
        return sellerName;
    }

    public void setSellerName(String sellerName) {
        this.sellerName = sellerName;
    }

    public boolean isGropuCb() {
        return gropuCb;
    }

    public void setGropuCb(boolean gropuCb) {
        this.gropuCb = gropuCb;
    }

    @Override
    public String toString() {
        return "GroupBean{" +
                "sellerName='" + sellerName + '\'' +
                ", gropuCb=" + gropuCb +
                '}';
    }
}

MessageBean

package com.bawie.www.gw_che.entity;

/**
 * 作者:戈鹏
 * on 2017/12/19 09:06
 */

public class MessageBean <T>{


    private String code;
    private T data;
    private String msg;
    private SellerBean seller;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public SellerBean getSeller() {
        return seller;
    }

    public void setSeller(SellerBean seller) {
        this.seller = seller;
    }



    public static class SellerBean {
        /**
         * description : 我是商家19
         * icon : http://120.27.23.105/images/icon.png
         * name : 商家19
         * productNums : 999
         * score : 5.0
         * sellerid : 19
         */

        private String description;
        private String icon;
        private String name;
        private int productNums;
        private double score;
        private int sellerid;

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public String getIcon() {
            return icon;
        }

        public void setIcon(String icon) {
            this.icon = icon;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getProductNums() {
            return productNums;
        }

        public void setProductNums(int productNums) {
            this.productNums = productNums;
        }

        public double getScore() {
            return score;
        }

        public void setScore(double score) {
            this.score = score;
        }

        public int getSellerid() {
            return sellerid;
        }

        public void setSellerid(int sellerid) {
            this.sellerid = sellerid;
        }
    }
}

adapter包下的ExpandableAdapter类

package com.bawie.www.gw_che.adapter;

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.TextView;
import android.widget.Toast;

import com.bawie.www.gw_che.R;
import com.bawie.www.gw_che.entity.ChildBean;
import com.bawie.www.gw_che.entity.GroupBean;
import com.bawie.www.gw_che.myview.AddDeleteView;
import com.bawie.www.gw_che.view.Main2Activity;
import com.facebook.drawee.view.SimpleDraweeView;

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

/**
 * 作者:戈鹏
 * on 2017/12/11 09:21
 */

public class ExpandableAdapter extends BaseExpandableListAdapter {

    // private static final String TAG = "ExpandableAdapter二级列表适配器";

    private Context context;
    private List<GroupBean> groupBeen=new ArrayList<>();
    private List<List<ChildBean>> childBeen=new ArrayList<>();

    private Main2Activity main;

    public ExpandableAdapter(Context context, List<GroupBean> groupBeen, List<List<ChildBean>> childBeen) {
        this.context = context;
        this.groupBeen = groupBeen;
        this.childBeen = childBeen;
        this.main = (Main2Activity) context;
    }

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

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

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

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

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

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

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

    //一级组
    @Override
    public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
        //加载视图
        view=View.inflate(context, R.layout.ex_group_item ,null);

        final CheckBox groupCb= (CheckBox) view.findViewById(R.id.group_checkbox);
        TextView shopName= (TextView) view.findViewById(R.id.shop_name);


        shopName.setText(groupBeen.get(i).getSellerName());
        groupCb.setChecked(groupBeen.get(i).isGropuCb());

        //组复选按钮
        groupCb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean gchecked = groupCb.isChecked();
                groupBeen.get(i).setGropuCb(gchecked);
                //Main2Activity main= (Main2Activity) context;
                for(GroupBean i: groupBeen){
                    boolean gropuCb = i.isGropuCb();
                    if(!gropuCb){
                        main.allCheckbox.setChecked(false);
                        break;
                    }else{
                        main.allCheckbox.setChecked(true);
                    }
                }
                int size = childBeen.get(i).size();
                if(gchecked){
                    for(int r=0;r<size;r++){
                        //Toast.makeText(context,"group按钮"+ gchecked+""+size, Toast.LENGTH_SHORT).show();
                        childBeen.get(i).get(r).setChildCb(true);
                    }
                }else{
                    for(int r=0;r<size;r++){
                        //Toast.makeText(context,"group按钮"+ gchecked+""+size, Toast.LENGTH_SHORT).show();
                        childBeen.get(i).get(r).setChildCb(false);
                    }
                }
                notifyDataSetChanged();
                main.changesum(childBeen);
            }
        });
        return view;
    }

    //二级组
    @Override
    public View getChildView(final int i, final int i1, boolean b, View v, ViewGroup viewGroup) {
        //加载视图
        v=View.inflate(context, R.layout.ex_child_item ,null);

        final CheckBox childCb = (CheckBox) v.findViewById(R.id.child_checkbox);
        TextView shopTitle= (TextView) v.findViewById(R.id.shop_title);
        TextView shopPrice= (TextView) v.findViewById(R.id.shop_price);
        //ImageView shopImg=v.findViewById(R.id.shop_img);
        //draweeView.setImageURI(uri);
        SimpleDraweeView shopImg = (SimpleDraweeView) v.findViewById(R.id.shop_img);
        final AddDeleteView adv = (AddDeleteView) v.findViewById(R.id.adv);
        Button shop_delete= (Button) v.findViewById(R.id.shop_delete);

        childCb.setChecked(childBeen.get(i).get(i1).isChildCb());
        String images = childBeen.get(i).get(i1).getImages();
        //Glide.with(context).load(images).into(shopImg);
        shopImg.setImageURI(images);
        shopTitle.setText(childBeen.get(i).get(i1).getTitle());
        shopPrice.setText(childBeen.get(i).get(i1).getPrice()+"");
        adv.setNumber(childBeen.get(i).get(i1).getNum());

        //final Main2Activity main= (Main2Activity) context;
        //控制删除按钮的显隐
        if(childBeen.get(i).get(i1).isBtn()){
            shop_delete.setVisibility(View.VISIBLE);
        }else{
            shop_delete.setVisibility(View.INVISIBLE);
        }
        //删除按钮监听
        shop_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int size = childBeen.get(i).size();
                if(size==1){
                    childBeen.remove(i);
                    groupBeen.remove(i);
                }else{
                    childBeen.get(i).remove(i1);
                }
                //点击删除后隐藏所有删除按钮
                for (List<ChildBean> i1:childBeen){
                    for(int r=0;r<i1.size();r++) {
                        i1.get(r).setBtn(false);
                    }
                }
                notifyDataSetChanged();
                main.changesum(childBeen);
            }
        });

        //加减器逻辑


        adv.setOnAddDelClickListener(new AddDeleteView.OnAddDelClickListener() {
            @Override
            public void onAddClick(View v) {
                int number = adv.getNumber();
                number++;
                adv.setNumber(number);
                childBeen.get(i).get(i1).setNum(number);
                main.changesum(childBeen);
            }

            @Override
            public void onDelClick(View v) {
                int number = adv.getNumber();
                if(number==1){
                    Toast.makeText(context,"用户最小数量为1",Toast.LENGTH_SHORT).show();
                }
                number--;
                adv.setNumber(number);
                childBeen.get(i).get(i1).setNum(number);
                main.changesum(childBeen);
            }
        });

        //二级组的复选框监听
        childCb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean flag=false;
                boolean cchecked = childCb.isChecked();
                childBeen.get(i).get(i1).setChildCb(cchecked);
                //Toast.makeText(context,"child按钮"+ cchecked+""+i1, Toast.LENGTH_SHORT).show();
               // Main2Activity main= (Main2Activity) context;
                for (List<ChildBean> i1:childBeen){
                    for(int r=0;r<i1.size();r++) {
                        boolean childCb1 = i1.get(r).isChildCb();
                        if(!childCb1){
                            main.allCheckbox.setChecked(false);
                            groupBeen.get(i).setGropuCb(false);
                            flag=true;
                            break;
                        }else{
                            main.allCheckbox.setChecked(true);
                            groupBeen.get(i).setGropuCb(true);
                        }
                    }
                    if(flag){
                        break;
                    }
                }

                int size = childBeen.get(i).size();
                for(int x=0;x<size;x++) {
                    boolean childCb1 = childBeen.get(i).get(x).isChildCb();
                    if(!childCb1){
                        groupBeen.get(i).setGropuCb(false);
                        break;
                    }else{
                        groupBeen.get(i).setGropuCb(true);
                    }
                }
                notifyDataSetChanged();
                main.changesum(childBeen);
            }
        });


        return v;
    }

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

最后不要忘了在项目清单里注册App

App类

package com.bawie.www.gw_che;

import android.app.Application;

import com.facebook.drawee.backends.pipeline.Fresco;

/**
 * 作者:戈鹏
 * on 2017/12/19 09:45
 */

public class App extends Application{
    @Override
    public void onCreate() {
        super.onCreate();
        Fresco.initialize(this);
    }
}

drawable下的edit

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF" />
    <!--<corners android:radius="3dip"/>-->
    <stroke
        android:width="1dip"
        android:color="#BDC7D8" />
</shape>

 

posted on 2017-12-21 08:41  权威的程序  阅读(428)  评论(0编辑  收藏  举报