MVP模式网络请求购物车

大体的布局

(二级列表)

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     xmlns:tools="http://schemas.android.com/tools"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     android:orientation="vertical"
 8     tools:context="mvpframework.bwie.com.a1509agoodcart.view.MainActivity">
 9
10     <TextView
11         android:layout_width="match_parent"
12         android:layout_height="40dp"
13         android:background="#990000ff"
14         android:gravity="center"
15         android:text="购物车"
16         android:textColor="#ff3660"
17         android:textSize="25sp" />
18
19     <ExpandableListView
20         android:id="@+id/elv"
21         android:layout_width="match_parent"
22         android:layout_height="match_parent"
23         android:layout_weight="1" />
24
25     <RelativeLayout
26         android:layout_width="match_parent"
27         android:layout_height="50dp"
28         android:layout_alignParentBottom="true"
29         android:background="@android:color/white"
30         android:gravity="center_vertical">
31
32         <CheckBox
33             android:id="@+id/checkbox2"
34             android:layout_width="wrap_content"
35             android:layout_height="wrap_content"
36             android:layout_centerVertical="true"
37             android:layout_marginLeft="10dp"
38             android:focusable="false" />
39
40         <TextView
41             android:layout_width="wrap_content"
42             android:layout_height="50dp"
43             android:layout_centerVertical="true"
44             android:layout_marginLeft="10dp"
45             android:layout_toRightOf="@+id/checkbox2"
46             android:gravity="center_vertical"
47             android:text="全选"
48             android:textSize="20sp" />
49
50         <LinearLayout
51             android:layout_width="wrap_content"
52             android:layout_height="wrap_content"
53             android:layout_alignParentRight="true"
54             android:orientation="horizontal">
55
56             <TextView
57                 android:layout_width="wrap_content"
58                 android:layout_height="wrap_content"
59                 android:layout_marginLeft="10dp"
60                 android:text="合计 :" />
61
62
63             <TextView
64                 android:id="@+id/tv_price"
65                 android:layout_width="wrap_content"
66                 android:layout_height="50dp"
67                 android:layout_marginLeft="10dp"
68                 android:paddingRight="10dp"
69                 android:text="0"
70                 android:textColor="@android:color/holo_red_light" />
71
72
73             <TextView
74                 android:id="@+id/tv_num"
75                 android:layout_width="wrap_content"
76                 android:layout_height="50dp"
77                 android:background="@android:color/holo_red_dark"
78                 android:gravity="center"
79                 android:padding="10dp"
80                 android:text="结算(0)"
81                 android:textColor="@android:color/white" />
82         </LinearLayout>
83     </RelativeLayout>
84
85
86 </LinearLayout>
<?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="100dp"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <CheckBox
        android:id="@+id/cb_parent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="30dp"
        android:focusable="false" />

    <TextView
        android:id="@+id/tv_sign"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:text="标记" />

    <TextView
        android:id="@+id/tv_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:text="12345678" />

</LinearLayout>

  

<?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="@android:color/darker_gray"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <CheckBox

        android:id="@+id/cb_child"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="30dp"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="30dp"
        android:focusable="false" />

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

        <TextView
            android:id="@+id/tv_tel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="iphone6" />

        <TextView
            android:id="@+id/tv_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="什么手机" />

        <TextView
            android:id="@+id/tv_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="2016-12-10" />
    </LinearLayout>

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

        <TextView
            android:id="@+id/tv_pri"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:text="¥3000.00" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical">

            <ImageView
                android:id="@+id/iv_del"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:src="@drawable/shopcart_minus_grey" />

            <TextView
                android:id="@+id/tv_num"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:background="@drawable/shopcart_add_btn"
                android:paddingBottom="2dp"
                android:paddingLeft="20dp"
                android:paddingRight="20dp"
                android:paddingTop="2dp"
                android:text="1" />

            <ImageView
                android:id="@+id/iv_add"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_marginLeft="5dp"
                android:src="@drawable/shopcart_add_red" />

        </LinearLayout>

    </LinearLayout>

    <TextView
        android:id="@+id/tv_del"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="删除" />
</LinearLayout>

  接下来是适配器类

