MVP +自定义view加减 之 购物车 全

依赖

compile 'com.squareup.okhttp3:logging-interceptor:3.9.0'
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
// Retrofit库
compile 'com.squareup.retrofit2:retrofit:2.0.2'
// Okhttp库
compile 'com.squareup.okhttp3:okhttp:3.1.2'

// rxJava依赖包:
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
//《RXjava2的适配器》
compile 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'
//《Rxjava2》
compile 'io.reactivex.rxjava2:rxjava:2.1.7'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'org.greenrobot:eventbus:3.1.1'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.5.1'

 

RetrofitUntils

package com.example.xiaoma.model.https;

import com.example.xiaoma.model.untils.Constant;
import com.example.xiaoma.model.untils.RetrofitApi;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by nyj on 2018/5/29.
 */

public class RetrofitUntils {

    private final Retrofit retrofit;

    private RetrofitUntils() {
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(httpLoggingInterceptor)
                .build();
        retrofit = new Retrofit.Builder()
                .baseUrl(Constant.baseUrl)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }

    //单列
    private static RetrofitUntils INSTANCE;

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

    //方法供调用
    public RetrofitApi getApi() {

        return retrofit.create(RetrofitApi.class);
    }
}

 
 

Constant

package com.example.xiaoma.model.untils;

/**
 * Created by nyj on 2018/5/29.
 */

public interface Constant {
    String baseUrl = "https://www.zhaoapi.cn/";
}
 

MessageEvent

package com.example.xiaoma.model.untils;

/**
 * Created by nyj on 2018/5/29.
 */

public class MessageEvent {
    private boolean checked;

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }
}

 

PriceAndCountEvent

package com.example.xiaoma.model.untils;

/**
 * Created by nyj on 2018/5/29.
 */

public class PriceAndCountEvent {

    private int price;
    private int count;

    public int getPrice() {
        return price;
    }

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

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}

 

RetrofitApi

package com.example.xiaoma.model.untils;

import com.example.xiaoma.model.beans.ShopBeans;

import io.reactivex.Observable;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;

/**
 * Created by nyj on 2018/5/29.
 */

public interface RetrofitApi {
    @POST("product/getCarts")
    @FormUrlEncoded
    Observable<ShopBeans> getShop(@Field("uid") String uid);
}
 

BasePresenter

package com.example.xiaoma.presenter;

import com.example.xiaoma.view.interfaces.IBaseView;

/**
 * Created by nyj on 2018/5/29.
 */

public class BasePresenter<V extends IBaseView> {

    private V iBaseView;

    public void attachView(V iBaseView) {
        this.iBaseView = iBaseView;
    }

    public V getView() {

        return iBaseView;
    }

    public void daechView() {
        iBaseView = null;
    }
}

MainPresenter

package com.example.xiaoma.presenter;

import com.example.xiaoma.model.beans.ShopBeans;
import com.example.xiaoma.model.https.RetrofitUntils;
import com.example.xiaoma.view.interfaces.IMainView;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by nyj on 2018/5/29.
 */

public class MainPresenter extends BasePresenter<IMainView> {

    private final RetrofitUntils retrofitUntils;

    public MainPresenter() {
        retrofitUntils = RetrofitUntils.getInstance();
    }

    public void loadDataServer(String uid) {
        Observable<ShopBeans> observable = retrofitUntils.getApi().getShop(uid);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ShopBeans>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(ShopBeans shopBeans) {
                        getView().onSuccess(shopBeans);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}

BaseActivity

package com.example.xiaoma.view.activity;

import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;

import com.example.xiaoma.R;
import com.example.xiaoma.presenter.BasePresenter;
import com.example.xiaoma.view.interfaces.IBaseView;

public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity implements IBaseView{

    public P basePresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(setLayout());

        initView();
        initBaseView();
        initData();
    }

    protected abstract void initData();

    abstract P setPresenter();

    private void initBaseView() {
        basePresenter = setPresenter();
        if (basePresenter != null) {
            basePresenter.attachView(this);
        }
    }

    protected abstract void initView();

    protected abstract int setLayout();
}

MainActivity

package com.example.xiaoma.view.activity;

import android.os.Bundle;

import android.view.View;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;


import com.example.xiaoma.R;
import com.example.xiaoma.model.beans.ShopBeans;
import com.example.xiaoma.model.untils.MessageEvent;
import com.example.xiaoma.model.untils.PriceAndCountEvent;

import com.example.xiaoma.presenter.MainPresenter;
import com.example.xiaoma.view.adapter.MyAdapter;
import com.example.xiaoma.view.interfaces.IMainView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

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

public class MainActivity extends BaseActivity<MainPresenter> implements IMainView {

