购物车

xiaoxiao2021-02-28  22

依赖

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.jakewharton:butterknife:8.8.1' annotationProcessor'com.jakewharton:butterknife-compiler:8.8.1' compile 'com.facebook.fresco:fresco:0.13.0' compile 'com.squareup.retrofit2:retrofit:2.3.0' compile 'com.squareup.retrofit2:converter-gson:2.0.2' compile 'io.reactivex.rxjava2:rxjava:2.1.6' compile 'io.reactivex.rxjava2:rxandroid:2.0.1' compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0' 权限 <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> package com.example.com.gouwuche; 住方法 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.LinearLayout; import android.widget.TextView; import com.example.com.gouwuche.MyView.CarTitleView; import com.example.com.gouwuche.R; import com.example.com.gouwuche.adapter.CarShopAdapter; import com.example.com.gouwuche.bean.Car_child_shop_Bean; import com.example.com.gouwuche.bean.Car_shop_Bean; import com.example.com.gouwuche.bean.SelBean; import com.example.com.gouwuche.innter.CarShopCheckedInterface; import com.example.com.gouwuche.innter.CarTitleInterface; import com.example.com.gouwuche.innter.IVInterface; import com.example.com.gouwuche.innter.NumberAndPrice; import com.example.com.gouwuche.presenter.Persent; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; public class MainActivity extends AppCompatActivity implements IVInterface { @BindView(R.id.ctv_title) CarTitleView ctv_title; @BindView(R.id.pt_lv) ExpandableListView pt_lv; @BindView(R.id.btn_toBuy) Button btn_toBuy; @BindView(R.id.ll_toBuy) LinearLayout ll_toBuy; @BindView(R.id.cb_all) CheckBox cb_all; @BindView(R.id.tv_rental) TextView tv_rental; @BindView(R.id.tv_num) TextView tv_num; @BindView(R.id.btn_goto) Button btn_goto; @BindView(R.id.lll_one) LinearLayout lll_one; @BindView(R.id.cb_alldel) CheckBox cb_alldel; @BindView(R.id.btn_delete_all) Button btn_delete_all; @BindView(R.id.l_two) LinearLayout l_two; private ArrayList<Car_shop_Bean> list_shop; private ArrayList<List<Car_child_shop_Bean>> list_shop_chiled; private CarShopAdapter carShopAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); initTitle(); initSelCar(); initDelCar(); } private void initDelCar() { Persent persent = new Persent(MainActivity.this); Map<String, String> map = new HashMap<>(); map.put("uid",1299+""); persent.getData(map, "http://120.27.23.105/product/", 0); } private void initSelCar() { } //头布局的点击事件 private void initTitle() { ctv_title.setCarTitleView(new CarTitleInterface() { boolean b = true; public TextView tv_compile; //点击编辑---- @Override public void compileClick(View v) { b = !b; tv_compile = (TextView) v.findViewById(R.id.tv_compile); if (b) { tv_compile.setText("编辑"); lll_one.setVisibility(View.VISIBLE); l_two.setVisibility(View.GONE); carShopAdapter.notifyDataSetChanged(); } else { tv_compile.setText("完成"); lll_one.setVisibility(View.GONE); l_two.setVisibility(View.VISIBLE); carShopAdapter.notifyDataSetChanged(); } } }); } @OnClick({R.id.btn_toBuy, R.id.cb_all, R.id.btn_goto, R.id.cb_alldel, R.id.btn_delete_all}) public void onViewClicked(View view) { switch (view.getId()) { case R.id.btn_toBuy: break; case R.id.cb_all: boolean checked = cb_all.isChecked(); for (int i = 0; i < list_shop.size(); i++) { list_shop.get(i).setFlag(checked); } for (List<Car_child_shop_Bean> i : list_shop_chiled) { for (int j = 0; j < i.size(); j++) { i.get(j).setChildCheck(checked); } } carShopAdapter.notifyDataSetChanged(); foreach(list_shop_chiled); break; case R.id.btn_goto: break; case R.id.cb_alldel: boolean checkeds = cb_alldel.isChecked(); for (int i = 0; i < list_shop.size(); i++) { list_shop.get(i).setFlag(checkeds); } for (List<Car_child_shop_Bean> i : list_shop_chiled) { for (int j = 0; j < i.size(); j++) { i.get(j).setChildCheck(checkeds); } } carShopAdapter.notifyDataSetChanged(); break; //点击删除 case R.id.btn_delete_all: ArrayList<Car_shop_Bean> list_new_shop = new ArrayList<Car_shop_Bean>(); ArrayList<List<Car_child_shop_Bean>> list_new_child = new ArrayList<List<Car_child_shop_Bean>>(); for (int i = 0; i < list_shop.size(); i++) { Car_shop_Bean car_shop_bean = list_shop.get(i); List<Car_child_shop_Bean> car_child_shop_been = list_shop_chiled.get(i); Boolean flag = list_shop.get(i).getFlag(); if (flag) { list_new_shop.add(car_shop_bean); list_new_child.add(car_child_shop_been); } ArrayList<Car_child_shop_Bean> list_child_new = new ArrayList<Car_child_shop_Bean>(); for (int j = 0; j < list_shop_chiled.get(i).size(); j++) { Car_child_shop_Bean car_child_shop_bean = list_shop_chiled.get(i).get(j); boolean childCheck = list_shop_chiled.get(i).get(j).isChildCheck(); if (childCheck) { list_child_new.add(car_child_shop_bean); int pid = car_child_shop_bean.getPid(); Map<String, String> map = new HashMap<String, String>(); map.put("pid", pid + ""); map.put("uid", 1299 + ""); Persent persent = new Persent(MainActivity.this); persent.getData(map, "http://120.27.23.105/product/", 1); } } list_shop_chiled.get(i).removeAll(list_child_new); } tv_rental.setText("合计:" + 0); tv_num.setText("数量:" + 0); list_shop.removeAll(list_new_shop); list_shop_chiled.removeAll(list_new_child); carShopAdapter.notifyDataSetChanged(); initAllData(list_shop); break; } } //请求成功 @Override public void Success(Object o,int tag) { //查询购物车 if(tag==0){ SelBean selBean = (SelBean)o; if(selBean!=null){ List<SelBean.DataBean> data = selBean.getData(); list_shop = new ArrayList<Car_shop_Bean>(); list_shop_chiled = new ArrayList<List<Car_child_shop_Bean>>(); //设置选择状态 for (int i = 0; i < data.size(); i++) { String sellerName = data.get(i).getSellerName(); list_shop.add(new Car_shop_Bean(sellerName, false)); List<SelBean.DataBean.ListBean> list = data.get(i).getList(); ArrayList<Car_child_shop_Bean> car_shop_been = new ArrayList<>(); for (SelBean.DataBean.ListBean childList : list) { int num = childList.getNum(); float price = childList.getPrice(); String images = childList.getImages(); String title = childList.getTitle(); int pid = childList.getPid(); String[] split = images.toString().split("\\|"); car_shop_been.add(new Car_child_shop_Bean(pid, title, num, price, split[0], false)); } list_shop_chiled.add(car_shop_been); } initAllData(list_shop); //设置适配器 carShopAdapter = new CarShopAdapter(MainActivity.this, list_shop, list_shop_chiled); cb_all.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { boolean checked = cb_all.isChecked(); for (int i = 0; i < list_shop.size(); i++) { list_shop.get(i).setFlag(checked); } for (List<Car_child_shop_Bean> i : list_shop_chiled) { for (int j = 0; j < i.size(); j++) { i.get(j).setChildCheck(checked); } } carShopAdapter.notifyDataSetChanged(); foreach(list_shop_chiled); } }); cb_alldel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { boolean checked = cb_alldel.isChecked(); for (int i = 0; i < list_shop.size(); i++) { list_shop.get(i).setFlag(checked); } for (List<Car_child_shop_Bean> i : list_shop_chiled) { for (int j = 0; j < i.size(); j++) { i.get(j).setChildCheck(checked); } } carShopAdapter.notifyDataSetChanged(); } }); carShopAdapter.setCarShop(new CarShopCheckedInterface() { @Override public void AllChecked(boolean ischeck) { if (!ischeck) { cb_all.setChecked(false); cb_alldel.setChecked(false); } else { cb_all.setChecked(true); cb_alldel.setChecked(true); } } }); carShopAdapter.setNumberAndPrice(new NumberAndPrice() { @Override public void AllNumAndPriceClick(final ArrayList<List<Car_child_shop_Bean>> list_shop_child) { int max = 0; int nums = 0; for (List<Car_child_shop_Bean> list_child : list_shop_child) { for (int i = 0; i < list_child.size(); i++) { boolean childCheck = list_child.get(i).isChildCheck(); Car_child_shop_Bean car_child_shop_bean = list_child.get(i); if (childCheck) { int num = car_child_shop_bean.getNum(); double price = car_child_shop_bean.getPrice(); int prices = (int) price; max += prices * num; nums++; } } } tv_rental.setText("合计:" + max); tv_num.setText("数量:" + nums); } }); pt_lv.setAdapter(carShopAdapter); //让他默认收回 int count = pt_lv.getCount(); for (int i = 0; i < count; i++) { pt_lv.expandGroup(i); } pt_lv.setGroupIndicator(null); } }else //删除购物车 if(tag==1){ } } //请求失败 @Override public void Failed(String message) { } //判断集合中有没有数据 private void initAllData(ArrayList<Car_shop_Bean> list_shop) { if(list_shop.size()==0){ ll_toBuy.setVisibility(View.VISIBLE); }else{ ll_toBuy.setVisibility(View.GONE); pt_lv.setVisibility(View.VISIBLE); } } //计算总价和数量 public void foreach(ArrayList<List<Car_child_shop_Bean>> list_shop_child) { carShopAdapter.notifyDataSetChanged(); int max = 0; int nums = 0; for (List<Car_child_shop_Bean> list_child : list_shop_child) { for (int i = 0; i < list_child.size(); i++) { boolean childCheck = list_child.get(i).isChildCheck(); Car_child_shop_Bean car_child_shop_bean = list_child.get(i); if (childCheck) { int num = car_child_shop_bean.getNum(); double price = car_child_shop_bean.getPrice(); int prices = (int) price; max += prices * num; nums++; } } } tv_rental.setText("合计:" + max); tv_num.setText("数量:" + nums); } } 主布局 <?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.com.gouwuche.MainActivity"> <com.example.com.gouwuche.MyView.CarTitleView android:id="@+id/ctv_title" android:layout_width="match_parent" android:layout_height="40dp"/> <ExpandableListView android:id="@+id/pt_lv" android:layout_below="@id/ctv_title" android:descendantFocusability = "blocksDescendants" android:divider="@null" android:scrollbars="none" android:layout_width="match_parent" android:layout_height="wrap_content"> </ExpandableListView> <LinearLayout android:visibility="gone" android:orientation="vertical" android:id="@+id/ll_toBuy" android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:layout_gravity="center" android:id="@+id/btn_toBuy" android:text="去逛逛" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:id="@+id/lll_one" android:orientation="horizontal" android:visibility="visible" android:background="#FFFFFF" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="50dp"> <CheckBox android:layout_gravity="center" android:id="@+id/cb_all" android:text="全选" android:textSize="16dp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_rental" android:layout_marginLeft="20dp" android:gravity="center_vertical" android:text="合计:0" android:textSize="20dp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" /> <TextView android:id="@+id/tv_num" android:layout_marginLeft="10dp" android:gravity="center_vertical" android:text="数量:0" android:textSize="20dp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" /> <Button android:id="@+id/btn_goto" android:layout_width="0dp" android:layout_weight="1" android:layout_gravity="right" android:gravity="center" android:layout_height="match_parent" android:background="#cc0000" android:text="去结算" android:textColor="#FFFF" android:textSize="20dp" /> </LinearLayout> <LinearLayout android:id="@+id/l_two" android:orientation="horizontal" android:visibility="gone" android:background="#FFFFFF" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="50dp"> <CheckBox android:layout_gravity="center" android:id="@+id/cb_alldel" android:text="全选" android:textSize="16dp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> <TextView android:layout_marginLeft="20dp" android:gravity="center_vertical" android:text="分享" android:textSize="20dp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" /> <TextView android:layout_marginLeft="10dp" android:gravity="center_vertical" android:text="移出关注" android:textSize="20dp" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" /> <Button android:id="@+id/btn_delete_all" android:layout_width="0dp" android:layout_weight="1" android:layout_gravity="right" android:gravity="center" android:layout_height="match_parent" android:text="删除" android:textColor="#FFFF" android:textSize="20dp" /> </LinearLayout> </RelativeLayout> car_shop_fragment.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:background="#F0F2F5" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:background="#F7FBF7" android:layout_width="match_parent" android:layout_height="200dp"> <CheckBox android:id="@+id/rb_commodity" android:layout_gravity="center" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> <LinearLayout android:layout_marginLeft="10dp" android:orientation="horizontal" android:layout_width="0dp" android:layout_weight="9" android:layout_height="300dp"> <ImageView android:gravity="center" android:id="@+id/iv_commodityImg" android:layout_width="80dp" android:layout_height="80dp" /> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv_commodityName" android:gravity="center" android:layout_width="match_parent" android:layout_weight="1" android:layout_height="0dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <TextView android:textSize="20dp" android:id="@+id/tv_commodityPrice" android:layout_width="0dp" android:layout_weight="1" android:layout_marginTop="20dp" android:layout_height="wrap_content" android:text="ooooooo" /> <LinearLayout android:id="@+id/ll_num" android:layout_width="0dp" android:layout_weight="1" android:gravity="right" android:layout_height="match_parent" > <Button android:id="@+id/btn_delete" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"/> <EditText android:id="@+id/et_num" android:gravity="center" android:layout_width="0dp" android:layout_weight="2" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_add" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout> </LinearLayout> </LinearLayout> </LinearLayout> </LinearLayout>car_shop_one_fragment.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_marginTop="20dp" android:background="#F8F8F8" android:layout_width="match_parent" android:layout_height="wrap_content"> <CheckBox android:id="@+id/rb_shop" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_shopName" android:layout_marginLeft="10dp" android:textSize="20dp" android:layout_width="0dp" android:layout_weight="9" android:layout_height="wrap_content"> </TextView> </LinearLayout> </LinearLayout>car_title_fragment.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="购物车" android:textSize="24dp" android:gravity="center" android:paddingLeft="80dp" android:layout_width="0dp" android:layout_weight="7" android:layout_height="wrap_content" /> <TextView android:layout_marginLeft="50dp" android:id="@+id/tv_compile" android:text="编辑" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="right" android:textSize="18dp" /> </LinearLayout>adapter层 package com.example.com.gouwuche.adapter; import android.content.Context; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.bumptech.glide.Glide; import com.example.com.gouwuche.R; import com.example.com.gouwuche.bean.Car_child_shop_Bean; import com.example.com.gouwuche.bean.Car_shop_Bean; import com.example.com.gouwuche.innter.CarShopCheckedInterface; import com.example.com.gouwuche.innter.NumberAndPrice; import com.example.com.gouwuche.innter.ShopNumInterface; import java.util.ArrayList; import java.util.List; /** * author:Created by jiangkerun on 2018/4/23. */ public class CarShopAdapter extends BaseExpandableListAdapter { private Context context; private ArrayList<Car_shop_Bean> list_shop; private ArrayList<List<Car_child_shop_Bean>> list_shop_child; private CarShopCheckedInterface carShopCheckedInterface; private ShopNumInterface shopNumInterface; private NumberAndPrice numberAndPrice; private TextView tv_shopName; private ImageView iv_commodityImg; private TextView tv_commodityName; private TextView tv_commodityPrice; private Button btn_delete; private Button btn_add; private EditText et_num; private int shopNum = 0; private int allNum = 0; private int allPrice = 0; private Button btn_delete_only; public CarShopAdapter(Context context, ArrayList<Car_shop_Bean> list_shop, ArrayList<List<Car_child_shop_Bean>> list_shop_child) { this.context = context; this.list_shop = list_shop; this.list_shop_child = list_shop_child; } public void setCarShop(CarShopCheckedInterface carShopCheckedInterface){ this.carShopCheckedInterface = carShopCheckedInterface; } public void setShopList(ShopNumInterface shopNumInterface){ this.shopNumInterface = shopNumInterface; } public void setNumberAndPrice(NumberAndPrice numberAndPrice){ this.numberAndPrice = numberAndPrice; } @Override public int getGroupCount() { return list_shop.size(); } @Override public int getChildrenCount(int i) { return list_shop_child.get(i).size(); } @Override public Object getGroup(int i) { return list_shop.get(i); } @Override public Object getChild(int i, int i1) { return list_shop_child.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 true; } @Override public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) { final Car_shop_Bean car_shop_bean1 = list_shop.get(i); final List<Car_child_shop_Bean> car_child_shop_been = list_shop_child.get(i); view = View.inflate(context, R.layout.car_shop_one_fragment, null); final CheckBox rb_shop = (CheckBox)view.findViewById(R.id.rb_shop); tv_shopName = (TextView)view.findViewById(R.id.tv_shopName); rb_shop.setChecked(car_shop_bean1.getFlag()); tv_shopName.setText(car_shop_bean1.getName()); rb_shop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Log.e("---------点击:",rb_shop.isChecked()+""); list_shop.get(i).setFlag(rb_shop.isChecked()); boolean checked = rb_shop.isChecked(); car_shop_bean1.setFlag(checked); boolean a = true; for (int j = 0; j < list_shop.size(); j++) { Boolean flag = list_shop.get(j).getFlag(); if(flag==false){ a = false; carShopCheckedInterface.AllChecked(a); break; }else{ a = true; carShopCheckedInterface.AllChecked(a); } } for (int j = 0; j < car_child_shop_been.size(); j++) { car_child_shop_been.get(j).setChildCheck(rb_shop.isChecked()); } notifyDataSetChanged(); for (Car_child_shop_Bean list_child:car_child_shop_been){ numberAndPrice.AllNumAndPriceClick(list_shop_child); } } }); return view; } @Override public View getChildView(final int i, final int i1, final boolean b, View view, ViewGroup viewGroup) { final Car_shop_Bean car_shop_bean = list_shop.get(i); final Car_child_shop_Bean car_child_shop_bean = list_shop_child.get(i).get(i1); view = View.inflate(context, R.layout.car_shop_fragment, null); final CheckBox rb_commodity = (CheckBox)view.findViewById(R.id.rb_commodity); iv_commodityImg = (ImageView)view.findViewById(R.id.iv_commodityImg); tv_commodityName = (TextView)view.findViewById(R.id.tv_commodityName); tv_commodityPrice = (TextView)view.findViewById(R.id.tv_commodityPrice); btn_delete = (Button)view.findViewById(R.id.btn_delete); btn_add = (Button)view.findViewById(R.id.btn_add); et_num = (EditText)view.findViewById(R.id.et_num); et_num.setText(car_child_shop_bean.getNum()+""); rb_commodity.setChecked(car_child_shop_bean.isChildCheck()); tv_commodityName.setText(car_child_shop_bean.getTitle()); Glide.with(context).load(car_child_shop_bean.getImages()).into(iv_commodityImg); tv_commodityPrice.setText(":"+car_child_shop_bean.getPrice()); btn_delete.setText("—"); btn_add.setText("+"); notifyDataSetChanged(); rb_commodity.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { car_child_shop_bean.setChildCheck(rb_commodity.isChecked()); boolean flag = true; for (int j = 0; j < list_shop_child.get(i).size(); j++) { if(list_shop_child.get(i).get(j).isChildCheck()==false) { flag = false; break; } } if(flag){ list_shop.get(i).setFlag(true); }else{ list_shop.get(i).setFlag(false); } boolean c = true; for (int j = 0; j < list_shop.size(); j++) { if(list_shop.get(j).getFlag() == false){ c = false; break; }else{ c = true; } } if(c){ carShopCheckedInterface.AllChecked(true); }else{ carShopCheckedInterface.AllChecked(false); } Log.e("-----取到的索引:",i1+""); numberAndPrice.AllNumAndPriceClick(list_shop_child); notifyDataSetChanged(); } }); btn_add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { boolean childCheck = car_child_shop_bean.isChildCheck(); int num1 = car_child_shop_bean.getNum(); num1++; car_child_shop_bean.setNum(num1); numberAndPrice.AllNumAndPriceClick(list_shop_child); notifyDataSetChanged(); } }); final boolean childCheck = car_child_shop_bean.isChildCheck(); btn_delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int num1 = car_child_shop_bean.getNum(); num1--; if(num1<1){ car_child_shop_bean.setNum(1); Toast.makeText(context,"不能再少了···",Toast.LENGTH_SHORT).show(); }else{ car_child_shop_bean.setNum(num1); } numberAndPrice.AllNumAndPriceClick(list_shop_child); notifyDataSetChanged(); } }); return view; } @Override public boolean isChildSelectable(int i, int i1) { return true; } } bean类 package com.example.com.gouwuche.bean; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import org.json.JSONException; import org.json.JSONObject; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; /** * author:Created by jiangkerun on 2018/4/23. */ public class AddBean { /** * msg : 加购成功 * code : 0 */ private String msg; private String code; public static AddBean objectFromData(String str) { return new Gson().fromJson(str, AddBean.class); } public static AddBean objectFromData(String str, String key) { try { JSONObject jsonObject = new JSONObject(str); return new Gson().fromJson(jsonObject.getString(str), AddBean.class); } catch (JSONException e) { e.printStackTrace(); } return null; } public static List<AddBean> arrayAddBeanFromData(String str) { Type listType = new TypeToken<ArrayList<AddBean>>() { }.getType(); return new Gson().fromJson(str, listType); } public static List<AddBean> arrayAddBeanFromData(String str, String key) { try { JSONObject jsonObject = new JSONObject(str); Type listType = new TypeToken<ArrayList<AddBean>>() { }.getType(); return new Gson().fromJson(jsonObject.getString(str), listType); } catch (JSONException e) { e.printStackTrace(); } return new ArrayList(); } 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; } } package com.example.com.gouwuche.bean; /** * author:Created by jiangkerun on 2018/4/23. */ public class Car_child_shop_Bean { private int pid; private String title; private int num; private double price; private String images; private boolean childCheck; public Car_child_shop_Bean(int pid, String title, int num, double price, String images, boolean childCheck) { this.pid = pid; this.title = title; this.num = num; this.price = price; this.images = images; this.childCheck = childCheck; } public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getImages() { return images; } public void setImages(String images) { this.images = images; } public boolean isChildCheck() { return childCheck; } public void setChildCheck(boolean childCheck) { this.childCheck = childCheck; } } package com.example.com.gouwuche.bean; /** * author:Created by jiangkerun on 2018/4/23. */ public class Car_shop_Bean { private String name; private boolean flag; public Car_shop_Bean(String name, Boolean flag) { this.name = name; this.flag = flag; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Boolean getFlag() { return flag; } public void setFlag(Boolean flag) { this.flag = flag; } } package com.example.com.gouwuche.bean; /** * author:Created by jiangkerun on 2018/4/23. */ public class DelBean { 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; } } package com.example.com.gouwuche.bean; import java.util.List; /** * author:Created by jiangkerun on 2018/4/23. */ public class SelBean { 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 { 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 { 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; } } } } inner接口 package com.example.com.gouwuche.innter; /** * author:Created by jiangkerun on 2018/4/23. */ public interface CarShopCheckedInterface { void AllChecked(boolean ischeck); } package com.example.com.gouwuche.innter; import android.view.View; /** * author:Created by jiangkerun on 2018/4/23. */ public interface CarTitleInterface { void compileClick(View v); }   package com.example.com.gouwuche.innter; /** * author:Created by jiangkerun on 2018/4/23. */ public interface IVInterface { void Success(Object o, int tag); void Failed(String message); } package com.example.com.gouwuche.innter; import com.example.com.gouwuche.bean.Car_child_shop_Bean; import java.util.ArrayList; import java.util.List; /** * author:Created by jiangkerun on 2018/4/23. */ public interface NumberAndPrice { void AllNumAndPriceClick(ArrayList<List<Car_child_shop_Bean>> list_shop_child); } package com.example.com.gouwuche.innter; import com.example.com.gouwuche.bean.Car_child_shop_Bean; import java.util.List; /** * author:Created by jiangkerun on 2018/4/23. */ public interface ShopNumInterface { void ListClick(List<Car_child_shop_Bean> car_child_shop_been); } model层 package com.example.com.gouwuche.model; import android.util.Log; import com.example.com.gouwuche.bean.DelBean; import com.example.com.gouwuche.bean.SelBean; import com.example.com.gouwuche.presenter.Persent; import com.example.com.gouwuche.utils.RetrofitUtils; import java.util.Map; import io.reactivex.Flowable; /** * Created by linmeihui on 2018/4/25. */ public class Model implements ModelInterface { private Persent persent; private Flowable<SelBean> selFlowable; private Flowable<DelBean> delFlowable; public Model(Persent persent){ this.persent = persent; } @Override public void getData(Map<String, String> map, String uri, int tag) { Log.e("-------M层的TAG:",tag+"----"); if(tag==0){ selFlowable = RetrofitUtils.getInstance(map, uri, tag).getSelFlowable(); persent.getSelCar(selFlowable,tag); }else if(tag==1){ delFlowable = RetrofitUtils.getInstance(map, uri, tag).getDelFlowable(); if(delFlowable!=null) { persent.getDelCar(delFlowable, tag); } } } } package com.example.com.gouwuche.model; import java.util.Map; /** * Created by linmeihui on 2018/4/25. */ public interface ModelInterface { void getData(Map<String, String> map, String uri, int tag); } MyView层 package com.example.com.gouwuche.MyView; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import com.example.com.gouwuche.R; import com.example.com.gouwuche.innter.CarTitleInterface; /** * author:Created by jiangkerun on 2018/4/23. */ public class CarTitleView extends LinearLayout implements View.OnClickListener{ private CarTitleInterface carTitleInterface; private ImageView title_address; private TextView tv_compile; private ImageView iv_message; public void setCarTitleView(CarTitleInterface carTitleInterface){ this.carTitleInterface = carTitleInterface; } public CarTitleView(Context context) { this(context,null); } public CarTitleView(Context context, AttributeSet attrs) { this(context, attrs,0); } public CarTitleView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); View.inflate(context, R.layout.car_title_fragment,this); initView(); } private void initView() { tv_compile = (TextView)findViewById(R.id.tv_compile); tv_compile.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.tv_compile: Log.e("-------点击事件:","--------"); carTitleInterface.compileClick(view); break; } } } presenter层 package com.example.com.gouwuche.presenter; import com.example.com.gouwuche.bean.DelBean; import com.example.com.gouwuche.bean.SelBean; import com.example.com.gouwuche.innter.IVInterface; import com.example.com.gouwuche.model.Model; import com.example.com.gouwuche.model.ModelInterface; import java.util.Map; import io.reactivex.Flowable; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.schedulers.Schedulers; import io.reactivex.subscribers.DefaultSubscriber; /** * author:Created by jiangkerun on 2018/4/23. */ public class Persent implements PersentInterface{ private IVInterface ivInterface; public Persent(IVInterface ivInterface){ this.ivInterface = ivInterface; } @Override public void getData(Map<String, String> map, String uri, int tag) { ModelInterface modelInterface = new Model(this); modelInterface.getData(map,uri,tag); } //得到主页的网络请求 public void getSelCar(Flowable<SelBean> builder, final int tag){ builder.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new DefaultSubscriber<SelBean>() { @Override public void onNext(SelBean selBean) { if(selBean!=null){ ivInterface.Success(selBean,tag); } } @Override public void onError(Throwable t) { ivInterface.Failed(t.getMessage()); } @Override public void onComplete() { } }); } //得到详情的网络请求 public void getDelCar(Flowable<DelBean> delBeanFlowable, final int tag){ delBeanFlowable.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new DefaultSubscriber<DelBean>() { @Override public void onNext(DelBean delBean) { if (delBean!=null){ ivInterface.Success(delBean,tag); } } @Override public void onError(Throwable t) { ivInterface.Failed(t.getMessage()); } @Override public void onComplete() { } }); } } package com.example.com.gouwuche.presenter; import java.util.Map; /** * author:Created by jiangkerun on 2018/4/23. */ public interface PersentInterface { void getData(Map<String, String> map, String uri, int tag); } utils层 package com.example.com.gouwuche.utils; import java.io.IOException; import okhttp3.HttpUrl; import okhttp3.Interceptor; import okhttp3.Request; import okhttp3.Response; /** * author:Created by jiangkerun on 2018/4/23. */ 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); } } package com.example.com.gouwuche.utils; import android.util.Log; import com.example.com.gouwuche.bean.DelBean; import com.example.com.gouwuche.bean.SelBean; import com.google.gson.Gson; import java.util.Map; import io.reactivex.Flowable; import okhttp3.OkHttpClient; import retrofit2.Retrofit; import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; import retrofit2.converter.gson.GsonConverterFactory; /** * author:Created by jiangkerun on 2018/4/23. */ public class RetrofitUtils { //单例模式 private static volatile RetrofitUtils instance; private Flowable<SelBean> selBeanFlowable; private Flowable<DelBean> delBeanFlowable; private RetrofitUtils(Map<String,String> map, String uri, int tag){ OkHttpClient build = new OkHttpClient.Builder() .addInterceptor(new Logger()) .build(); Retrofit build1 = new Retrofit.Builder() .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create(new Gson())) .client(build) .baseUrl(uri) .build(); RxJava rxJava = build1.create(RxJava.class); Log.e("---TAG:",tag+""); if(tag==0){ selBeanFlowable = rxJava.Sel_Car(map); }else if(tag==1){ delBeanFlowable = rxJava.Del_Car(map); } }; public static RetrofitUtils getInstance(Map<String,String> map,String uri,int tag){ instance = new RetrofitUtils(map,uri,tag); return instance; } public Flowable<SelBean> getSelFlowable(){ return selBeanFlowable; } public Flowable<DelBean> getDelFlowable(){ return delBeanFlowable; } } package com.example.com.gouwuche.utils; import com.example.com.gouwuche.bean.AddBean; import com.example.com.gouwuche.bean.DelBean; import com.example.com.gouwuche.bean.SelBean; import java.util.Map; import io.reactivex.Flowable; import retrofit2.http.GET; import retrofit2.http.QueryMap; /** * author:Created by jiangkerun on 2018/4/23. */ public interface RxJava { @GET("product/getCarts") Flowable<SelBean> Sel_Car(@QueryMap Map<String, String> map); @GET("product/deleteCart") Flowable<DelBean> Del_Car(@QueryMap Map<String, String> map); @GET("product/addCart") Flowable<AddBean> Add_Car(@QueryMap Map<String, String> map); }
转载请注明原文地址: https://www.6miu.com/read-2250249.html

最新回复(0)