public class MyAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<GoosBean.DataBean> groupList;
    private List<List<GoosBean.DataBean.DatasBean>> childList;
    private final LayoutInflater inflater;

    public MyAdapter(Context context, List<GoosBean.DataBean> groupList, List<List<GoosBean.DataBean.DatasBean>> childList) {
        this.context = context;
        this.groupList = groupList;
        this.childList = childList;
        inflater = LayoutInflater.from(context);
    }

    @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) {
        View view;
        final GroupViewHolder holder;
        if (convertView == null) {
            holder = new GroupViewHolder();
            view = inflater.inflate(R.layout.item_parent_market, null);
            holder.cbGroup = view.findViewById(R.id.cb_parent);
            holder.tv_number = view.findViewById(R.id.tv_number);
            view.setTag(holder);
        } else {
            view = convertView;
            holder = (GroupViewHolder) view.getTag();
        }
        final GoosBean.DataBean dataBean = groupList.get(groupPosition);
        holder.cbGroup.setChecked(dataBean.isChecked());
        holder.tv_number.setText(dataBean.getTitle());
        //一级checkbox
        holder.cbGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dataBean.setChecked(holder.cbGroup.isChecked());
                changeChildCbState(groupPosition, holder.cbGroup.isChecked());
                EventBus.getDefault().post(compute());
                changeAllCbState(isAllGroupCbSelected());
                notifyDataSetChanged();
            }
        });

        return view;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View view;
        final ChildViewHolder holder;
        if (convertView == null) {
            holder = new ChildViewHolder();
            view = inflater.inflate(R.layout.item_child_market, null);
            holder.cbChild = view.findViewById(R.id.cb_child);
            holder.tv_tel = view.findViewById(R.id.tv_tel);
            holder.tv_content = view.findViewById(R.id.tv_content);
            holder.tv_time = view.findViewById(R.id.tv_time);
            holder.tv_price = view.findViewById(R.id.tv_pri);
            holder.tv_del = view.findViewById(R.id.tv_del);
            holder.iv_add = view.findViewById(R.id.iv_add);
            holder.iv_del = view.findViewById(R.id.iv_del);
            holder.tv_num = view.findViewById(R.id.tv_num);
            view.setTag(holder);
        } else {
            view = convertView;
            holder = (ChildViewHolder) view.getTag();
        }
        final GoosBean.DataBean.DatasBean datasBean = childList.get(groupPosition).get(childPosition);
        holder.cbChild.setChecked(datasBean.isChecked());
        holder.tv_tel.setText(datasBean.getType_name());
        holder.tv_content.setText(datasBean.getMsg());
        holder.tv_time.setText(datasBean.getAdd_time());
        holder.tv_price.setText(datasBean.getPrice() + "");
        holder.tv_num.setText(datasBean.getNum() + "");

        //二级checkbox
        holder.cbChild.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //设置该条目对象里的checked属性值
                datasBean.setChecked(holder.cbChild.isChecked());
                PriceAndCountEvent priceAndCountEvent = compute();
                EventBus.getDefault().post(priceAndCountEvent);

                if (holder.cbChild.isChecked()) {
                    //当前checkbox是选中状态
                    if (isAllChildCbSelected(groupPosition)) {
                        changGroupCbState(groupPosition, true);
                        changeAllCbState(isAllGroupCbSelected());
                    }
                } else {
                    changGroupCbState(groupPosition, false);
                    changeAllCbState(isAllGroupCbSelected());
                }
                notifyDataSetChanged();
            }
        });
        //加号
        holder.iv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = datasBean.getNum();
                holder.tv_num.setText(++num + "");
                datasBean.setNum(num);
                if (holder.cbChild.isChecked()) {
                    PriceAndCountEvent priceAndCountEvent = compute();
                    EventBus.getDefault().post(priceAndCountEvent);
                }
            }
        });
        //减号
        holder.iv_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = datasBean.getNum();
                if (num == 1) {
                    return;
                }
                holder.tv_num.setText(--num + "");
                datasBean.setNum(num);
                if (holder.cbChild.isChecked()) {
                    PriceAndCountEvent priceAndCountEvent = compute();
                    EventBus.getDefault().post(priceAndCountEvent);
                }
            }
        });
        //删除
        holder.tv_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<GoosBean.DataBean.DatasBean> datasBeen = childList.get(groupPosition);
                GoosBean.DataBean.DatasBean remove = datasBeen.remove(childPosition);
                if (datasBeen.size() == 0) {
                    childList.remove(groupPosition);
                    groupList.remove(groupPosition);
                }
                EventBus.getDefault().post(compute());
                notifyDataSetChanged();
            }
        });
        return view;
    }

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

    class GroupViewHolder {
        CheckBox cbGroup;
        TextView tv_number;
    }

    class ChildViewHolder {
        CheckBox cbChild;
        TextView tv_tel;
        TextView tv_content;
        TextView tv_time;
        TextView tv_price;
        TextView tv_del;
        ImageView iv_del;
        ImageView iv_add;
        TextView tv_num;
    }

    /**
     * 改变全选的状态
     *
     * @param flag
     */
    private void changeAllCbState(boolean flag) {
        MessageEvent messageEvent = new MessageEvent();
        messageEvent.setChecked(flag);
        EventBus.getDefault().post(messageEvent);
    }

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

    /**
     * 改变二级列表checkbox状态
     *
     * @param groupPosition
     * @param flag
     */
    private void changeChildCbState(int groupPosition, boolean flag) {
        List<GoosBean.DataBean.DatasBean> datasBeen = childList.get(groupPosition);
        for (int i = 0; i < datasBeen.size(); i++) {
            GoosBean.DataBean.DatasBean datasBean = datasBeen.get(i);
            datasBean.setChecked(flag);
        }
    }

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

    /**
     * 判断二级列表是否全部选中
     *
     * @param groupPosition
     * @return
     */
    private boolean isAllChildCbSelected(int groupPosition) {
        List<GoosBean.DataBean.DatasBean> datasBeen = childList.get(groupPosition);
        for (int i = 0; i < datasBeen.size(); i++) {
            GoosBean.DataBean.DatasBean datasBean = datasBeen.get(i);
            if (!datasBean.isChecked()) {
                return false;
            }
        }
        return true;
    }

    /**
     * 计算列表中,选中的钱和数量
     */
    private PriceAndCountEvent compute() {
        int count = 0;
        int price = 0;
        for (int i = 0; i < childList.size(); i++) {
            List<GoosBean.DataBean.DatasBean> datasBeen = childList.get(i);
            for (int j = 0; j < datasBeen.size(); j++) {
                GoosBean.DataBean.DatasBean 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;
    }

    /**
     * 设置全选、反选
     *
     * @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();
    }

}

  Bean类

public class GoosBean {

    /**
     * code : 200
     * data : [{"datas":[{"add_time":"2016-12-10 14:54:58","cart_id":"445162","house_id":"1","msg":"购买渠道:大陆国行","price":500,"type_name":"苹果 iPhone 6(白金色)","type_sn_id":"ggh"},{"add_time":"2016-12-10 14:55:18","cart_id":"445163","house_id":"1","msg":"购买渠道:水货无锁","price":1000,"type_name":"苹果 iPhone 7 (亮黑色)","type_sn_id":"tgg"}],"title":"苹果","title_id":"59280"},{"datas":[{"add_time":"2016-12-10 14:54:58","cart_id":"445162","house_id":"1","msg":"边框背板:全新未使用","price":50,"type_name":"小米4s (白金色)","type_sn_id":"ggh"},{"add_time":"2016-12-10 14:55:18","cart_id":"445163","house_id":"1","msg":"屏幕性能:色差/亮点/轻微发黄","price":100,"type_name":"小米5s (亮黑色)","type_sn_id":"tgg"}],"title":"小米","title_id":"59279"},{"datas":[{"add_time":"2016-12-10 14:54:58","cart_id":"445162","house_id":"1","msg":"边框背板:全新未使用","price":50,"type_name":"三星 (白金色)","type_sn_id":"ggh"},{"add_time":"2016-12-10 14:55:18","cart_id":"445163","house_id":"1","msg":"屏幕性能:色差/亮点/轻微发黄","price":100,"type_name":"三星 (亮黑色)","type_sn_id":"tgg"}],"title":"三星","title_id":"59279"},{"datas":[{"add_time":"2016-12-10 14:54:58","cart_id":"445162","house_id":"1","msg":"边框背板:全新未使用","price":50,"type_name":"华为 (白金色)","type_sn_id":"ggh"},{"add_time":"2016-12-10 14:55:18","cart_id":"445163","house_id":"1","msg":"屏幕性能:色差/亮点/轻微发黄","price":100,"type_name":"华为 (亮黑色)","type_sn_id":"tgg"},{"add_time":"2016-12-10 4:55:28","cart_id":"445164","house_id":"1","msg":"屏幕性能:色差/亮点/轻微发黄","price":150,"type_name":"华为 (纯黑色)","type_sn_id":"hgg"}],"title":"华为","title_id":"59279"}]
     * flag : Success
     * msg : 描述
     */

    private String code;
    private String flag;
    private String msg;
    private List<DataBean> data;

    public String getCode() {
        return code;
    }

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

    public String getFlag() {
        return flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }

    public String getMsg() {
        return msg;
    }

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

    public List<DataBean> getData() {
        return data;
    }

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

    public static class DataBean {
        /**
         * datas : [{"add_time":"2016-12-10 14:54:58","cart_id":"445162","house_id":"1","msg":"购买渠道:大陆国行","price":500,"type_name":"苹果 iPhone 6(白金色)","type_sn_id":"ggh"},{"add_time":"2016-12-10 14:55:18","cart_id":"445163","house_id":"1","msg":"购买渠道:水货无锁","price":1000,"type_name":"苹果 iPhone 7 (亮黑色)","type_sn_id":"tgg"}]
         * title : 苹果
         * title_id : 59280
         */
        private boolean checked;
        private String title;
        private String title_id;
        private List<DatasBean> datas;

        public boolean isChecked() {
            return checked;
        }

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

        public String getTitle() {
            return title;
        }

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

        public String getTitle_id() {
            return title_id;
        }

        public void setTitle_id(String title_id) {
            this.title_id = title_id;
        }

        public List<DatasBean> getDatas() {
            return datas;
        }

        public void setDatas(List<DatasBean> datas) {
            this.datas = datas;
        }

        public static class DatasBean {
            /**
             * add_time : 2016-12-10 14:54:58
             * cart_id : 445162
             * house_id : 1
             * msg : 购买渠道:大陆国行
             * price : 500
             * type_name : 苹果 iPhone 6(白金色)
             * type_sn_id : ggh
             */
            private boolean checked;
            private int num = 1;
            private String add_time;
            private String cart_id;
            private String house_id;
            private String msg;
            private int price;
            private String type_name;
            private String type_sn_id;

            public int getNum() {
                return num;
            }

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

            public boolean isChecked() {
                return checked;
            }

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

            public String getAdd_time() {
                return add_time;
            }

            public void setAdd_time(String add_time) {
                this.add_time = add_time;
            }

            public String getCart_id() {
                return cart_id;
            }

            public void setCart_id(String cart_id) {
                this.cart_id = cart_id;
            }

            public String getHouse_id() {
                return house_id;
            }

            public void setHouse_id(String house_id) {
                this.house_id = house_id;
            }

            public String getMsg() {
                return msg;
            }

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

            public int getPrice() {
                return price;
            }

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

            public String getType_name() {
                return type_name;
            }

            public void setType_name(String type_name) {
                this.type_name = type_name;
            }

            public String getType_sn_id() {
                return type_sn_id;
            }

            public void setType_sn_id(String type_sn_id) {
                this.type_sn_id = type_sn_id;
            }
        }
    }
}

  

Model层

public interface IMainModel {    public void getGoods(OnNetListener<GoosBean> onNetListener);}
调用接口请求数据public class MainModel implements IMainModel {    private Handler handler = new Handler(Looper.getMainLooper());

public void getGoods(final OnNetListener<GoosBean> onNetListener) {        HttpUtils.getHttpUtils().doGet(Api.url, new Callback() {            @Override            public void onFailure(Call call, IOException e) {

}

@Override            public void onResponse(Call call, Response response) throws IOException {                String string = response.body().string();                final GoosBean goosBean = new Gson().fromJson(string, GoosBean.class);                handler.post(new Runnable() {                    @Override                    public void run() {                        onNetListener.onSuccess(goosBean);                    }                });            }        });    }}

net层
网络请求public class HttpUtils {    private static volatile HttpUtils httpUtils;    private final OkHttpClient client;

private HttpUtils() {        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();        logging.setLevel(HttpLoggingInterceptor.Level.BODY);        client = new OkHttpClient.Builder()                .addInterceptor(logging)                .build();    }

public static HttpUtils getHttpUtils() {        if (httpUtils == null) {            synchronized (HttpUtils.class) {                if (httpUtils == null) {                    httpUtils = new HttpUtils();                }            }        }        return httpUtils;    }

/**     * GET请求     *     * @param url     * @param callback     */    public void doGet(String url, Callback callback) {        Request request = new Request.Builder().url(url).build();        client.newCall(request).enqueue(callback);    }}
返回成功或失败  
public interface OnNetListener<T> {    public void onSuccess(T t);    public void onFailure(Exception e);}
Api类 需要的url
Presenter层里面是关于model层的逻辑
public class MainPresenter {

private final IMainModel imainModel;    private final IMainActivity iMainActivity;

public MainPresenter(IMainActivity iMainActivity) {        this.iMainActivity = iMainActivity;        imainModel = new MainModel();    }

public void getGoods() {        imainModel.getGoods(new OnNetListener<GoosBean>() {            @Override            public void onSuccess(GoosBean goosBean) {                //List<GoosBean.DataBean> groupList, List<List<GoosBean.DataBean.DatasBean>> childList                List<GoosBean.DataBean> dataBean = goosBean.getData();                List<List<GoosBean.DataBean.DatasBean>> childList = new ArrayList<List<GoosBean.DataBean.DatasBean>>();                for (int i = 0; i < dataBean.size(); i++) {                    List<GoosBean.DataBean.DatasBean> datas = dataBean.get(i).getDatas();                    childList.add(datas);                }                iMainActivity.showList(dataBean, childList);            }

@Override            public void onFailure(Exception e) {

}        });

}}

View层定义一个接口
public interface IMainActivity {    public void showList(List<GoosBean.DataBean> groupList, List<List<GoosBean.DataBean.DatasBean>> childList);}

Mainactivity类

public class MainActivity extends AppCompatActivity implements IMainActivity {

private ExpandableListView mElv;    private CheckBox mCheckbox2;    /**     * 0     */    private TextView mTvPrice;    /**     * 结算(0)     */    private TextView mTvNum;    private MyAdapter adapter;

@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        EventBus.getDefault().register(this);        initView();        new MainPresenter(this).getGoods();        mCheckbox2.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                adapter.changeAllListCbState(mCheckbox2.isChecked());            }        });    }

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

}