    List<List<ShopBeans.DataBean.ListBean>> childList = new ArrayList<>();
    List<ShopBeans.DataBean> groupList = new ArrayList<>();
    private CheckBox shopcart_check;
    private ExpandableListView shopcart_expandable;
    private TextView shopcart_jiesuan;
    private TextView shopcart_price;
    private SmartRefreshLayout shopcart_srl;
    private MyAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        EventBus.getDefault().register(this);
        //刷新
        shopcart_srl.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                refreshLayout.finishLoadMore(2000);
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                refreshLayout.finishRefresh(2000);
                initData();
            }
        });
        //全选监听
        shopcart_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.changeAllListCbState(shopcart_check.isChecked());
            }
        });
    }

    @Override
    protected void initData() {
        basePresenter.loadDataServer("13590");
    }

    @Override
    MainPresenter setPresenter() {
        return new MainPresenter();
    }

    @Override
    protected void initView() {
        shopcart_check = findViewById(R.id.shopcart_check);
        shopcart_expandable = findViewById(R.id.shopcart_expandable);
        shopcart_jiesuan = findViewById(R.id.shopcart_jiesuan);
        shopcart_price = findViewById(R.id.shopcart_price);
        shopcart_srl = findViewById(R.id.shopcart_srl);

    }

    @Override
    protected int setLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void onSuccess(ShopBeans shopBeans) {
        List<ShopBeans.DataBean> data = shopBeans.getData();
        groupList.addAll(data);
        for (int i = 0; i < data.size(); i++) {
            List<ShopBeans.DataBean.ListBean> list = data.get(i).getList();
            childList.add(list);
        }
        adapter = new MyAdapter(this, groupList, childList);
        shopcart_expandable.setAdapter(adapter);
        shopcart_expandable.setGroupIndicator(null);
        //默认让其全部展开
        for (int i = 0; i < groupList.size(); i++) {
            shopcart_expandable.expandGroup(i);
        }
    }

    public void onMessageEvent(MessageEvent event) {
        shopcart_check.setChecked(event.isChecked());
    }

    @Subscribe
    public void onMessageEvent(PriceAndCountEvent event) {
        shopcart_jiesuan.setText("结算(" + event.getCount() + ")");
        shopcart_price.setText(event.getPrice() + "");

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        basePresenter.daechView();
        EventBus.getDefault().unregister(this);
    }
}

 

MyAdapter

 
package com.example.viewjiajian.view.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.viewjiajian.MyView;
import com.example.viewjiajian.R;
import com.example.viewjiajian.model.beans.ShopBeans;
import com.example.viewjiajian.model.untils.MessageEvent;
import com.example.viewjiajian.model.untils.PriceAndCountEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by nyj on 2018/5/29.
 */

