----购物车类-----
package com.example.administrator.gouwuche_20171219.view.activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.administrator.gouwuche_20171219.R;
import com.example.administrator.gouwuche_20171219.model.bean.CartBean;
import com.example.administrator.gouwuche_20171219.model.bean.CountPriceBean;
import com.example.administrator.gouwuche_20171219.presenter.CartPresenter;
import com.example.administrator.gouwuche_20171219.util.ApiUtil;
import com.example.administrator.gouwuche_20171219.view.adapter.CartAdapter;
import com.example.administrator.gouwuche_20171219.view.interfac.CartView;
import com.example.administrator.gouwuche_20171219.view.zidingyi_shitu.CartExpanableListview;
import com.nostra13.universalimageloader.utils.L;
public class CartActivity extends AppCompatActivity implements CartView{
private CartExpanableListview expanable_listview;
private CheckBox check_all;
private TextView text_total;
private TextView text_buy;
private CartPresenter cartPresenter;
private RelativeLayout relative_progress;
private LinearLayout linear_layout;
private CartBean cartBean;
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
if (msg.what==0){
countPriceBean = (CountPriceBean) msg.obj;
text_total.setText("合计:¥"+ countPriceBean.getPriceString());
text_buy.setText("去结算("+ countPriceBean.getCount()+")");
}
}
};
private CartAdapter cartAdapter;
private CountPriceBean countPriceBean;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
//二级列表
expanable_listview = findViewById(R.id.expanable_listview);
//复选框 全选按钮
check_all = findViewById(R.id.check_all);
//合计 价钱
text_total = findViewById(R.id.text_total);
//去结算 数量
text_buy = findViewById(R.id.text_buy);
relative_progress = findViewById(R.id.relative_progress);
linear_layout = findViewById(R.id.linear_layout);
//去掉默认的指示器
expanable_listview.setGroupIndicator(null);
//获取数据 关联p层
cartPresenter = new CartPresenter(this);
//复选框的点击事件
check_all.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
cartAdapter.setAllChildState(check_all.isChecked());
}
});
//点击去结算
text_buy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(CartActivity.this, JieSuanActivity.class);
intent.putExtra("price",countPriceBean.getPriceString());
startActivity(intent);
}
});
}
@Override
protected void onResume() {
super.onResume();
relative_progress.setVisibility(View.VISIBLE);
cartPresenter.getDataUrl(ApiUtil.selGouWuChe);
}
/**
* 解决MVP内存泄漏
*/
@Override
protected void onDestroy() {
super.onDestroy();
//解除绑定
if (cartPresenter!=null){
cartPresenter.destroy();
}
}
@Override
public void success(final CartBean cartBean) {
this.cartBean=cartBean;
this.runOnUiThread(new Runnable() {
@Override
public void run() {
relative_progress.setVisibility(View.GONE);
if (cartBean!=null){
linear_layout.setVisibility(View.VISIBLE);
//1.根据组中子条目是否选中,,,决定该组是否选中...初始化一下每一组中isGroupCheck这个数据
for (int i=0;i<cartBean.getData().size();i++){
if (isAllChildInGroupSelected(i)){
cartBean.getData().get(i).setGroupChecked(true);
}
}
//2.根据每一个组是否选中的状态,,,初始化全选是否选中
check_all.setChecked(isAllGroupChecked());
//设置适配器
cartAdapter = new CartAdapter(CartActivity.this,cartBean,cartPresenter,handler);
expanable_listview.setAdapter(cartAdapter);
//展开
for (int i= 0;i<cartBean.getData().size();i++){
expanable_listview.expandGroup(i);
}
//3.根据子条目是否选中 初始化价格和数量
cartAdapter.sendPriceAndCount();
}else {
//隐藏下面的全选.... 等
linear_layout.setVisibility(View.GONE);
//显示去逛逛,,,添加购物车
Toast.makeText(CartActivity.this,"购物车为空,去逛逛",Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void failed(Exception e) {
Toast.makeText(CartActivity.this,"数据出错",Toast.LENGTH_SHORT).show();
}
private boolean isAllGroupChecked(){
for (int i=0;i<cartBean.getData().size();i++){
if (!cartBean.getData().get(i).isGroupChecked()){
return false;
}
}
return true;
}
/**
* 判断当前组里面所有的子条目是否选中
* @param groupPosition
* @return
*/
private boolean isAllChildInGroupSelected(int groupPosition){
for (int i=0;i<cartBean.getData().get(groupPosition).getList().size();i++){
if (cartBean.getData().get(groupPosition).getList().get(i).getSelected()==0){
return false;
}
}
return true;
}
}
-----适配器-----
package com.example.administrator.gouwuche_20171219.view.adapter;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.administrator.gouwuche_20171219.R;
import com.example.administrator.gouwuche_20171219.model.bean.CartBean;
import com.example.administrator.gouwuche_20171219.model.bean.CountPriceBean;
import com.example.administrator.gouwuche_20171219.presenter.CartPresenter;
import com.example.administrator.gouwuche_20171219.util.ApiUtil;
import com.example.administrator.gouwuche_20171219.util.OkHttp3Util;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
/**
* Created by Administrator on 2017/12/19.
*/
public class CartAdapter extends BaseExpandableListAdapter{
private final Handler handler;
private CartPresenter cartPresenter;
Context context;
CartBean cartBean;
private int size;
private int childI;
private int allSize;
private int index;
public CartAdapter(Context context, CartBean cartBean, CartPresenter cartPresenter, Handler handler) {
this.context = context;
this.cartBean = cartBean;
this.cartPresenter=cartPresenter;
this.handler=handler;
}
@Override
public int getGroupCount() {
return cartBean.getData().size();
}
@Override
public int getChildrenCount(int i) {
return cartBean.getData().get(i).getList().size();
}
@Override
public Object getGroup(int i) {
return cartBean.getData().get(i);
}
@Override
public Object getChild(int i, int i1) {
return cartBean.getData().get(i).getList().get(i1);
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int i1) {
return i1;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
final GroupHolder groupHolder;
if (view==null){
groupHolder = new GroupHolder();
view=View.inflate(context, R.layout.group_item_layout,null);
//复选框 一级列表
groupHolder.checkBox=view.findViewById(R.id.check_group);
//商家名称
groupHolder.textView=view.findViewById(R.id.text_group);
view.setTag(groupHolder);
}else {
groupHolder = (GroupHolder) view.getTag();
}
final CartBean.DataBean dataBean = cartBean.getData().get(i);
groupHolder.textView.setText(dataBean.getSellerName());
groupHolder.checkBox.setChecked(dataBean.isGroupChecked());
groupHolder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
size = dataBean.getList().size();
childI = 0;
updateAllInGroup(groupHolder.checkBox.isChecked(),dataBean);
}
});
return view;
}
private void updateAllInGroup(final boolean checked, final CartBean.DataBean dataBean) {
CartBean.DataBean.ListBean listBean = dataBean.getList().get(childI);
Map<String, String> map=new HashMap<>();
map.put("uid","3690");
map.put("sellerid", String.valueOf(listBean.getSellerid()));
map.put("pid", String.valueOf(listBean.getPid()));
map.put("selected", String.valueOf(checked ? 1:0));
map.put("num", String.valueOf(listBean.getNum()));
OkHttp3Util.doPost(ApiUtil.updateCartUrl, map, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()){
childI=childI+1;
if (childI<size){
updateAllInGroup(checked,dataBean);
}else {
//所有的条目已经更新完成....再次请求查询购物车的数据
cartPresenter.getDataUrl(ApiUtil.selGouWuChe);
}
}
}
});
}
@Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
ChildHolder childHolder;
if(view==null){
childHolder = new ChildHolder();
view=View.inflate(context,R.layout.child_item_layout,null);
childHolder.check_child=view.findViewById(R.id.check_child);
childHolder.image_good=view.findViewById(R.id.image_good);
childHolder.text_title=view.findViewById(R.id.text_title);
childHolder.text_price=view.findViewById(R.id.text_price);
childHolder.text_jian=view.findViewById(R.id.text_jian);
childHolder.text_num=view.findViewById(R.id.text_num);
childHolder.text_add=view.findViewById(R.id.text_add);
childHolder.text_delete=view.findViewById(R.id.text_delete);
view.setTag(childHolder);
}else {
childHolder = (ChildHolder) view.getTag();
}
//赋值
final CartBean.DataBean.ListBean listBean = cartBean.getData().get(i).getList().get(i1);
childHolder.text_num.setText(listBean.getNum()+"");//......注意
childHolder.text_price.setText("¥"+listBean.getBargainPrice());
childHolder.text_title.setText(listBean.getTitle());
//listBean.getSelected().....0false,,,1true
//设置checkBox选中状态
childHolder.check_child.setChecked(listBean.getSelected()==0? false:true);
Glide.with(context).load(listBean.getImages().split("\\|")[0]).into(childHolder.image_good);
//点击事件
childHolder.check_child.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
updateChildChecked(listBean);
}
});
//加号
childHolder.text_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//请求更新的接口
updateChildNum(listBean,true);
}
});
//减号
childHolder.text_jian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (listBean.getNum()==1){
return;
}
//更新数量,,,减
updateChildNum(listBean,false);
}
});
return view;
}
private void updateChildNum(CartBean.DataBean.ListBean listBean, boolean isAdded) {
Map<String, String> map=new HashMap<>();
map.put("uid","3690");
map.put("sellerid", String.valueOf(listBean.getSellerid()));
map.put("pid", String.valueOf(listBean.getPid()));
map.put("selected", String.valueOf(listBean.getSelected()));
if (isAdded){
map.put("num", String.valueOf(listBean.getNum() + 1));
}else {
map.put("num", String.valueOf(listBean.getNum() - 1));
}
OkHttp3Util.doPost(ApiUtil.updateCartUrl, map, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()){
cartPresenter.getDataUrl(ApiUtil.selGouWuChe);
}
}
});
}
private void updateChildChecked(CartBean.DataBean.ListBean listBean) {
Map<String, String> map=new HashMap<>();
map.put("uid","3690");
map.put("sellerid", String.valueOf(listBean.getSellerid()));
map.put("pid", String.valueOf(listBean.getPid()));
map.put("selected", String.valueOf(listBean.getSelected() == 0? 1:0));
map.put("num", String.valueOf(listBean.getNum()));
OkHttp3Util.doPost(ApiUtil.updateCartUrl, map, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//更新成功之后...网络上的数据发生了改变...再次请求购物车的接口进行数据的展示
if (response.isSuccessful()){
cartPresenter.getDataUrl(ApiUtil.selGouWuChe);
}
}
});
}
/**
* 计算价格和数量 并发送显示
*/
public void sendPriceAndCount() {
double price = 0;
int count = 0;
//通过判断二级列表是否勾选,,,,计算价格数量
for (int i=0;i<cartBean.getData().size();i++){
for (int j = 0;j<cartBean.getData().get(i).getList().size();j++){
if (cartBean.getData().get(i).getList().get(j).getSelected() == 1){
//价格是打折的价格...........
price += cartBean.getData().get(i).getList().get(j).getNum() * cartBean.getData().get(i).getList().get(j).getBargainPrice();
count += cartBean.getData().get(i).getList().get(j).getNum();
}
}
}
//精准的保留double的两位小数
DecimalFormat decimalFormat = new DecimalFormat("#.00");
String priceString = decimalFormat.format(price);
CountPriceBean countPriceBean = new CountPriceBean(priceString, count);
//发送...显示
Message msg = Message.obtain();
msg.what = 0;
msg.obj = countPriceBean;
handler.sendMessage(msg);
}
/**
* 根据全选的状态,,,,跟新每一个子条目的状态,,,全部更新完成后,查询购物车的数据进行展示
* @param checked
*/
public void setAllChildState(boolean checked) {
//创建一个集合 装所有的子条目
List<CartBean.DataBean.ListBean> allList = new ArrayList<>();
for (int i=0;i<cartBean.getData().size();i++){
for (int j=0;j<cartBean.getData().get(i).getList().size();j++){
allList.add(cartBean.getData().get(i).getList().get(j));
}
}
// relative_progress.setVisibility(View.VISIBLE);
allSize = allList.size();
index = 0;
//通过 递归 更新所有子条目的选中
updateAllChild(allList,checked);
}
private void updateAllChild(final List<CartBean.DataBean.ListBean> allList, final boolean checked) {
CartBean.DataBean.ListBean listBean = allList.get(index);//0
//跟新的操作
//?uid=71&sellerid=1&pid=1&selected=0&num=10
Map<String, String> map = new HashMap<>();
map.put("uid","3690");
map.put("sellerid", String.valueOf(listBean.getSellerid()));
map.put("pid", String.valueOf(listBean.getPid()));
map.put("selected", String.valueOf(checked ? 1:0));
map.put("num", String.valueOf(listBean.getNum()));
OkHttp3Util.doPost(ApiUtil.updateCartUrl, map, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()){
index = index +1;//0,1,2......3
if (index < allSize){
updateAllChild(allList,checked);
}else {
//查询购物车
cartPresenter.getDataUrl(ApiUtil.selGouWuChe);
}
}
}
});
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
class GroupHolder{
CheckBox checkBox;
TextView textView;
}
class ChildHolder{
CheckBox check_child;
ImageView image_good;
TextView text_title;
TextView text_price;
TextView text_jian;
TextView text_num;
TextView text_add;
TextView text_delete;
}
}
-----主布局-----
<?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.example.administrator.gouwuche_20171219.view.activity.CartActivity">
<ScrollView
android:layout_above="@+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--购物车的二级列表-->
<com.example.administrator.gouwuche_20171219.view.zidingyi_shitu.CartExpanableListview
android:id="@+id/expanable_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.example.administrator.gouwuche_20171219.view.zidingyi_shitu.CartExpanableListview>
</LinearLayout>
</ScrollView>
<RelativeLayout
android:visibility="gone"
android:id="@+id/relative_progress"
android:layout_above="@+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<LinearLayout
android:id="@+id/linear_layout"
android:layout_alignParentBottom="true"
android:gravity="center_vertical"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp">
<CheckBox
android:layout_marginLeft="10dp"
android:button="@null"
android:background="@drawable/check_box_selector"
android:id="@+id/check_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text_total"
android:text="合计:¥0.00"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:text="去结算(0)"
android:background="#ff0000"
android:textColor="#ffffff"
android:gravity="center"
android:id="@+id/text_buy"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout>
-----自定义二级列表------
package com.example.administrator.gouwuche_20171219.view.zidingyi_shitu;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ExpandableListView;
/**
* Created by Administrator on 2017/12/19.
*/
public class CartExpanableListview extends ExpandableListView {
public CartExpanableListview(Context context) {
super(context);
}
public CartExpanableListview(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CartExpanableListview(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
---复选框的图片是否选中模式-----
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/shopping_cart_checked"/>
<item android:state_checked="false" android:drawable="@drawable/shopping_cart_none_check"/>
<item android:drawable="@drawable/shopping_cart_none_check"/>
</selector>
-----一级列表布局------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:padding="10dp"
android:layout_height="match_parent">
<CheckBox
android:button="@null"
android:background="@drawable/check_box_selector"
android:id="@+id/check_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_marginLeft="10dp"
android:text="京东自营"
android:id="@+id/text_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
-----二级列表布局------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:padding="10dp"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/rel"
android:layout_toLeftOf="@+id/text_delete"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:layout_centerVertical="true"
android:id="@+id/check_child"
android:button="@null"
android:background="@drawable/check_box_selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/image_good"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/check_child"
android:layout_marginLeft="10dp"
android:layout_width="80dp"
android:layout_height="80dp" />
<TextView
android:id="@+id/text_title"
android:layout_toRightOf="@+id/image_good"
android:layout_marginLeft="10dp"
android:layout_alignTop="@+id/image_good"
android:maxLines="2"
android:minLines="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text_price"
android:layout_toRightOf="@+id/image_good"
android:layout_marginLeft="10dp"
android:layout_alignBottom="@+id/image_good"
android:text="¥99.99"
android:textColor="#ff0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_alignParentRight="true"
android:layout_alignBottom="@+id/image_good"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text_jian"
android:text="一"
android:padding="5dp"
android:background="@drawable/bian_kuang_line"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:gravity="center"
android:id="@+id/text_num"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:background="@drawable/bian_kuang_line"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<TextView
android:id="@+id/text_add"
android:text="十"
android:padding="5dp"
android:background="@drawable/bian_kuang_line"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
<TextView
android:layout_marginLeft="3dp"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/rel"
android:layout_alignBottom="@+id/rel"
android:id="@+id/text_delete"
android:background="#ff0000"
android:text="删除"
android:gravity="center"
android:textColor="#ffffff"
android:layout_width="50dp"
android:layout_height="match_parent" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:padding="10dp"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/rel"
android:layout_toLeftOf="@+id/text_delete"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:layout_centerVertical="true"
android:id="@+id/check_child"
android:button="@null"
android:background="@drawable/check_box_selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/image_good"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/check_child"
android:layout_marginLeft="10dp"
android:layout_width="80dp"
android:layout_height="80dp" />
<TextView
android:id="@+id/text_title"
android:layout_toRightOf="@+id/image_good"
android:layout_marginLeft="10dp"
android:layout_alignTop="@+id/image_good"
android:maxLines="2"
android:minLines="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text_price"
android:layout_toRightOf="@+id/image_good"
android:layout_marginLeft="10dp"
android:layout_alignBottom="@+id/image_good"
android:text="¥99.99"
android:textColor="#ff0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_alignParentRight="true"
android:layout_alignBottom="@+id/image_good"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text_jian"
android:text="一"
android:padding="5dp"
android:background="@drawable/bian_kuang_line"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:gravity="center"
android:id="@+id/text_num"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:background="@drawable/bian_kuang_line"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<TextView
android:id="@+id/text_add"
android:text="十"
android:padding="5dp"
android:background="@drawable/bian_kuang_line"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
<TextView
android:layout_marginLeft="3dp"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/rel"
android:layout_alignBottom="@+id/rel"
android:id="@+id/text_delete"
android:background="#ff0000"
android:text="删除"
android:gravity="center"
android:textColor="#ffffff"
android:layout_width="50dp"
android:layout_height="match_parent" />
</RelativeLayout>
-----数量增加和减少的边框-----
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff" />
<stroke
android:width="0.1dp"
android:color="#000000" />
</shape>
-----封装bean类------
package com.example.administrator.gouwuche_20171219.model.bean;
import java.util.List;
/**
* Created by Administrator on 2017/12/19.
*/
public class CartBean {
/**
* msg : 请求成功
* code : 0
* data : [{"list":[{"bargainPrice":399,"createtime":"2017-10-02T15:20:02","detailUrl":"https://item.m.jd.com/product/1439822107.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t5887/201/859509257/69994/6bde9bf6/59224c24Ne854e14c.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5641/233/853609022/57374/5c73d281/59224c24N3324d5f4.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5641/233/853609022/57374/5c73d281/59224c24N3324d5f4.jpg!q70.jpg","num":7,"pid":81,"price":699,"pscid":85,"selected":0,"sellerid":2,"subhead":"满2件,总价打6.50折","title":"Gap男装 休闲舒适简约水洗五袋直筒长裤紧身牛仔裤941825 深灰色 33/32(175/84A)"}],"sellerName":"商家2","sellerid":"2"},{"list":[{"bargainPrice":399,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/1439822107.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t5887/201/859509257/69994/6bde9bf6/59224c24Ne854e14c.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5641/233/853609022/57374/5c73d281/59224c24N3324d5f4.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5641/233/853609022/57374/5c73d281/59224c24N3324d5f4.jpg!q70.jpg","num":1,"pid":83,"price":444,"pscid":85,"selected":0,"sellerid":4,"subhead":"满2件,总价打6.50折","title":"Gap男装 休闲舒适简约水洗五袋直筒长裤紧身牛仔裤941825 深灰色 33/32(175/84A)"}],"sellerName":"商家4","sellerid":"4"},{"list":[{"bargainPrice":399,"createtime":"2017-10-03T23:53:28","detailUrl":"https://item.m.jd.com/product/1439822107.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t5887/201/859509257/69994/6bde9bf6/59224c24Ne854e14c.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5641/233/853609022/57374/5c73d281/59224c24N3324d5f4.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5641/233/853609022/57374/5c73d281/59224c24N3324d5f4.jpg!q70.jpg","num":1,"pid":87,"price":888,"pscid":85,"selected":0,"sellerid":8,"subhead":"满2件,总价打6.50折","title":"Gap男装 休闲舒适简约水洗五袋直筒长裤紧身牛仔裤941825 深灰色 33/32(175/84A)"}],"sellerName":"商家8","sellerid":"8"},{"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-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":1,"pid":2,"price":299,"pscid":1,"selected":0,"sellerid":18,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家18","sellerid":"18"},{"list":[{"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":1,"pid":5,"price":88.99,"pscid":1,"selected":0,"sellerid":21,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家21","sellerid":"21"}]
*/
private String msg;
private String code;
private List<DataBean> data;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public List<DataBean> getData() {
return data;
}
public void setData(List<DataBean> data) {
this.data = data;
}
public static class DataBean {
public boolean isGroupChecked() {
return isGroupChecked;
}
public void setGroupChecked(boolean groupChecked) {
isGroupChecked = groupChecked;
}
/**
* list : [{"bargainPrice":399,"createtime":"2017-10-02T15:20:02","detailUrl":"https://item.m.jd.com/product/1439822107.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t5887/201/859509257/69994/6bde9bf6/59224c24Ne854e14c.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5641/233/853609022/57374/5c73d281/59224c24N3324d5f4.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5641/233/853609022/57374/5c73d281/59224c24N3324d5f4.jpg!q70.jpg","num":7,"pid":81,"price":699,"pscid":85,"selected":0,"sellerid":2,"subhead":"满2件,总价打6.50折","title":"Gap男装 休闲舒适简约水洗五袋直筒长裤紧身牛仔裤941825 深灰色 33/32(175/84A)"}]
* sellerName : 商家2
* sellerid : 2
*/
private boolean isGroupChecked; //一级列表是否选中
private String sellerName;
private String sellerid;
private List<ListBean> list;
public String getSellerName() {
return sellerName;
}
public void setSellerName(String sellerName) {
this.sellerName = sellerName;
}
public String getSellerid() {
return sellerid;
}
public void setSellerid(String sellerid) {
this.sellerid = sellerid;
}
public List<ListBean> getList() {
return list;
}
public void setList(List<ListBean> list) {
this.list = list;
}
public static class ListBean {
/**
* bargainPrice : 399.0
* createtime : 2017-10-02T15:20:02
* detailUrl : https://item.m.jd.com/product/1439822107.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends
* images : https://m.360buyimg.com/n0/jfs/t5887/201/859509257/69994/6bde9bf6/59224c24Ne854e14c.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5641/233/853609022/57374/5c73d281/59224c24N3324d5f4.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5641/233/853609022/57374/5c73d281/59224c24N3324d5f4.jpg!q70.jpg
* num : 7
* pid : 81
* price : 699.0
* pscid : 85
* selected : 0
* sellerid : 2
* subhead : 满2件,总价打6.50折
* title : Gap男装 休闲舒适简约水洗五袋直筒长裤紧身牛仔裤941825 深灰色 33/32(175/84A)
*/
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;
}
}
}
}
---P层类-----
package com.example.administrator.gouwuche_20171219.presenter;
import com.example.administrator.gouwuche_20171219.model.CartModel;
import com.example.administrator.gouwuche_20171219.model.bean.CartBean;
import com.example.administrator.gouwuche_20171219.presenter.interfac.CartP;
import com.example.administrator.gouwuche_20171219.view.activity.CartActivity;
import com.example.administrator.gouwuche_20171219.view.interfac.CartView;
/**
* Created by Administrator on 2017/12/19.
*/
public class CartPresenter implements CartP{
private CartModel cartModel;
CartView cartView;
public CartPresenter(CartView cartView) {
this.cartView = cartView;
cartModel = new CartModel(this);
}
public void getDataUrl(String selGouWuChe) {
cartModel.getDataUrlM(selGouWuChe);
}
@Override
public void success(CartBean cartBean) {
cartView.success(cartBean);
}
@Override
public void failed(Exception e) {
cartView.failed(e);
}
//销毁
public void destroy() {
if (cartView!=null){
cartView=null;
}
}
}
-----P层接口和View-----
package com.example.administrator.gouwuche_20171219.presenter.interfac;
import com.example.administrator.gouwuche_20171219.model.bean.CartBean;
import com.example.administrator.gouwuche_20171219.model.bean.SecondBean;
/**
* Created by Administrator on 2017/12/19.
*/
public interface CartP {
public void success(CartBean cartBean);
public void failed(Exception e);
}
----Model类-----
package com.example.administrator.gouwuche_20171219.model;
import com.example.administrator.gouwuche_20171219.model.bean.CartBean;
import com.example.administrator.gouwuche_20171219.presenter.CartPresenter;
import com.example.administrator.gouwuche_20171219.presenter.interfac.CartP;
import com.example.administrator.gouwuche_20171219.util.OkHttp3Util;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
/**
* Created by Administrator on 2017/12/19.
*/
public class CartModel {
CartP cartP;
public CartModel(CartP cartP) {
this.cartP = cartP;
}
public void getDataUrlM(String selGouWuChe) {
OkHttp3Util.doGet(selGouWuChe, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
cartP.failed(e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()){
String string = response.body().string();
Gson gson = new Gson();
CartBean cartBean = gson.fromJson(string, CartBean.class);
cartP.success(cartBean);
}
}
});
}
}
--接口---
package com.example.administrator.gouwuche_20171219.util;
/**
* Created by Administrator on 2017/12/19.
*/
public class ApiUtil {
//查询购物车
public static String selGouWuChe="https://www.zhaoapi.cn/product/getCarts?uid=3690";
//更新数据
public static final String updateCartUrl = "https://www.zhaoapi.cn/product/updateCarts";
//创建订单
public static final String dingdan = "https://www.zhaoapi.cn/product/createOrder?uid=3690";
//订单状态
public static final String zhuangtai = "https://www.zhaoapi.cn/product/getOrders";
}