private void initView() {        mElv = (ExpandableListView) findViewById(R.id.elv);        mCheckbox2 = (CheckBox) findViewById(R.id.checkbox2);        mTvPrice = (TextView) findViewById(R.id.tv_price);        mTvNum = (TextView) findViewById(R.id.tv_num);    }

@Override    public void showList(List<GoosBean.DataBean> groupList, List<List<GoosBean.DataBean.DatasBean>> childList) {        adapter = new MyAdapter(this, groupList, childList);        mElv.setAdapter(adapter);        mElv.setGroupIndicator(null);        //默认让其全部展开        for (int i = 0; i < groupList.size(); i++) {            mElv.expandGroup(i);        }    }

@Subscribe    public void onMessageEvent(MessageEvent event) {        mCheckbox2.setChecked(event.isChecked());    }

@Subscribe    public void onMessageEvent(PriceAndCountEvent event) {        mTvNum.setText("结算(" + event.getCount() + ")");        mTvPrice.setText(event.getPrice() + "");    }}
<shape xmlns:android="http://schemas.android.com/apk/res/android">

<corners android:radius="200dp"></corners>    <stroke android:color="@color/colorPrimaryDark" android:width="1dp"></stroke></shape>

				
时间: 2024-10-24 08:32:23

MVP模式网络请求购物车的相关文章