public class MyAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<ShopBeans.DataBean> groupList;
    private List<List<ShopBeans.DataBean.ListBean>> childList;

    public MyAdapter(Context context, List<ShopBeans.DataBean> groupList, List<List<ShopBeans.DataBean.ListBean>> childList) {
        this.context = context;
        this.groupList = groupList;
        this.childList = childList;
    }

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

    @Override
    public int getChildrenCount(int groupPosition) {
        return childList.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return childList.get(groupPosition).get(childPosition);
    }

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

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

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

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        final ViewHolderGroup holderGroup;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.cart_group_layout, null);
            holderGroup = new ViewHolderGroup();
            holderGroup.group_name = convertView.findViewById(R.id.group_name);
            holderGroup.group_check = convertView.findViewById(R.id.group_check);
            holderGroup.group_line = convertView.findViewById(R.id.group_line);
            convertView.setTag(holderGroup);
        } else {
            holderGroup = (ViewHolderGroup) convertView.getTag();
        }
        //赋值
        holderGroup.group_name.setText(groupList.get(groupPosition).getSellerName());

        /**
         *  一级CheckBox
         */

        final ShopBeans.DataBean dataBean = groupList.get(groupPosition);
        holderGroup.group_check.setChecked(dataBean.isChecked());

        holderGroup.group_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dataBean.setChecked(holderGroup.group_check.isChecked());
                changeChildCbState(groupPosition, holderGroup.group_check.isChecked());
                EventBus.getDefault().post(compute());
                changeAllCbState(isAllGroupCbSelected());
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final ViewHolderChild holderChild;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.cart_child_layout, null);
            holderChild = new ViewHolderChild();
            holderChild.child_img = convertView.findViewById(R.id.child_img);
            holderChild.child_content = convertView.findViewById(R.id.child_content);
            holderChild.child_price = convertView.findViewById(R.id.child_price);
            holderChild.child_del = convertView.findViewById(R.id.child_delete);
            holderChild.child_check = convertView.findViewById(R.id.child_chek);
            holderChild.myview = convertView.findViewById(R.id.myview);
            convertView.setTag(holderChild);
        } else {
            holderChild = (ViewHolderChild) convertView.getTag();
        }
        //赋值
        holderChild.child_content.setText(childList.get(groupPosition).get(childPosition).getTitle());
        String[] temp = null;
        temp = (childList.get(groupPosition).get(childPosition).getImages()).split("\\|");
        Glide.with(context).load(temp[0]).into(holderChild.child_img);
        /**
         *
         */
        final ShopBeans.DataBean.ListBean listBean = childList.get(groupPosition).get(childPosition);
        holderChild.child_check.setChecked(listBean.isChecked());
        holderChild.child_price.setText(listBean.getPrice() + "");
        holderChild.myview.setEditText(listBean.getNum());

        //加减监听
        holderChild.myview.setCustomListener(new MyView.CustomListener() {
            @Override
            public void jiajian(int count) {
                childList.get(groupPosition).get(childPosition).setNum(count);
                EventBus.getDefault().post(compute());
                notifyDataSetChanged();
            }
        });

        //二级checkbox
        holderChild.child_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //设置该条目对象里的checked属性值
                listBean.setChecked(holderChild.child_check.isChecked());
                PriceAndCountEvent priceAndCountEvent = compute();
                EventBus.getDefault().post(priceAndCountEvent);
                if (holderChild.child_check.isChecked()) {
                    //当前checkbox是选中状态
                    if (isAllChildCbSelected(groupPosition)) {
                        changGroupCbState(groupPosition, true);
                        changeAllCbState(isAllGroupCbSelected());
                    }
                } else {
                    changGroupCbState(groupPosition, false);
                    changeAllCbState(isAllGroupCbSelected());
                }
                notifyDataSetChanged();
            }

        });
        //删除
        holderChild.child_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //对话框
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("操作提示");
                builder.setMessage("您确定要将这些商品从购物车中移除吗?");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        List<ShopBeans.DataBean.ListBean> datasBeen = childList.get(groupPosition);
                        ShopBeans.DataBean.ListBean remove = datasBeen.remove(childPosition);
                        if (datasBeen.size() == 0) {
                            childList.remove(groupPosition);

                            groupList.remove(groupPosition);

                        }
                        /* SharedPreferences sharedPreferences = context.getSharedPreferences("login", Context.MODE_PRIVATE);
                        int uid = sharedPreferences.getInt("uid", 1);
                        String token = sharedPreferences.getString("token", null);
                        int pid = childList.get(groupPosition).get(childPosition).getPid();
                       HttpsDelete instance = HttpsDelete.getInstance();
                        String path = "https://www.zhaoapi.cn/product/deleteCart?source=android";
                        HashMap<String, String> map = new HashMap<>();
                        map.put("token", token);
                        map.put("uid", uid + "");
                        map.put("pid", pid + "");
                        instance.doPost(path, map, new HttpCallback() {
                            @Override
                            public void onSuccess(String s) {
                                Log.e("dele", s);
                            }

                            @Override
                            public void onFail(int errCode, String errMsg) {

                            }
                        }); */
                        EventBus.getDefault().post(compute());
                        notifyDataSetChanged();
                    }
                });
                // 设置取消(消极)按钮
                builder.setNegativeButton("取消", null);
                // 3.显示对话框
                builder.show();
            }
        });
        return convertView;
    }


    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    //优化
    class ViewHolderGroup {
        private TextView group_name;
        private CheckBox group_check;
        LinearLayout group_line;
    }

    class ViewHolderChild {
        private ImageView child_img;
        private TextView child_content;
        private TextView child_price;
        private MyView myview;
        private TextView child_del;
        private CheckBox child_check;
    }

    /**
     * 判断一级列表是否全部选中
     *
     * @return
     */
    private boolean isAllGroupCbSelected() {
        for (int i = 0; i < groupList.size(); i++) {
            ShopBeans.DataBean dataBean = groupList.get(i);
            if (!dataBean.isChecked()) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断二级列表是否全部选中
     *
     * @param groupPosition
     * @return
     */

    private boolean isAllChildCbSelected(int groupPosition) {
        List<ShopBeans.DataBean.ListBean> datasBeen = childList.get(groupPosition);
        for (int i = 0; i < datasBeen.size(); i++) {
            ShopBeans.DataBean.ListBean datasBean = datasBeen.get(i);
            if (!datasBean.isChecked()) {
                return false;
            }
        }
        return true;
    }

    /**
     * 设置全选、反选
     *
     * @param flag
     */
    public void changeAllListCbState(boolean flag) {
        for (int i = 0; i < groupList.size(); i++) {
            changGroupCbState(i, flag);
            changeChildCbState(i, flag);
        }
        EventBus.getDefault().post(compute());
        notifyDataSetChanged();
    }

    /**
     * 改变一级列表checkbox状态
     *
     * @param groupPosition
     */
    private void changGroupCbState(int groupPosition, boolean flag) {
        ShopBeans.DataBean dataBean = groupList.get(groupPosition);
        dataBean.setChecked(flag);
    }

    /**
     * 改变二级列表checkbox状
     *
     * @param groupPosition
     * @param flag
     */

    private void changeChildCbState(int groupPosition, boolean flag) {
        List<ShopBeans.DataBean.ListBean> datasBeen = childList.get(groupPosition);
        for (int i = 0; i < datasBeen.size(); i++) {
            ShopBeans.DataBean.ListBean datasBean = datasBeen.get(i);
            datasBean.setChecked(flag);
        }
    }

    /**
     * 改变全选的状态
     *
     * @param flag
     */

    private void changeAllCbState(boolean flag) {
        MessageEvent messageEvent = new MessageEvent();
        messageEvent.setChecked(flag);
        EventBus.getDefault().post(messageEvent);
    }

    /**
     * 计算列表中,选中的钱和数量
     */
    private PriceAndCountEvent compute() {
        int count = 0;
        int price = 0;
        for (int i = 0; i < childList.size(); i++) {
            List<ShopBeans.DataBean.ListBean> datasBeen = childList.get(i);
            for (int j = 0; j < datasBeen.size(); j++) {
                ShopBeans.DataBean.ListBean datasBean = datasBeen.get(j);
                if (datasBean.isChecked()) {
                    price += datasBean.getNum() * datasBean.getPrice();
                    count += datasBean.getNum();
                }
            }
        }
        PriceAndCountEvent priceAndCountEvent = new PriceAndCountEvent();
        priceAndCountEvent.setCount(count);
        priceAndCountEvent.setPrice(price);
        return priceAndCountEvent;
    }

}


MyView

package com.example.xiaoma;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

/**
 * Created by nyj on 2018/5/29.
 */

public class MyView extends RelativeLayout {
    private Button reverse;
    private Button add;
    private EditText countEdit;
    private CustomListener customListener;

    public MyView(Context context) {
        super(context);
        initView(context);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }


    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public void initView(Context context) {
        View view = View.inflate(context, R.layout.changeview, null);

        reverse = (Button) view.findViewById(R.id.child_reverse);

        add = (Button) view.findViewById(R.id.child_add);

        countEdit = (EditText) view.findViewById(R.id.child_count);
        //减的时候回调
        reverse.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int count = Integer.valueOf(countEdit.getText().toString());
                if (count > 1) {
                    count--;
                    countEdit.setText(count + "");
                    if (customListener != null) {
                        customListener.jiajian(count);
                    }
                }
            }
        });
        //加的时候回调
        add.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int count = Integer.valueOf(countEdit.getText().toString());
                count++;
                countEdit.setText(count + "");
                if (customListener != null) {
                    customListener.jiajian(count);
                }
            }
        });

        this.addView(view);
    }

    //设置接口
    public interface CustomListener {
        void jiajian(int count);
    }

    //设置回调方法
    public void setCustomListener(CustomListener customListener) {
        this.customListener = customListener;
    }

    public void setEditText(int num) {
        if (countEdit != null) {
            countEdit.setText(num + "");
        }
    }
}

activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<com.scwang.smartrefresh.layout.SmartRefreshLayout android:id="@+id/shopcart_srl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.5"
            android:gravity="center"
            android:text="购物车"
            android:textColor="#ff00"
            android:textSize="20dp" />

        <ExpandableListView
            android:id="@+id/shopcart_expandable"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="9"></ExpandableListView>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_alignParentBottom="true"
            android:background="@android:color/white"
            android:gravity="center_vertical">

            <CheckBox
                android:id="@+id/shopcart_check"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="10dp"
                android:focusable="false" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                android:layout_centerVertical="true"
                android:layout_marginLeft="10dp"
                android:layout_toRightOf="@+id/shopcart_check"
                android:gravity="center_vertical"
                android:text="全选"
                android:textSize="20sp" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:text="合计 :" />

                <TextView
                    android:id="@+id/shopcart_price"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:layout_marginLeft="10dp"
                    android:paddingRight="10dp"
                    android:text="0"
                    android:textColor="@android:color/holo_red_light" />

                <TextView
                    android:id="@+id/shopcart_jiesuan"
                    android:layout_width="wrap_content"
                    android:layout_height="50dp"
                    android:background="@android:color/holo_red_dark"
                    android:gravity="center"
                    android:padding="10dp"
                    android:text="结算(0)"
                    android:textColor="@android:color/white" />
            </LinearLayout>
        </RelativeLayout>

    </LinearLayout>

