动画的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="weilin.bwei.com.yuekao.MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher_round"
android:id="@+id/img"
android:layout_centerHorizontal="true"
/>
RelativeLayout>
动画的代码
public class MainActivity extends AppCompatActivity {
private ImageView img;
private AnimationSet animationSet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.img);
initAnimates();//初始化动画
/**
* 动画的监听事件
*/
animationSet.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
/**
* 结束监听的事件
*/
Intent intent = new Intent(MainActivity.this, Xiangqing.class);
startActivity(intent);
finish();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
//初始化动画
private void initAnimates() {
//创建属性动画对象
animationSet = new AnimationSet(true);
/** 参数1:从哪个旋转角度开始
* 参数2:转到什么角度
* 后4个参数用于设置围绕着旋转的圆的圆心在哪里
* 参数3:确定x轴坐标的类型,有ABSOLUT绝对坐标、RELATIVE_TO_SELF相对于自身坐标、RELATIVE_TO_PARENT相对于父控件的坐标
* 参数4:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴
* 参数5:确定y轴坐标的类型
* 参数6:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴
*/
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(3000);
// rotateAnimation.setFillAfter(true);
animationSet.addAnimation(rotateAnimation);
/**
* 移动
* 参数1~2:x轴的开始位置
* 参数3~4:y轴的开始位置
* 参数5~6:x轴的结束位置
* 参数7~8:x轴的结束位置
*/
TranslateAnimation translateAnimation =
new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 3f);
translateAnimation.setDuration(3000);
// translateAnimation.setFillAfter(true);
animationSet.addAnimation(translateAnimation);
//创建一个AnimationSet对象,参数为Boolean型,
//true表示使用Animation的interpolator,false则是使用自己的
//创建一个AlphaAnimation对象,参数从完全的透明度,到完全的不透明
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
//设置动画执行的时间
alphaAnimation.setDuration(3000);
// alphaAnimation.setFillAfter(true);
animationSet.addAnimation(alphaAnimation);
//参数1:x轴的初始值
//参数2:x轴收缩后的值
//参数3:y轴的初始值
//参数4:y轴收缩后的值
//参数5:确定x轴坐标的类型
//参数6:x轴的值,0.5f表明是以自身这个控件的一半长度为x轴
//参数7:确定y轴坐标的类型
//参数8:y轴的值,0.5f表明是以自身这个控件的一半长度为x轴
ScaleAnimation scaleAnimation = new ScaleAnimation(
2, 1f, 2, 1f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(3000);
// scaleAnimation.setFillAfter(true);
animationSet.addAnimation(scaleAnimation);
//使动画静止
animationSet.setFillAfter(true);
img.startAnimation(animationSet);
}
}
商品详情
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="weilin.bwei.com.yuekao.view.Xiangqing"
android:orientation="vertical">
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/title"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/price"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/youhuiprice"
android:background="#FF00"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cart"
android:layout_weight="1"
android:text="购物车"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_add"
android:layout_weight="1"
android:text="加入购物车"
/>
LinearLayout>
LinearLayout>
商品详情的代码
public class Xiangqing extends AppCompatActivity implements Iview,AddIv{
private ImageView img;
private TextView title1;
private TextView price1;
private TextView youhuiprice;
private Button cart;
private ShangpingPre shangpingPre;
private Button btn_add;
private AddShoppingPre addShoppingPre;
private int uid=100;
private int pid=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xiangqing);
img = (ImageView) findViewById(R.id.img);
title1= (TextView) findViewById(R.id.title);
price1= (TextView) findViewById(R.id.price);
youhuiprice= (TextView) findViewById(R.id.youhuiprice);
btn_add= (Button) findViewById(R.id.btn_add);
cart= (Button) findViewById(R.id.cart);
loadData();
loadData2();
}
private void loadData2() {
addShoppingPre =new AddShoppingPre(this,this);
addShoppingPre.get(uid,pid);
}
//
private void loadData() {
shangpingPre =new ShangpingPre(this);
shangpingPre.get();
}
@Override
public void onSuccess(Bean.DataBean bean) {
String images = bean.getImages();
String[] split = images.split("\\|");
String title = bean.getTitle();
String bargainPrice =bean.getBargainPrice()+"";
String price = bean.getPrice()+"";
//int pid =bean.getPid();
Glide.with(this).load(split[0]).into(img);
title1.setText("商品名"+ title);
price1.setText("原价:"+price);
price1.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);
youhuiprice.setText("优惠价"+bargainPrice);
}
@Override
public void onFiled(Exception e) {
}
@Override
public void onSucce(final InitBean initBean) {
btn_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadData2();
Toast.makeText(Xiangqing.this,""+initBean.getMsg(), Toast.LENGTH_SHORT).show();
}
});
cart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Xiangqing.this,ShopingCart.class);
startActivity(intent);
}
});
}
@Override
public void onFile(Exception e) {
}
购物车的布局 activity_shoping_cart
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="weilin.bwei.com.yuekao.view.ShopingCart">
<ExpandableListView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
android:id="@+id/exlv"/>
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/rl"
android:background="#ffffff">
<CheckBox
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/checkbox"
android:layout_centerVertical="true"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/qx"
android:layout_centerVertical="true"
android:text="全选"
android:layout_toRightOf="@id/checkbox"/>
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/qx">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/price"
android:text="总价:"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/num"
android:text="共0件商品"/>
LinearLayout>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/jiesuan"
android:background="#ff0000"
android:text="去结算"
android:layout_alignParentRight="true"/>
RelativeLayout>
LinearLayout>
chidren
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="11dp"
android:layout_centerVertical="true"
android:id="@+id/z_che"/>
-<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="30dp"
android:id="@+id/linearLayout"
android:orientation="vertical"
android:layout_toRightOf="@id/z_che">
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/z_title"
android:textStyle="bold"
android:text="三只松鼠大礼包"/>
-<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<ImageView
android:layout_height="140dp"
android:layout_width="140dp"
android:id="@+id/z_img"
android:src="@mipmap/ic_launcher"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/z_price"
android:layout_toRightOf="@+id/z_img"
android:text="价钱"
android:textColor="#ff0000"
android:layout_toEndOf="@+id/z_img"
android:layout_alignParentTop="true"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/z_shuxing"
android:layout_toRightOf="@+id/z_img"
android:text="价钱"
android:layout_toEndOf="@+id/z_img"
android:layout_below="@+id/z_price"/>
-<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:layout_toRightOf="@id/z_img"
android:layout_below="@id/z_shuxing">
<ImageView
android:layout_height="20dp"
android:layout_width="20dp"
android:id="@+id/iv_jian"
android:src="@drawable/shopcart_minus_red"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/tv_num"
android:text="1"
android:layout_marginLeft="10dp"/>
<ImageView
android:layout_height="20dp"
android:layout_width="20dp"
android:id="@+id/iv_add"
android:src="@drawable/shopcart_add_red"
android:layout_marginLeft="5dp"/>
<ImageView
android:layout_height="20dp"
android:layout_width="20dp"
android:id="@+id/del"
android:src="@drawable/rublish"
android:layout_weight="1"
android:layout_gravity="right"/>
LinearLayout>
RelativeLayout>
LinearLayout>
RelativeLayout>
group
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:layout_height="25dp"
android:layout_width="25dp"
android:id="@+id/che"/>
<ImageView
android:layout_height="20dp"
android:layout_width="20dp"
android:src="@drawable/dianpu"
android:layout_marginLeft="20dp"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/g_title"
android:layout_marginLeft="20dp"
android:text="------------"/>
LinearLayout>
购物车的代码 ShopingCart
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.ArrayList;
import java.util.List;
import weilin.bwei.com.yuekao.R;
import weilin.bwei.com.yuekao.bean.ElvDataBean;
import weilin.bwei.com.yuekao.model.MyElvadapter;
import weilin.bwei.com.yuekao.persenter.Elvpresenter;
import weilin.bwei.com.yuekao.utils.MessageEvent;
import weilin.bwei.com.yuekao.utils.PriceAndCountEvent;
public class ShopingCart extends AppCompatActivity implements Iv{
private ExpandableListView exlv;
private CheckBox checkBox;
private MyElvadapter adapter;
private List grouplist = new ArrayList<>();
private List> childlist = new ArrayList<>();
private TextView price;
private TextView num;
private Elvpresenter elvp;
private Button jiesuan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shoping_cart);
exlv= (ExpandableListView) findViewById(R.id.exlv);
checkBox= (CheckBox) findViewById(R.id.checkbox);
price= (TextView) findViewById(R.id.price);
num= (TextView) findViewById(R.id.num);
jiesuan= (Button) findViewById(R.id.jiesuan);
EventBus.getDefault().register(this);
//全选/全不选
checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//设置全选
adapter.changeAllListCbState(checkBox.isChecked());
}
});
jiesuan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent =new Intent(ShopingCart.this,Dingdan.class);
intent.putExtra("key",price.getText().toString()+"");
startActivity(intent);
}
});
loadData();
}
private void loadData() {
elvp = new Elvpresenter(this);
elvp.get();
}
@Override
public void Success(ElvDataBean elvDataBean) {
grouplist.clear();
List data = elvDataBean.getData();
grouplist.addAll(data);
childlist.clear();
for (int i=0;i list = dataBean.getList();
childlist.add(list);
}
adapter =new MyElvadapter(this,grouplist,childlist);
exlv.setAdapter(adapter);
exlv.setGroupIndicator(null);
// 展开
for (int i=0;i@Override
public void Failed(Exception e) {
}
@Subscribe
public void onMessageEvent(MessageEvent event) {
checkBox.setChecked(event.isCheckd());
}
@Subscribe
public void onMessageEven(PriceAndCountEvent event) {
price.setText("总价:"+event.getPrice());
num.setText("共" + event.getCount() + "件商品");
}
//解绑
@Override
protected void onDestroy() {
super.onDestroy();
//eventBus销毁
EventBus.getDefault().unregister(this);
elvp.onDetchView();
}
}
订单的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="weilin.bwei.com.yuekao.view.Dingdan">
<TextView
android:id="@+id/Allprice"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
/>
<Button
android:id="@+id/xiadan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:background="#ff0000"
android:text="立即下单" />
RelativeLayout>
确定订单的代码
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import weilin.bwei.com.yuekao.R;
import weilin.bwei.com.yuekao.bean.OrderBean;
import weilin.bwei.com.yuekao.persenter.OrderPresenter;
public class Dingdan extends AppCompatActivity implements Iva{
private TextView text;
private Button xiadan;
private OrderPresenter orderPresenter;
private int i=100;
private String key;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dingdan);
text= (TextView) findViewById(R.id.Allprice);
xiadan= (Button) findViewById(R.id.xiadan);
Intent intent = getIntent();
key = intent.getStringExtra("key");
text.setText("实付款¥"+ key);
}
private void loadData() {
orderPresenter = new OrderPresenter(this,this);
orderPresenter.get(key);
}
@Override
public void onMake(final OrderBean orderBean) {
xiadan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadData();
Toast.makeText(Dingdan.this,orderBean.getMsg(),Toast.LENGTH_SHORT);
}
});
}
@Override
public void onFider(Exception e) {
}
商品详情的bean
public class Bean {
/**
* msg :
* seller : {"description":"我是商家17","icon":"http://120.27.23.105/images/icon.png","name":"商家17","productNums":999,"score":5,"sellerid":17}
* code : 0
* data : {"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","itemtype":1,"pid":1,"price":118,"pscid":1,"salenum":0,"sellerid":17,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}
*/
private String msg;
private SellerBean seller;
private String code;
private DataBean data;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public SellerBean getSeller() {
return seller;
}
public void setSeller(SellerBean seller) {
this.seller = seller;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public DataBean getData() {
return data;
}
public void setData(DataBean data) {
this.data = data;
}
public static class SellerBean {
/**
* description : 我是商家17
* icon : http://120.27.23.105/images/icon.png
* name : 商家17
* productNums : 999
* score : 5
* sellerid : 17
*/
private String description;
private String icon;
private String name;
private int productNums;
private int score;
private int sellerid;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getProductNums() {
return productNums;
}
public void setProductNums(int productNums) {
this.productNums = productNums;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public int getSellerid() {
return sellerid;
}
public void setSellerid(int sellerid) {
this.sellerid = sellerid;
}
}
public static class DataBean {
/**
* 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
* itemtype : 1
* pid : 1
* price : 118
* pscid : 1
* salenum : 0
* sellerid : 17
* subhead : 每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下
* title : 北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g
*/
private double bargainPrice;
private String createtime;
private String detailUrl;
private String images;
private int itemtype;
private int pid;
private int price;
private int pscid;
private int salenum;
private int sellerid;
private String subhead;
private String title;
public double getBargainPrice() {
return bargainPrice;
}
public void setBargainPrice(double bargainPrice) {
this.bargainPrice = bargainPrice;
}
public String getCreatetime() {
return createtime;
}
public void setCreatetime(String createtime) {
this.createtime = createtime;
}
public String getDetailUrl() {
return detailUrl;
}
public void setDetailUrl(String detailUrl) {
this.detailUrl = detailUrl;
}
public String getImages() {
return images;
}
public void setImages(String images) {
this.images = images;
}
public int getItemtype() {
return itemtype;
}
public void setItemtype(int itemtype) {
this.itemtype = itemtype;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getPscid() {
return pscid;
}
public void setPscid(int pscid) {
this.pscid = pscid;
}
public int getSalenum() {
return salenum;
}
public void setSalenum(int salenum) {
this.salenum = salenum;
}
public int getSellerid() {
return sellerid;
}
public void setSellerid(int sellerid) {
this.sellerid = sellerid;
}
public String getSubhead() {
return subhead;
}
public void setSubhead(String subhead) {
this.subhead = subhead;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
}
购物车的 bean
public class ElvDataBean {
/**
* msg : 请求成功
* code : 0
* data : [{"list":[{"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":2,"pid":24,"price":288,"pscid":2,"selected":0,"sellerid":1,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋"}],"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 黑)"},{"bargainPrice":6666,"createtime":"2017-10-10T16:01:31","detailUrl":"https://item.m.jd.com/product/5089273.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8284/363/1326459580/71585/6d3e8013/59b857f2N6ca75622.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t9346/182/1406837243/282106/68af5b54/59b8480aNe8af7f5c.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8434/54/1359766007/56140/579509d9/59b85801Nfea207db.jpg!q70.jpg","num":1,"pid":46,"price":234,"pscid":39,"selected":0,"sellerid":2,"subhead":"【iPhone新品上市】新一代iPhone,让智能看起来更不一样","title":"Apple iPhone 8 Plus (A1864) 64GB 金色 移动联通电信4G手机"}],"sellerName":"商家2","sellerid":"2"},{"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":87,"pid":10,"price":555.55,"pscid":1,"selected":0,"sellerid":3,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家3","sellerid":"3"},{"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":61,"price":14999,"pscid":40,"selected":0,"sellerid":5,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家5","sellerid":"5"},{"list":[{"bargainPrice":159,"createtime":"2017-10-14T21:49:15","detailUrl":"https://item.m.jd.com/product/5061723.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t8716/197/1271594444/173291/2f40bb4f/59b743bcN8509428e.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8347/264/1286771527/92188/5cf5ec04/59b7420fN65378e9e.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7363/165/3000956253/190883/179a372/59b743bfNd0c79d93.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7399/112/2935531768/183594/b77c7d4a/59b7441aNc3d40133.jpg!q70.jpg","num":1,"pid":100,"price":2200,"pscid":112,"selected":0,"sellerid":11,"subhead":"针织针织闪闪闪亮你的眼","title":"维迩旎 2017秋冬新款长袖针织连衣裙韩版气质中长款名媛包臀A字裙 zx179709 黑色 XL"}],"sellerName":"商家11","sellerid":"11"},{"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":69,"price":16999,"pscid":40,"selected":0,"sellerid":13,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家13","sellerid":"13"},{"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":2,"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":3,"pid":2,"price":299,"pscid":1,"selected":0,"sellerid":18,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家18","sellerid":"18"}]
*/
private String msg;
private String code;
private List 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 getData() {
return data;
}
public void setData(List data) {
this.data = data;
}
public static class DataBean {
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
/**
* list : [{"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":2,"pid":24,"price":288,"pscid":2,"selected":0,"sellerid":1,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋"}]
* sellerName : 商家1
* sellerid : 1
*/
private boolean checked;
private String sellerName;
private String sellerid;
private List 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 getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public static class ListBean {
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
/**
* 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 : 2
* pid : 24
* price : 288
* pscid : 2
* selected : 0
* sellerid : 1
* subhead : 三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》
* title : 三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋
*/
private boolean checked;
private double bargainPrice;
private String createtime;
private String detailUrl;
private String images;
private int num;
private int pid;
private float price;
private int pscid;
private int selected;
private int sellerid;
private String subhead;
private String title;
public double getBargainPrice() {
return bargainPrice;
}
public void setBargainPrice(double bargainPrice) {
this.bargainPrice = bargainPrice;
}
public String getCreatetime() {
return createtime;
}
public void setCreatetime(String createtime) {
this.createtime = createtime;
}
public String getDetailUrl() {
return detailUrl;
}
public void setDetailUrl(String detailUrl) {
this.detailUrl = detailUrl;
}
public String getImages() {
return images;
}
public void setImages(String images) {
this.images = images;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public int getPscid() {
return pscid;
}
public void setPscid(int pscid) {
this.pscid = pscid;
}
public int getSelected() {
return selected;
}
public void setSelected(int selected) {
this.selected = selected;
}
public int getSellerid() {
return sellerid;
}
public void setSellerid(int sellerid) {
this.sellerid = sellerid;
}
public String getSubhead() {
return subhead;
}
public void setSubhead(String subhead) {
this.subhead = subhead;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
}
}
添加购物车的bean
public class InitBean {
private String msg;
private int code;
public InitBean(String msg, int code) {
this.msg = msg;
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
@Override
public String toString() {
return "InitBean{" +
"msg='" + msg + '\'' +
", code=" + code +
'}';
}
}
ListBean
public class ListBean {
private String img;
private String title;
private String price;
public ListBean(String img, String title, String price) {
this.img = img;
this.title = title;
this.price = price;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
订单创建成功OrderBean
public class OrderBean {
/**
* msg : 订单创建成功
* code : 0
*/
private String msg;
private String code;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
model层
CallBack
public interface CallBack {
void onSuccess(Object o);
void onFailed(Exception e);
}
Logger
/**
* 拦截器
*/
public class Logger implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
HttpUrl url=original.url().newBuilder()
.addQueryParameter("source","android")
.build();
//添加请求头
Request request = original.newBuilder()
.url(url)
.build();
return chain.proceed(request);
}
}
MyElvadapter
public class MyElvadapter extends BaseExpandableListAdapter{
private static final String TAG = "ExlvAdapter";
private Context context;
private List groupList;
private List> childList;
private boolean allGroupCbSelect;
public MyElvadapter(Context context, List groupList, List> childList) {
this.context = context;
this.groupList = groupList;
this.childList = childList;
}
@Override
public int getGroupCount() {
return groupList.size();
}
@Override
public int getChildrenCount(int i) {
return childList.get(i).size();
}
@Override
public Object getGroup(int i) {
return groupList.get(i);
}
@Override
public Object getChild(int i, int i1) {
return childList.get(i).get(i1);
}
@Override
public long getGroupId(int i) {
return i;
}
@Override
public long getChildId(int i, int i1) {
return i1;
}
@Override
public boolean hasStableIds() {
return false;
}
//一级列表布局
@Override
public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
final ElvDataBean.DataBean dataBean = groupList.get(i);
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = View.inflate(context, R.layout.group, null);
holder.textView = (TextView) view.findViewById(R.id.g_title);
holder.che = (CheckBox) view.findViewById(R.id.che);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.textView.setText(dataBean.getSellerName());
//设置一级列表checkBox的状态
holder.che.setChecked(dataBean.isChecked());
//一级列表checkBox的点击事件
holder.che.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//判断一级列表复选框的状态 设置为true或false
dataBean.setChecked(holder.che.isChecked());
//改变二级checkbod的状态
changeChildCbState(i, holder.che.isChecked());
//算钱
EventBus.getDefault().post(computer());
//改变全选状态 isAllGroupCbSelect判断一级是否全部选中
changeAllCbState(isAllGroupCbSelect());
//必刷新
notifyDataSetChanged();
}
});
return view;
}
//二级列表布局
@Override
public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
final ElvDataBean.DataBean.ListBean listBean = childList.get(i).get(i1);
final ViewHolder2 holder2;
if (view == null) {
holder2 = new ViewHolder2();
view = View.inflate(context, R.layout.chidren, null);
holder2.z_title = (TextView) view.findViewById(R.id.z_title);
holder2.z_che = (CheckBox) view.findViewById(R.id.z_che);
holder2.img = (ImageView) view.findViewById(R.id.z_img);
holder2.price = view.findViewById(R.id.z_price);
holder2.xiangqing = view.findViewById(R.id.z_shuxing);
holder2.jian = view.findViewById(R.id.iv_jian);
holder2.jia = view.findViewById(R.id.iv_add);
holder2.del = view.findViewById(R.id.del);
holder2.num = view.findViewById(R.id.tv_num);
view.setTag(holder2);
} else {
holder2 = (ViewHolder2) view.getTag();
}
holder2.num.setText(listBean.getNum() + "");
holder2.z_title.setText(listBean.getTitle());
holder2.price.setText("¥" + listBean.getPrice());
holder2.xiangqing.setText(listBean.getSubhead());
//设置二级列表checkbox的属性
holder2.z_che.setChecked(listBean.isChecked());
String images = listBean.getImages();
String[] split = images.split("\\|");
Glide.with(context).load(split[0]).into(holder2.img);
//二级列表的点击事件
holder2.z_che.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//设置该条目中的checkbox属性值
listBean.setChecked(holder2.z_che.isChecked());
//计算价钱
PriceAndCountEvent priceAndCountEvent = computer();
EventBus.getDefault().post(priceAndCountEvent);
//判断当前checkbox是选中的状态
if (holder2.z_che.isChecked()) {
//如果全部选中(isAllChildCbSelected)
if (isAllChildCbSelected(i)) {
//改变一级列表的状态
changeGroupCbState(i, true);
//改变全选的状态
changeAllCbState(isAllGroupCbSelect());
}
} else {
//如果没有全部选中,一级列表的checkbox为false不为选中
changeGroupCbState(i, false);
changeAllCbState(isAllGroupCbSelect());
}
notifyDataSetChanged();
}
});
//加号
holder2.jia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int num = listBean.getNum();
//num为int类型所以要加空字符串
holder2.num.setText(++num + "");
listBean.setNum(num);
//如果二级列表的checkbox为选中,计算价钱
if (holder2.z_che.isChecked()) {
PriceAndCountEvent priceAndCountEvent = computer();
EventBus.getDefault().post(priceAndCountEvent);
}
}
});
//减号
holder2.jian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int num = listBean.getNum();
if (num == 1) {
return;
}
holder2.num.setText(--num + "");
listBean.setNum(num);
if (holder2.z_che.isChecked()) {
PriceAndCountEvent priceAndCountEvent = computer();
EventBus.getDefault().post(priceAndCountEvent);
}
}
});
//删除
holder2.del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
List listBeen = childList.get(i);
ElvDataBean.DataBean.ListBean remove = listBeen.remove(i1);
if (listBeen.size() == 0) {
//先移除二级列表的集合,再移除一级列表的集合
childList.remove(i);
groupList.remove(i);
}
//算钱
EventBus.getDefault().post(computer());
notifyDataSetChanged();
}
});
return view;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return true;
}
class ViewHolder {
TextView textView;
CheckBox che;
}
class ViewHolder2 {
ImageView img;
TextView z_title;
CheckBox z_che;
TextView price;
TextView xiangqing;
ImageView jia;
ImageView jian;
ImageView del;
TextView num;
}
//改变二级列表的checkbox的状态 如果一级选中,控制二级也选中
private void changeChildCbState(int i, boolean flag) {
List listBeen = childList.get(i);
for (int j = 0; j < listBeen.size(); j++) {
ElvDataBean.DataBean.ListBean listBean = listBeen.get(j);
listBean.setChecked(flag);
}
Log.i(TAG, "changeChildCbState: ");
}
//判断一级列表是否全部选中
public boolean isAllGroupCbSelect() {
for (int i = 0; i < groupList.size(); i++) {
ElvDataBean.DataBean dataBean = groupList.get(i);
if (!dataBean.isChecked()) {
return false;
}
}
return true;
}
//改变全选的状态
private void changeAllCbState(boolean flag) {
MessageEvent messageEvent = new MessageEvent();
messageEvent.setCheckd(flag);
EventBus.getDefault().post(messageEvent);
}
//改变一级列表的checkbox的状态
private void changeGroupCbState(int i, boolean flag) {
ElvDataBean.DataBean dataBean = groupList.get(i);
dataBean.setChecked(flag);
}
//判断二级列表是否全部选中
private boolean isAllChildCbSelected(int i) {
List listBeen = childList.get(i);
for (int j = 0; j < listBeen.size(); j++) {
ElvDataBean.DataBean.ListBean listBean = listBeen.get(j);
if (!listBean.isChecked()) {
return false;
}
}
return true;
}
//设置全选,反选
public void changeAllListCbState(boolean flag) {
for (int i = 0; i < groupList.size(); i++) {
changeGroupCbState(i, flag);
changeChildCbState(i, flag);
}
//算钱
EventBus.getDefault().post(computer());
//
notifyDataSetChanged();
}
//计算列表的价钱
private PriceAndCountEvent computer() {
int count = 0;
int price = 0;
for (int i = 0; i < childList.size(); i++) {
List listBeen = childList.get(i);
for (int j = 0; j < listBeen.size(); j++) {
ElvDataBean.DataBean.ListBean listBean = listBeen.get(j);
if (listBean.isChecked()) {
price += listBean.getNum() * listBean.getPrice();
count += listBean.getNum();
}
}
}
PriceAndCountEvent priceAndCountEvent = new PriceAndCountEvent();
priceAndCountEvent.setCount(count);
priceAndCountEvent.setPrice(price);
return priceAndCountEvent;
}
}
OkHttpUtils
public class OkHttpUtils {
private static volatile OkHttpUtils instance;
private static Handler handler = new Handler();
private OkHttpUtils(){
}
public static OkHttpUtils getInstance() {
if (instance == null) {
synchronized (OkHttpUtils.class) {
if (instance == null) {
instance = new OkHttpUtils();
}
}
}
return instance;
}
public void get(String url, Map map, final CallBack callBack, final Class c) {
//对url和参数做拼接处理
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(url);
//判断是否存在? if中是存在
if (stringBuffer.indexOf("?") != -1) {
//判断?是否在最后一位 if中是不在最后一位
if (stringBuffer.indexOf("?") != stringBuffer.length() - 1) {
stringBuffer.append("&");
}
} else {
stringBuffer.append("?");
}
for (Map.Entry entry : map.entrySet()) {
stringBuffer.append(entry.getKey())
.append("=")
.append(entry.getValue())
.append("&");
}
//判断是否存在& if中是存在
if (stringBuffer.indexOf("&") != -1) {
stringBuffer.deleteCharAt(stringBuffer.lastIndexOf("&"));
}
Log.i("TAG",stringBuffer.toString());
OkHttpClient okHttpClient = new OkHttpClient
.Builder()
.addInterceptor(new Logger())
.build();
Request request = new Request.Builder().get().url(stringBuffer.toString()).build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
callBack.onFailed(e);
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String result = response.body().string();
Log.i("TAG",result);
//拿到数据开始解析
final Object o = new Gson().fromJson(result, c);
//当前是在子线程,回到主线程中
handler.post(new Runnable() {
@Override
public void run() {
//回调
callBack.onSuccess(o);
}
});
}
});
}
}
persenter
添加购物车 AddShoppingPre
public class AddShoppingPre {
private Context context;
private AddIv addiv;
public AddShoppingPre(Context context, AddIv addiv) {
this.context = context;
this.addiv = addiv;
}
public void get(int i,int j){
Map map = new HashMap();
map.put("uid",i+"");
map.put("pid",j+"");
OkHttpUtils.getInstance().get("http://120.27.23.105/product/addCart", map, new CallBack() {
@Override
public void onSuccess(Object o) {
InitBean initBean = (InitBean) o;
addiv.onSucce(initBean);
}
@Override
public void onFailed(Exception e) {
}
}, InitBean.class);
}
}
Elvpresenter
public class Elvpresenter {
private Iv ivew;
public Elvpresenter(Iv ivew) {
this.ivew = ivew;
}
public void get(){
Map map = new HashMap();
map.put("uid","100");
OkHttpUtils.getInstance().get("http://120.27.23.105/product/getCarts", map, new CallBack() {
@Override
public void onSuccess(Object o) {
ElvDataBean elvDataBean = (ElvDataBean) o;
ivew.Success(elvDataBean);
}
@Override
public void onFailed(Exception e) {
ivew.Failed(e);
}
},ElvDataBean.class);
}
//解绑
public void onDetchView(){
if (ivew!=null){
ivew=null;
}
}
}
OrderPresenter
public class OrderPresenter {
private Context context;
private Iva iv;
public OrderPresenter(Context context, Iva iv) {
this.context = context;
this.iv = iv;
}
public void get(String key){
Map map = new HashMap();
map.put("uid","100");
map.put("price",key);
OkHttpUtils.getInstance().get("http://120.27.23.105/product/createOrder", map, new CallBack() {
@Override
public void onSuccess(Object o) {
OrderBean orderBean = (OrderBean) o;
iv.onMake(orderBean);
}
@Override
public void onFailed(Exception e) {
}
}, OrderBean.class);
}
}
ShangpingPre
public class ShangpingPre {
private Iview iv;
public ShangpingPre( Iview iv) {
this.iv = iv;
}
public void get(){
Map map = new HashMap();
map.put("pid","1");
OkHttpUtils.getInstance().get("https://www.zhaoapi.cn/product/getProductDetail", map, new CallBack() {
@Override
public void onSuccess(Object o) {
Bean bean = (Bean) o;
Bean.DataBean data = bean.getData();
iv.onSuccess(data);
}
@Override
public void onFailed(Exception e) {
}
}, Bean.class);
}
}
utils
MessageEvent
public class MessageEvent {
private boolean checkd;
public boolean isCheckd(){
return checkd;
}
public void setCheckd(boolean checkd){
this.checkd=checkd;
}
}
PriceAndCountEvent
public class PriceAndCountEvent {
private int price;
private int count;
public int getPrice() {
return price;
}
public int getCount() {
return count;
}
public void setPrice(int price) {
this.price = price;
}
public void setCount(int count) {
this.count = count;
}
}
view层
AddIv
public interface AddIv {
void onSucce(InitBean initBean);
void onFile(Exception e);
}
Dingdan
public class Dingdan extends AppCompatActivity implements Iva{
private TextView text;
private Button xiadan;
private OrderPresenter orderPresenter;
private int i=100;
private String key;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dingdan);
text= (TextView) findViewById(R.id.Allprice);
xiadan= (Button) findViewById(R.id.xiadan);
Intent intent = getIntent();
key = intent.getStringExtra("key");
text.setText("实付款¥"+ key);
}
private void loadData() {
orderPresenter = new OrderPresenter(this,this);
orderPresenter.get(key);
}
@Override
public void onMake(final OrderBean orderBean) {
xiadan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loadData();
Toast.makeText(Dingdan.this,orderBean.getMsg(),Toast.LENGTH_SHORT);
}
});
}
@Override
public void onFider(Exception e) {
}
}
Iv
public interface Iv {
void Success(ElvDataBean elvDataBean);
void Failed(Exception e);
}
Iva
public interface Iva {
void onMake(OrderBean orderBean);
void onFider(Exception e);
}
Iview
public interface Iview {
void onSuccess(Bean.DataBean bean);
void onFiled(Exception e);
}