mvp+RecyclerView实现网络请求

public class Api { public static final String url="http://tingapi.ting.baidu.com/v1/restserver/ting?method=baidu.ting.billboard.billList&type=1&size=10&offset=%s"; } net包下       网络请求 package bwie.com.zhoukao2.net; import java.util.co

从最简单的HelloWorld理解MVP模式

大多数编程语言相关的学习书籍,都会以hello,world这个典型的程序作为第一个示例.作为Android应用开发者,无论使用eclipse还是用android studio,在新建项目的时候,一直按IDE默认选择项,下一步进行下去,就会创建出一个可以运行的hello,world应用程序.对于这个程序,可以认为是采用MVC模式,对应关系为: View:对应于布局文件 Model:业务逻辑和实体模型 Controller:对应于Activity   但是数据绑定.事件处理(hello world程

iOS学习之MVC,MVVM,MVP模式优缺点

为什么要关注架构设计? 因为假如你不关心架构,那么总有一天,需要在同一个庞大的类中调试若干复杂的事情,你会发现在这样的条件下,根本不可能在这个类中快速的找到以及有效的修改任何bug.当然,把这样的一个类想象为一个整体是困难的,因此,有可能一些重要的细节总会在这个过程中会被忽略. 分析三种模式的优缺点: MVC 即 Modal View Controller(模型 视图 控制器). 20 世纪 80年代为编程语言 Smalltalk-80 发明的一种软件设计模式 MVC 的几个明显的特征和体现: 

MVP模式入门(结合Rxjava,Retrofit)

本文MVP的sample实现效果: github地址:https://github.com/xurui1995/MvpSample 老规矩,在说对MVP模式的理解之前还是要再谈谈MVC模式,了解了MVC的缺点.我们才知道为什么要用MVP. 关于MVC的图解,我在网上找到了一些图.如下:    MVC模式在开发web或者管理系统中应用很多,我们的View与人交互,人点击鼠标或者输入一些东西时,View会发送相应的指令给Controller,Controller 接到指令,再去调用Model的方法去

MVP模式在Android中的使用

转载了一篇博客.博客来自:http://www.liuling123.com/2015/12/mvp-pattern-android.html 觉得博主写的非常好 曾经在写项目的时候.没有过多考虑架构模式的问题,由于之前一直做J2EE开发,而J2EE都是採用MVC模式进行开发的.所以在搭建公司项目的时候.也是使用相似MVC的架构(严格来讲.之前的项目还算不上MVC模式,仅仅是简单将网络请求与界面分离.然后通过Handle通知更新界面). 这样的写法,在后面随着项目越来越大,Activty或者Fr

浅谈安卓中的MVP模式

端午放假,天气下雨,于是乎在家撸一下博客,本篇博客将为大家解析MVP模式在安卓中的应用. 本文将从以下几个方面对MVP模式进行讲解: 1.  MVP简介 2.  为什么使用MVP模式 3.  MVP模式实例 4.  MVP中的内存泄露问题 1.  MVP简介: 随着UI创建技术的功能日益增强,UI层也履行着越来越多的职责.为了更好地细分视图(View)与模型(Model)的功能,让View专注于处理数据的可视化以及与用户的交互,同时让Model只关系数据的处理,基于MVC概念的MVP(Model

Android 架构设计实现——MVP模式

转载请注明出处:http://blog.csdn.net/smartbetter/article/details/70853135 随着 UI 创建技术的功能日益增强,UI 层也履行着越来越多的职责.为了更好地细分视图(View)与模型(Model)的功能,让 View 专注于处理数据的可视化以及与用户的交互,同时让 Model 只关系数据的处理,基于 MVC(Model View Controller) 模式的 MVP(Model-View-Presenter) 模式应运而生.目前MVP模式在

MVP模式在Android开发中的最佳实践

这篇文章拖了好久了,一直存在草稿箱里没有继续写,趁今天有空,撸撸完. 回想一下,你刚刚学习Android的时候,总会看到一些书上写着,Android使用的是MVC模式,Activity就是一个Controller,或许那个时候,你没有什么深刻的体会.随着经验的积累.你发现,Activity既是Controller,掌管着许许多多的业务逻辑,同时它也作为View的一部分,控制着视图层的显示.久而久之,这个Controller便显得过于重,职责不再那么单一. 于是,再后来,为了使Activity的职

[转]MVP模式开发

转自:http://www.jianshu.com/p/f7ff18ac1c31 基于面向协议MVP模式下的软件设计-(iOS篇) 字数9196 阅读505 评论3 喜欢11 基于面向协议MVP模式下的软件设计-(iOS篇) 传统模式下的开发 MVC MVVM 基于面向协议MVP的介绍 MVP实战开发 说在前面:相信就算你是个iOS新手也应该听说过MVC的,MVC是构建iOS App的标准模板.随着时间的推移,在iOS平台上MVC也逐渐开始面临着越来越多的问题,最近又开始流行MVVM,MVVM使