</com.scwang.smartrefresh.layout.SmartRefreshLayout>
    </LinearLayout>

cart_child_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <CheckBox
        android:id="@+id/child_chek"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="20dp" />

    <ImageView
        android:id="@+id/child_img"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginLeft="5dp"
        android:src="@mipmap/ic_launcher_round" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/child_content"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:text="content" />

            <TextView
                android:id="@+id/child_price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="price"
                android:textColor="#ff00" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <com.example.xiaoma.MyView
                android:id="@+id/myview"
                android:layout_width="wrap_content"
                android:layout_height="36dp"
                android:layout_centerInParent="true"
                android:layout_gravity="right"
                android:layout_marginRight="15dp"
                app:btnTextSize="14sp"
                app:btnWidth="36dp"
                app:tvWidth="50dp" />
        </LinearLayout>

        <TextView
            android:id="@+id/child_delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="25dp"
            android:text="删除"
            android:textSize="18dp" />
    </LinearLayout>
</LinearLayout>

cart_group_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/group_line"
    android:layout_height="wrap_content"
    android:background="#ccc">
    <CheckBox
        android:id="@+id/group_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/group_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:text="name"
        android:textColor="@color/colorAccent"
        android:textSize="20dp" />
</LinearLayout>

changeview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_amount_layout"
    android:divider="@drawable/divider"
    android:focusable="true"
    android:orientation="horizontal"
    android:showDividers="middle">

    <Button
        android:id="@+id/child_reverse"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:background="@drawable/btn_amount"
        android:gravity="center"
        android:text="-" />

    <EditText
        android:id="@+id/child_count"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:background="@null"
        android:gravity="center"
        android:inputType="number"
        android:minWidth="60dp"
        android:text="1" />

    <Button
        android:id="@+id/child_add"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:background="@drawable/btn_amount"
        android:gravity="center"
        android:text="+" />
