mvp实现购物车
程序员文章站
2022-05-23 08:46:42
...
依赖
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
权限
<uses-permission android:name="android.permission.INTERNET"/>
配置application的name属性,ImageLoader初始化
<application
android:name=".appli.App"
</application>
public class App extends Application{
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this).build();
ImageLoader.getInstance().init(configuration);
}
}
布局中需要用到的的布局,在drawable下面新建qujiesuan.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="200dp"/>
<solid android:color="#e53e42"/>
<size android:height="60dp" android:width="130dp"/>
</shape>
布局
custom_jiajian.xml
<?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:gravity="center_vertical"
android:layout_height="wrap_content">
<Button
android:background="#fff"
android:textSize="20sp"
android:id="@+id/reverse"
android:text="一"
android:layout_width="50dp"
android:layout_height="wrap_content" />
<EditText
android:textStyle="bold"
android:textSize="23sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="@+id/count"
/>
<Button
android:id="@+id/add"
android:background="#fff"
android:textSize="25sp"
android:text="+"
android:layout_width="50dp"
android:layout_height="wrap_content" />
</LinearLayout>
recy_cart_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:padding="15dp"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/shop_checkbox"
android:layout_width="50dp"
android:layout_height="50dp" />
<TextView
android:layout_marginLeft="20dp"
android:text="良品铺子"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="23sp"
android:textStyle="bold"
android:id="@+id/shop_name"
/>
</LinearLayout>
<LinearLayout
android:gravity="center_vertical"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/item_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/item_face"
android:src="@mipmap/ic_launcher"
android:layout_width="120dp"
android:layout_height="120dp" />
<LinearLayout
android:layout_marginLeft="10dp"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content">
<TextView
android:id="@+id/item_name"
android:textSize="20sp"
android:text="三只松鼠"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="0dp"
/>
<TextView
android:textColor="#f00"
android:id="@+id/item_price"
android:textSize="23sp"
android:text="299"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
/>
<com.bwie.shopcardemo.customview.CustomJiaJian
android:id="@+id/custom_jiajian"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
<ImageView
android:id="@+id/item_delete"
android:layout_marginRight="10dp"
android:src="@drawable/shopcart_delete"
android:layout_width="30dp"
android:layout_height="30dp" />
</LinearLayout>
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.bwie.shopcardemo.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_View"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<LinearLayout
android:gravity="center_vertical"
android:padding="10dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:background="@drawable/shopcart_unselected"
android:button="@null"
android:id="@+id/quanxuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:textStyle="bold"
android:layout_marginLeft="10dp"
android:textSize="23sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选"
/>
<LinearLayout
android:padding="10dp"
android:layout_marginLeft="10dp"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content">
<TextView
android:textColor="#e53e42"
android:id="@+id/total_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="总价 : ¥0元"
/>
<TextView
android:id="@+id/total_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="共0件商品"
/>
</LinearLayout>
<TextView
android:gravity="center"
android:textSize="25sp"
android:text="去结算"
android:textColor="#fff"
android:background="@drawable/qujiesuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
MainActivity
public class MainActivity extends AppCompatActivity implements ViewCallBack {
private RecyclerView recyclerView;
private TextView total_price;
private TextView total_num;
private CheckBox quanxuan;
private MyPresenter myPresenter;
private RecyAdapter recyAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//http://120.27.23.105/product/getCarts?uid=100
recyclerView = (RecyclerView) findViewById(R.id.recycler_View);
total_price = (TextView) findViewById(R.id.total_price);
total_num = (TextView) findViewById(R.id.total_num);
quanxuan = (CheckBox) findViewById(R.id.quanxuan);
quanxuan.setTag(1);//1为不选中
LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false);
//new出适配器
recyAdapter = new RecyAdapter(this);
myPresenter = new MyPresenter(this);
//调用presenter里面的请求数据的方法
myPresenter.getData();
recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(recyAdapter);
//调用recyAdapter里面的接口,设置 全选按钮 总价 总数量
recyAdapter.setUpdateListener(new RecyAdapter.UpdateListener() {
@Override
public void setTotal(String total, String num, boolean allCheck) {
//设置ui的改变
total_num.setText("共"+num+"件商品");//总数量
total_price.setText("总价 :¥"+total+"元");//总价
if(allCheck){
quanxuan.setTag(2);
quanxuan.setBackgroundResource(R.drawable.shopcart_selected);
}else{
quanxuan.setTag(1);
quanxuan.setBackgroundResource(R.drawable.shopcart_unselected);
}
quanxuan.setChecked(allCheck);
}
});
//这里只做ui更改, 点击全选按钮,,调到adapter里面操作
quanxuan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//调用adapter里面的方法 ,,把当前quanxuan状态传递过去
int tag = (int) quanxuan.getTag();
if(tag==1){
quanxuan.setTag(2);
quanxuan.setBackgroundResource(R.drawable.shopcart_selected);
}else{
quanxuan.setTag(1);
quanxuan.setBackgroundResource(R.drawable.shopcart_unselected);
}
recyAdapter.quanXuan(quanxuan.isChecked());
}
});
}
//实现接口,重写的方法
@Override
public void success(CartBean cartBean) {
//拿到返回来的数据 ,, 传给适配器数据
recyAdapter.add(cartBean);
}
@Override
public void failure(final Exception e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this,"error",Toast.LENGTH_SHORT).show();
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
//调用p层的解除绑定
myPresenter.detach();
}
}
adapter包
RecyAdapter
public class RecyAdapter extends RecyclerView.Adapter<RecyAdapter.MyViewHolder>{
Context context;
//创建大的集合
private List<CartBean.DataBean.ListBean> list;
//存放商家的id和商家的名称的map集合
private Map<String,String> map = new HashMap<>();
public RecyAdapter(Context context) {
this.context = context;
}
/**
* 添加数据并更新显示
* */
public void add(CartBean cartBean){
//传进来的是bean对象
if(list == null){
list = new ArrayList<>();
}
//第一层遍历商家和商品
for (CartBean.DataBean shop : cartBean.getData()){
//把商品的id和商品的名称添加到map集合里 ,,为了之后方便调用
map.put(shop.getSellerid(),shop.getSellerName());
//第二层遍历里面的商品
for (int i=0;i<shop.getList().size();i++){
//添加到list集合里
list.add(shop.getList().get(i));
}
}
//调用方法 设置显示或隐藏 商铺名
setFirst(list);
notifyDataSetChanged();
}
/**
* 设置数据源,控制是否显示商家
* */
private void setFirst(List<CartBean.DataBean.ListBean> list) {
if(list.size()>0){
//如果是第一条数据就设置isFirst为1
list.get(0).setIsFirst(1);
//从第二条开始遍历
for (int i=1;i<list.size();i++){
//如果和前一个商品是同一家商店的
if (list.get(i).getSellerid() == list.get(i-1).getSellerid()){
//设置成2不显示商铺
list.get(i).setIsFirst(2);
}else{//设置成1显示商铺
list.get(i).setIsFirst(1);
//如果当前条目选中,把当前的商铺也选中
if (list.get(i).isItem_check()==true){
list.get(i).setShop_check(list.get(i).isItem_check());
}
}
}
}
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(context, R.layout.recy_cart_item,null);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
/**
* 设置商铺的 shop_checkbox和商铺的名字 显示或隐藏
* */
if(list.get(position).getIsFirst()==1){
//显示商家
holder.shop_checkbox.setVisibility(View.VISIBLE);
holder.shop_name.setVisibility(View.VISIBLE);
//设置shop_checkbox的选中状态
holder.shop_checkbox.setChecked(list.get(position).isShop_check());
holder.shop_name.setText(map.get(String.valueOf(list.get(position).getSellerid())));
}else{//2
//隐藏商家
holder.shop_name.setVisibility(View.GONE);
holder.shop_checkbox.setVisibility(View.GONE);
}
//拆分images字段
String[] split = list.get(position).getImages().split("\\|");
//设置商品的图片
ImageLoader.getInstance().displayImage(split[0],holder.item_face);
//控制商品的item_checkbox,,根据字段改变
holder.item_checkbox.setChecked(list.get(position).isItem_check());
holder.item_name.setText(list.get(position).getTitle());
holder.item_price.setText(list.get(position).getPrice()+"");
//调用customjiajian里面的方法设置 加减号中间的数字
holder.customJiaJian.setEditText(list.get(position).getNum());
//商铺的shop_checkbox点击事件 ,控制商品的item_checkbox
holder.shop_checkbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//先改变数据源中的shop_check
list.get(position).setShop_check(holder.shop_checkbox.isChecked());
for (int i=0;i<list.size();i++){
//如果是同一家商铺的 都给成相同状态
if(list.get(position).getSellerid()==list.get(i).getSellerid()){
//当前条目的选中状态 设置成 当前商铺的选中状态
list.get(i).setItem_check(holder.shop_checkbox.isChecked());
}
}
//刷新适配器
notifyDataSetChanged();
//调用求和的方法
sum(list);
}
});
//商品的item_checkbox点击事件,控制商铺的shop_checkbox
holder.item_checkbox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//先改变数据源中的item_checkbox
list.get(position).setItem_check(holder.item_checkbox.isChecked());
//反向控制商铺的shop_checkbox
for (int i=0;i<list.size();i++){
for (int j=0;j<list.size();j++){
//如果两个商品是同一家店铺的 并且 这两个商品的item_checkbox选中状态不一样
if(list.get(i).getSellerid()==list.get(j).getSellerid() && !list.get(j).isItem_check()){
//就把商铺的shop_checkbox改成false
list.get(i).setShop_check(false);
break;
}else{
//同一家商铺的商品 选中状态都一样,就把商铺shop_checkbox状态改成true
list.get(i).setShop_check(true);
}
}
}
//更新适配器
notifyDataSetChanged();
//调用求和的方法
sum(list);
}
});
//删除条目的点击事件
holder.item_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.remove(position);//移除集合中的当前数据
//删除完当前的条目 重新判断商铺的显示隐藏
setFirst(list);
//调用重新求和
sum(list);
notifyDataSetChanged();
}
});
//加减号的监听,
holder.customJiaJian.setCustomListener(new CustomJiaJian.CustomListener() {
@Override
public void jiajian(int count) {
//改变数据源中的数量
list.get(position).setNum(count);
notifyDataSetChanged();
sum(list);
}
@Override
//输入值 求总价
public void shuRuZhi(int count) {
list.get(position).setNum(count);
notifyDataSetChanged();
sum(list);
}
});
}
/**
* 计算总价的方法
* */
private void sum(List<CartBean.DataBean.ListBean> list){
int totalNum = 0;//初始的总价为0
float totalMoney = 0.0f;
boolean allCheck = true;
for (int i=0;i<list.size();i++){
//把 已经选中的 条目 计算价格
if (list.get(i).isItem_check()){
totalNum += list.get(i).getNum();
totalMoney += list.get(i).getNum() * list.get(i).getPrice();
}else{
//如果有个未选中,就标记为false
allCheck = false;
}
}
//接口回调出去 把总价 总数量 和allcheck 传给view层
updateListener.setTotal(totalMoney+"",totalNum+"",allCheck);
}
//view层调用这个方法, 点击quanxuan按钮的操作
public void quanXuan(boolean checked) {
for (int i=0;i<list.size();i++){
list.get(i).setShop_check(checked);
list.get(i).setItem_check(checked);
}
notifyDataSetChanged();
sum(list);
}
@Override
public int getItemCount() {
return list==null?0:list.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
private final CheckBox shop_checkbox;
private final TextView shop_name;
private final CheckBox item_checkbox;
private final TextView item_name;
private final TextView item_price;
private final CustomJiaJian customJiaJian;
private final ImageView item_delete;
private final ImageView item_face;
public MyViewHolder(View itemView) {
super(itemView);
shop_checkbox = (CheckBox) itemView.findViewById(R.id.shop_checkbox);
shop_name = (TextView) itemView.findViewById(R.id.shop_name);
item_checkbox = (CheckBox) itemView.findViewById(R.id.item_checkbox);
item_name = (TextView) itemView.findViewById(R.id.item_name);
item_price = (TextView) itemView.findViewById(R.id.item_price);
customJiaJian = (CustomJiaJian) itemView.findViewById(R.id.custom_jiajian);
item_delete = (ImageView) itemView.findViewById(R.id.item_delete);
item_face = (ImageView) itemView.findViewById(R.id.item_face);
}
}
UpdateListener updateListener;
public void setUpdateListener(UpdateListener updateListener){
this.updateListener = updateListener;
}
//接口
public interface UpdateListener{
public void setTotal(String total,String num,boolean allCheck);
}
}
bean包
CartBean
接口:https://www.zhaoapi.cn/product/getCarts?uid=4861&source=android
单独在CartBean里面添加:
//自己添加的三个字段 ,用来判断checkBox的选中状态
private int isFirst = 1;//1为显示商铺, 2为隐藏商铺
private boolean item_check;//每个商品的选中状态
private boolean shop_check;//商店的选中状态
public int getIsFirst() {
return isFirst;
}
public void setIsFirst(int isFirst) {
this.isFirst = isFirst;
}
public boolean isItem_check() {
return item_check;
}
public void setItem_check(boolean item_check) {
this.item_check = item_check;
}
public boolean isShop_check() {
return shop_check;
}
public void setShop_check(boolean shop_check) {
this.shop_check = shop_check;
}
public class CartBean {
/**
* msg : 请求成功
* code : 0
* data : [{"list":[{"bargainPrice":111.99,"createtime":"2017-10-03T23:53:28","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":1,"pid":8,"price":324,"pscid":1,"selected":0,"sellerid":1,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家1","sellerid":"1"},{"list":[{"bargainPrice":22.9,"createtime":"2017-10-03T23:53:28","detailUrl":"https://item.m.jd.com/product/2542855.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t1930/284/2865629620/390243/e3ade9c4/56f0a08fNbd3a1235.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2137/336/2802996626/155915/e5e90d7a/56f0a09cN33e01bd0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t1882/31/2772215910/389956/c8dbf370/56f0a0a2Na0c86ea6.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2620/166/2703833710/312660/531aa913/57709035N33857877.jpg!q70.jpg","num":1,"pid":26,"price":88,"pscid":2,"selected":0,"sellerid":3,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋"}],"sellerName":"商家3","sellerid":"3"},{"list":[{"bargainPrice":1999,"createtime":"2017-10-10T16:09:02","detailUrl":"https://item.m.jd.com/product/5025971.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t7210/232/3738666823/232298/9004583e/59c3a9a7N8de42e15.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8356/82/2107423621/109733/c019b8c6/59c3a9a6Ne9a4bdd7.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t10219/74/25356012/171379/7d55e296/59c3a9a8N82fa6e02.jpg!q70.jpg","num":1,"pid":49,"price":333,"pscid":39,"selected":0,"sellerid":5,"subhead":"vivo X20 带你开启全面屏时代!逆光也清晰,照亮你的美!","title":"vivo X20 全面屏手机 全网通 4GB+64GB 金色 移动联通电信4G手机 双卡双待"}],"sellerName":"商家5","sellerid":"5"},{"list":[{"bargainPrice":11800,"createtime":"2017-10-03T23:53:28","detailUrl":"https://mitem.jd.hk/ware/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1","images":"https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5815/178/2614671118/51656/7f52d137/593276c7N107b725a.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5878/60/2557817477/30873/4502b606/593276caN5a7d6357.jpg!q70.jpg","num":1,"pid":62,"price":15999,"pscid":40,"selected":0,"sellerid":6,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家6","sellerid":"6"},{"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":20,"price":599,"pscid":1,"selected":0,"sellerid":13,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家13","sellerid":"13"},{"list":[{"bargainPrice":111.99,"createtime":"2017-10-14T21:48:08","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":22,"price":799,"pscid":1,"selected":0,"sellerid":15,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家15","sellerid":"15"},{"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":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":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 {
/**
* list : [{"bargainPrice":111.99,"createtime":"2017-10-03T23:53:28","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":1,"pid":8,"price":324,"pscid":1,"selected":0,"sellerid":1,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}]
* sellerName : 商家1
* sellerid : 1
*/
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 : 111.99
* createtime : 2017-10-03T23:53:28
* detailUrl : https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends
* images : https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg
* num : 1
* pid : 8
* price : 324.0
* pscid : 1
* selected : 0
* sellerid : 1
* subhead : 每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下
* title : 北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g
*/
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;
//自己添加的三个字段
private int isFirst = 1;//1为显示商铺, 2为隐藏商铺
private boolean item_check;//每个商品的选中状态
private boolean shop_check;//商店的选中状态
public int getIsFirst() {
return isFirst;
}
public void setIsFirst(int isFirst) {
this.isFirst = isFirst;
}
public boolean isItem_check() {
return item_check;
}
public void setItem_check(boolean item_check) {
this.item_check = item_check;
}
public boolean isShop_check() {
return shop_check;
}
public void setShop_check(boolean shop_check) {
this.shop_check = shop_check;
}
public double getBargainPrice() {
return bargainPrice;
}
public void setBargainPrice(double bargainPrice) {
this.bargainPrice = bargainPrice;
}
public String getCreatetime() {
return createtime;
}
public void setCreatetime(String createtime) {
this.createtime = createtime;
}
public String getDetailUrl() {
return detailUrl;
}
public void setDetailUrl(String detailUrl) {
this.detailUrl = detailUrl;
}
public String getImages() {
return images;
}
public void setImages(String images) {
this.images = images;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getPscid() {
return pscid;
}
public void setPscid(int pscid) {
this.pscid = pscid;
}
public int getSelected() {
return selected;
}
public void setSelected(int selected) {
this.selected = selected;
}
public int getSellerid() {
return sellerid;
}
public void setSellerid(int sellerid) {
this.sellerid = sellerid;
}
public String getSubhead() {
return subhead;
}
public void setSubhead(String subhead) {
this.subhead = subhead;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
}
}
callBack包
ModelCallBack
public interface ModelCallBack {
public void success(CartBean cartBean);
public void failure(Exception e);
}
ViewCallBack
public interface ViewCallBack {
public void success(CartBean cartBean);
public void failure(Exception e);
}
customview包
CustomJiaJian
public class CustomJiaJian extends LinearLayout {
private Button reverse;
private Button add;
private EditText countEdit;
private int mCount =1;
public CustomJiaJian(Context context) {
super(context);
}
public CustomJiaJian(Context context, AttributeSet attrs) {
super(context, attrs);
View view = View.inflate(context, R.layout.custom_jiajian,this);
reverse = (Button) view.findViewById(R.id.reverse);
add = (Button) view.findViewById(R.id.add);
countEdit = (EditText) view.findViewById(R.id.count);
reverse.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String content = countEdit.getText().toString().trim();
int count = Integer.valueOf(content);
if(count>1){
mCount = count-1;
countEdit.setText(mCount+"");
//回调给adapter里面
if(customListener!=null){
customListener.jiajian(mCount);
}
}
}
});
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String content = countEdit.getText().toString().trim();
int count = Integer.valueOf(content)+1;
mCount = count;
countEdit.setText(mCount+"");
//接口回调给adapter
if(customListener!=null){
customListener.jiajian(mCount);
}
}
});
}
public CustomJiaJian(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
CustomListener customListener;
public void setCustomListener(CustomListener customListener){
this.customListener = customListener;
}
//加减的接口
public interface CustomListener{
public void jiajian(int count);
public void shuRuZhi(int count);
}
//这个方法是供recyadapter设置 数量时候调用的
public void setEditText(int num) {
if(countEdit !=null) {
countEdit.setText(num + "");
}
}
}
model包
MyModel
public class MyModel {
public void getData(final ModelCallBack modelCallBack){
//访问接口
String path = "https://www.zhaoapi.cn/product/getCarts?uid=4861";
OkhttpUtils.getInstance().asy(null, path, new AbstractUiCallBack<CartBean>() {
@Override
public void success(CartBean cartBean) {
modelCallBack.success(cartBean);
}
@Override
public void fail(Exception e) {
modelCallBack.failure(e);
}
});
}
}
presenter包
MyPresenter
public class MyPresenter {
MyModel myModel = new MyModel();
ViewCallBack viewCallBack;
public MyPresenter(ViewCallBack viewCallBack) {
this.viewCallBack = viewCallBack;
}
//调用model 层的请求数据
public void getData(){
myModel.getData(new ModelCallBack() {
@Override
public void success(CartBean cartBean) {
if(viewCallBack!=null) {
viewCallBack.success(cartBean);
}
}
@Override
public void failure(Exception e) {
if(viewCallBack!=null) {
viewCallBack.failure(e);
}
}
});
}
/**
* 防止内存泄露
* */
public void detach(){
viewCallBack=null;
}
}
okhttp
AbstractUiCallBack
public abstract class AbstractUiCallBack<T> implements Callback {
public abstract void success(T t);
public abstract void fail(Exception e);
private Handler handler = null;
private Class clazz;
public AbstractUiCallBack(){
handler = new Handler(Looper.getMainLooper());
Type type = getClass().getGenericSuperclass();
Type[] arr = ((ParameterizedType)type).getActualTypeArguments();
clazz = (Class) arr[0];
}
@Override
public void onFailure(Call call, IOException e) {
fail(e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
try {
String result = response.body().string();
Gson gson = new Gson();
final T t = (T) gson.fromJson(result,clazz);
handler.post(new Runnable() {
@Override
public void run() {
success(t);//成功的回调出去
}
});
}catch (Exception e){
e.printStackTrace();
fail(e);//失败的回调
}
}
}
MyInterceptor
public class MyInterceptor implements Interceptor {
@Override public Response intercept(Interceptor.Chain chain) throws IOException {
//首先取到Request
Request request = chain.request();
Response response = null;
Request requestProcess = null ;
if("GET".equals(request.method())){
String url = request.url().toString() + "&source=android";
Request.Builder builder = request.newBuilder() ;
builder.get().url(url);
requestProcess = builder.build();
response = chain.proceed(requestProcess);
} else {
FormBody.Builder builder = new FormBody.Builder() ;
RequestBody requestBody = request.body() ;
if(requestBody instanceof FormBody){
FormBody formBody = (FormBody)requestBody ;
for (int i=0;i<formBody.size();i++){
builder.add(formBody.encodedName(i),formBody.encodedValue(i));
}
builder.add("source","android");
}
requestProcess = request.newBuilder().url(request.url().toString()).post(builder.build()).build() ;
response = chain.proceed(requestProcess);
}
return response;
}
}
OkhttpUtils
public class OkhttpUtils {
private static OkhttpUtils okhttpUtils = null;
private OkhttpUtils(){
}
public static OkHttpClient client;
public static OkhttpUtils getInstance(){
if(okhttpUtils==null){
okhttpUtils = new OkhttpUtils();
client = new OkHttpClient.Builder()
.readTimeout(20, TimeUnit.SECONDS)
.writeTimeout(20,TimeUnit.SECONDS)
.connectTimeout(20,TimeUnit.SECONDS)
//添加应用拦截器
.addInterceptor(new MyInterceptor())
.build();
}
return okhttpUtils;
}
public void asy(Map<String,String> params, String url, AbstractUiCallBack callBack){
Request request = null;
if(params!=null){
FormBody.Builder builder = new FormBody.Builder();
for(Map.Entry<String,String> entry : params.entrySet()){
builder.add(entry.getKey(),entry.getValue());
}
FormBody body = builder.build();
request = new Request.Builder()
.url(url)
.post(body)
.build();
}else{
request = new Request.Builder()
.url(url)
.build();
}
client.newCall(request).enqueue(callBack);
}
}