</LinearLayout>
ShopBeans
 
package com.example.viewjiajian.model.beans;

import java.util.List;

/**
 * Created by nyj on 2018/5/29.
 */

public class ShopBeans {


    /**
     * msg : 请求成功
     * code : 0
     * data : [{"list":[{"bargainPrice":99,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/4345173.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6037/35/2944615848/95178/6cd6cff0/594a3a10Na4ec7f39.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6607/258/1025744923/75738/da120a2d/594a3a12Ne3e6bc56.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6370/292/1057025420/64655/f87644e3/594a3a12N5b900606.jpg!q70.jpg","num":1,"pid":45,"price":2999,"pscid":39,"selected":0,"sellerid":1,"subhead":"高清双摄,就是清晰!2000+1600万高清摄像头,6GB大内存+高通骁龙835处理器,性能怪兽!","title":"一加手机5 (A5000) 6GB+64GB 月岩灰 全网通 双卡双待 移动联通电信4G手机"}],"sellerName":"商家1","sellerid":"1"},{"list":[{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/5025518.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8830/106/1760940277/195595/5cf9412f/59bf2ef5N5ab7dc16.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5428/70/1520969931/274676/b644dd0d/591128e7Nd2f70da0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5566/365/1519564203/36911/620c750c/591128eaN54ac3363.jpg!q70.jpg","num":1,"pid":58,"price":6399,"pscid":40,"selected":0,"sellerid":2,"subhead":"升级4G大显存!Nvme协议Pcie SSD,速度快人一步】GTX1050Ti就选拯救者!专业游戏键盘&新模具全新设计!","title":"联想(Lenovo)拯救者R720 15.6英寸游戏笔记本电脑(i5-7300HQ 8G 1T+128G SSD GTX1050Ti 4G IPS 黑)"}],"sellerName":"商家2","sellerid":"2"},{"list":[{"bargainPrice":11800,"createtime":"2017-10-03T23:53:28","detailUrl":"https://mitem.jd.hk/ware/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1","images":"https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5815/178/2614671118/51656/7f52d137/593276c7N107b725a.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5878/60/2557817477/30873/4502b606/593276caN5a7d6357.jpg!q70.jpg","num":1,"pid":62,"price":15999,"pscid":40,"selected":0,"sellerid":6,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"},{"bargainPrice":22.9,"createtime":"2017-10-14T21:48:08","detailUrl":"https://item.m.jd.com/product/2542855.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t1930/284/2865629620/390243/e3ade9c4/56f0a08fNbd3a1235.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2137/336/2802996626/155915/e5e90d7a/56f0a09cN33e01bd0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t1882/31/2772215910/389956/c8dbf370/56f0a0a2Na0c86ea6.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2620/166/2703833710/312660/531aa913/57709035N33857877.jpg!q70.jpg","num":1,"pid":29,"price":588,"pscid":2,"selected":0,"sellerid":6,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋"}],"sellerName":"商家6","sellerid":"6"},{"list":[{"bargainPrice":11800,"createtime":"2017-10-14T21:38:26","detailUrl":"https://mitem.jd.hk/ware/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1","images":"https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5815/178/2614671118/51656/7f52d137/593276c7N107b725a.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5878/60/2557817477/30873/4502b606/593276caN5a7d6357.jpg!q70.jpg","num":1,"pid":63,"price":10000,"pscid":40,"selected":0,"sellerid":7,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家7","sellerid":"7"},{"list":[{"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":7,"pid":1,"price":118,"pscid":1,"selected":0,"sellerid":17,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家17","sellerid":"17"},{"list":[{"bargainPrice":111.99,"createtime":"2017-10-03T23:53:28","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":1,"pid":3,"price":198,"pscid":1,"selected":0,"sellerid":19,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家19","sellerid":"19"}]
     * sellerName :
     * sellerid : 0
     */
    private boolean checked;
    private String msg;
    private String code;
    private String sellerName;
    private String sellerid;
    private List<DataBean> data;

    public ShopBeans(boolean checked) {
        this.checked = checked;
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

    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 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<DataBean> getData() {
        return data;
    }

    public void setData(List<DataBean> data) {
        this.data = data;
    }

    public static class DataBean {
        /**
         * list : [{"bargainPrice":99,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/4345173.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t6037/35/2944615848/95178/6cd6cff0/594a3a10Na4ec7f39.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6607/258/1025744923/75738/da120a2d/594a3a12Ne3e6bc56.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6370/292/1057025420/64655/f87644e3/594a3a12N5b900606.jpg!q70.jpg","num":1,"pid":45,"price":2999,"pscid":39,"selected":0,"sellerid":1,"subhead":"高清双摄,就是清晰!2000+1600万高清摄像头,6GB大内存+高通骁龙835处理器,性能怪兽!","title":"一加手机5 (A5000) 6GB+64GB 月岩灰 全网通 双卡双待 移动联通电信4G手机"}]
         * sellerName : 商家1
         * sellerid : 1
         */
        private boolean checked;
        private String sellerName;
        private String sellerid;
        private List<ListBean> list;

        public DataBean(boolean checked) {
            this.checked = checked;
        }

        public boolean isChecked() {
            return checked;
        }

        public void setChecked(boolean checked) {
            this.checked = checked;
        }

        public String getSellerName() {
            return sellerName;
        }

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

        public String getSellerid() {
            return sellerid;
        }

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

        public List<ListBean> getList() {
            return list;
        }

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

        public static class ListBean {
            /**
             * bargainPrice : 99.0
             * createtime : 2017-10-14T21:38:26
             * detailUrl : https://item.m.jd.com/product/4345173.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends
             * images : https://m.360buyimg.com/n0/jfs/t6037/35/2944615848/95178/6cd6cff0/594a3a10Na4ec7f39.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6607/258/1025744923/75738/da120a2d/594a3a12Ne3e6bc56.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t6370/292/1057025420/64655/f87644e3/594a3a12N5b900606.jpg!q70.jpg
             * num : 1
             * pid : 45
             * price : 2999.0
             * pscid : 39
             * selected : 0
             * sellerid : 1
             * subhead : 高清双摄,就是清晰!2000+1600万高清摄像头,6GB大内存+高通骁龙835处理器,性能怪兽!
             * title : 一加手机5 (A5000) 6GB+64GB 月岩灰 全网通 双卡双待 移动联通电信4G手机
             */
            private boolean checked;
            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 ListBean(boolean checked) {
                this.checked = checked;
            }

            public boolean isChecked() {
                return checked;
            }

            public void setChecked(boolean checked) {
                this.checked = checked;
            }

            //自己添加的三个字段
            private int isFirst = 1;//1为显示商铺, 2为隐藏商铺
            private boolean item_check;//每个商品的选中状态
            private boolean shop_check;//商店的选中状态

            public int getIsFirst() {
                return isFirst;
            }

            public void setIsFirst(int isFirst) {
                this.isFirst = isFirst;
            }

            public boolean isItem_check() {
                return item_check;
            }

            public void setItem_check(boolean item_check) {
                this.item_check = item_check;
            }

            public boolean isShop_check() {
                return shop_check;
            }

            public void setShop_check(boolean shop_check) {
                this.shop_check = shop_check;
            }


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


        }
    }
}

bg_amount_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF" />
    <stroke
        android:width="1dp"
        android:color="@color/divider" />
    <padding
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp" />
</shape>

btn_amount.xml

<?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/divider" />
    <item android:state_enabled="false" android:drawable="@color/divider" />
    <item android:drawable="@android:color/white" />
</selector>

divider.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size
        android:width="0.5dp"/>
    <solid android:color="@color/divider"/>
</shape>

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="AmountView">
        <!-- 左右2边+-按钮的宽度 -->
        <attr name="btnWidth" format="dimension" />
        <!-- 中间TextView的宽度 -->
        <attr name="tvWidth" format="dimension" />
        <!--<attr name="tvColor" format="color"/>-->
        <attr name="tvTextSize" format="dimension"/>
        <attr name="btnTextSize" format="dimension"/>
    </declare-styleable>
</resources>













  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值