1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > Retrofit+Rxjava+MVP网络请求购物车

Retrofit+Rxjava+MVP网络请求购物车

时间:2024-03-10 13:30:17

相关推荐

Retrofit+Rxjava+MVP网络请求购物车

效果图:

首先先导依赖

//retrofit依赖compile 'com.squareup.retrofit2:converter-gson:2.3.0'//retrofit内部封装的GSONcompile 'io.reactivex.rxjava2:rxjava:2.1.7'compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'compile 'io.reactivex.rxjava2:rxandroid:2.0.1'compile 'com.facebook.fresco:fresco:0.12.0'compile 'com.facebook.fresco:animated-base-support:0.12.0'compile 'com.facebook.fresco:animated-webp:0.12.0'compile 'com.facebook.fresco:webpsupport:0.12.0'compile 'com.jakewharton:butterknife:6.1.0'compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'

网络权限

<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

在values文件里创建一个attrs

<resources><declare-styleable name="AddDeleteView"><attr name="left_item" format="string"></attr><attr name="right_item" format="string"></attr><attr name="middle_item" format="string"></attr></declare-styleable></resources>

住布局activity_main.xml

<LinearLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.retrofitdemo5.MainActivity"android:orientation="vertical"><RelativeLayout android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="10dp"><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:gravity="center"android:text="购物车" /><TextView android:id="@+id/tv_bianji"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:text="编辑" /></RelativeLayout><ExpandableListView android:id="@+id/exlist"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="1"></ExpandableListView><LinearLayout android:layout_width="match_parent"android:layout_height="60dp"android:orientation="horizontal"android:padding="10dp"><CheckBox android:id="@+id/check_all"android:layout_width="wrap_content"android:layout_height="match_parent"android:gravity="center_vertical"android:text="全选"android:textSize="15dp" /><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:gravity="center_vertical"android:text="总价:"android:textSize="15dp" /><TextView android:id="@+id/tv_zjprice"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:gravity="center_vertical"android:text="0"android:textSize="15dp" /><TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:gravity="center_vertical"android:text="数量:"android:textSize="15dp" /><TextView android:id="@+id/tv_count"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:layout_weight="1"android:gravity="center_vertical"android:text="0"android:textSize="15dp" /><Button android:id="@+id/btn_js"android:layout_width="80dp"android:layout_height="40dp"android:layout_gravity="right"android:background="#fc0109"android:text="结算"android:textSize="15dp" /></LinearLayout></LinearLayout>

加减器布局jiajianqi.xml

<LinearLayout xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><TextView android:id="@+id/tv_delete"android:layout_width="31dp"android:layout_height="31dp"android:background="#999999"android:gravity="center"android:text="-" /><EditText android:id="@+id/ed_num"android:layout_width="30dp"android:layout_height="30dp"android:background="@null"android:gravity="center" /><TextView android:id="@+id/tv_add"android:layout_width="31dp"android:layout_height="31dp"android:background="#999999"android:gravity="center"android:text="+" /></LinearLayout>

布局gowuc_goods_item.xml

注意事件 《com.example.retrofitdemo5.AddDeleteView 把你自己创建的AddDeleteView 类的com.example.retrofitdemo5复制过去

<LinearLayout xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"xmlns:app="/apk/res-auto"android:weightSum="1"><CheckBox android:id="@+id/check_gwc_goods"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_centerVertical="true"android:gravity="center_vertical" /><ImageView android:id="@+id/img_gwc_goods"android:layout_width="107dp"android:layout_height="80dp"android:src="@mipmap/ic_launcher" /><LinearLayout android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><TextView android:id="@+id/tv_gwc_goods"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="title" /><LinearLayout android:layout_width="298dp"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:orientation="horizontal"><TextViewandroid:id="@+id/tv_gwc_price"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="价格:"android:textColor="#fa0000" /></LinearLayout><RelativeLayout android:layout_width="match_parent"android:layout_height="wrap_content"><com.example.retrofitdemo5.AddDeleteViewandroid:id="@+id/add_delete"android:layout_width="100dp"android:layout_height="30dp"android:layout_centerVertical="true"android:layout_marginRight="5dp"app:left_item="-"app:middle_item="1"app:right_item="+"></com.example.retrofitdemo5.AddDeleteView><Buttonandroid:id="@+id/btn_gwc_delete"android:layout_width="110dp"android:layout_height="40dp"android:layout_alignParentRight="true"android:background="#f70496"android:gravity="center"android:text="删除"android:textColor="#ffffff" /></RelativeLayout></LinearLayout></LinearLayout>

布局gowuc_group_item.xml

<LinearLayout xmlns:android="/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><CheckBox android:id="@+id/check_gwc_group"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_centerVertical="true"android:layout_marginLeft="20dp" /><TextView android:id="@+id/tv_gwc_group"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="title"android:textSize="20dp" /></LinearLayout>

创建Bean包

CarBean 类

/*** msg : 请求成功* code : 0* data : [{"list":[{"bargainPrice":11800,"createtime":"-10-10T17:33:37","detailUrl":"https://item./product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg","num":2,"pid":57,"price":5199,"pscid":40,"selected":0,"sellerid":1,"subhead":"【i5 MX150 2G显存】全高清窄边框 8G内存 256固态硬盘 支持指纹识别 预装WIN10系统","title":"小米(MI)Air 13.3英寸全金属轻薄笔记本(i5-7200U 8G 256G PCle SSD MX150 2G独显 FHD 指纹识别 Win10)银\r\n"},{"bargainPrice":99,"createtime":"-10-14T21:38:26","detailUrl":"https://item./product/4345173.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"/n0/jfs/t6037/35/2944615848/95178/6cd6cff0/594a3a10Na4ec7f39.jpg!q70.jpg|/n0/jfs/t6607/258/1025744923/75738/da120a2d/594a3a12Ne3e6bc56.jpg!q70.jpg|/n0/jfs/t6370/292/1057025420/64655/f87644e3/594a3a12N5b900606.jpg!q70.jpg","num":6,"pid":45,"price":2999,"pscid":39,"selected":0,"sellerid":1,"subhead":"高清双摄,就是清晰!2000+1600万高清摄像头,6GB大内存+高通骁龙835处理器,性能怪兽!","title":"一加手机5 (A5000) 6GB+64GB 月岩灰 全网通 双卡双待 移动联通电信4G手机"}],"sellerName":"商家1","sellerid":"1"},{"list":[{"bargainPrice":11800,"createtime":"-10-14T21:38:26","detailUrl":"https://item./product/5025518.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"/n0/jfs/t8830/106/1760940277/195595/5cf9412f/59bf2ef5N5ab7dc16.jpg!q70.jpg|/n0/jfs/t5428/70/1520969931/274676/b644dd0d/591128e7Nd2f70da0.jpg!q70.jpg|/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 黑)"}],"sellerName":"商家2","sellerid":"2"},{"list":[{"bargainPrice":22.9,"createtime":"-10-03T23:53:28","detailUrl":"https://item./product/2542855.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"/n0/jfs/t1930/284/2865629620/390243/e3ade9c4/56f0a08fNbd3a1235.jpg!q70.jpg|/n0/jfs/t2137/336/2802996626/155915/e5e90d7a/56f0a09cN33e01bd0.jpg!q70.jpg|/n0/jfs/t1882/31/2772215910/389956/c8dbf370/56f0a0a2Na0c86ea6.jpg!q70.jpg|/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":111.99,"createtime":"-10-14T21:39:05","detailUrl":"https://item./product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":11,"pid":1,"price":118,"pscid":1,"selected":0,"sellerid":17,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家17","sellerid":"17"},{"list":[{"bargainPrice":111.99,"createtime":"-10-14T21:39:05","detailUrl":"https://item./product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":7,"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<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":11800,"createtime":"-10-10T17:33:37","detailUrl":"https://item./product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"/n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg","num":2,"pid":57,"price":5199,"pscid":40,"selected":0,"sellerid":1,"subhead":"【i5 MX150 2G显存】全高清窄边框 8G内存 256固态硬盘 支持指纹识别 预装WIN10系统","title":"小米(MI)Air 13.3英寸全金属轻薄笔记本(i5-7200U 8G 256G PCle SSD MX150 2G独显 FHD 指纹识别 Win10)银\r\n"},{"bargainPrice":99,"createtime":"-10-14T21:38:26","detailUrl":"https://item./product/4345173.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends","images":"/n0/jfs/t6037/35/2944615848/95178/6cd6cff0/594a3a10Na4ec7f39.jpg!q70.jpg|/n0/jfs/t6607/258/1025744923/75738/da120a2d/594a3a12Ne3e6bc56.jpg!q70.jpg|/n0/jfs/t6370/292/1057025420/64655/f87644e3/594a3a12N5b900606.jpg!q70.jpg","num":6,"pid":45,"price":2999,"pscid":39,"selected":0,"sellerid":1,"subhead":"高清双摄,就是清晰!2000+1600万高清摄像头,6GB大内存+高通骁龙835处理器,性能怪兽!","title":"一加手机5 (A5000) 6GB+64GB 月岩灰 全网通 双卡双待 移动联通电信4G手机"}]* 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 : 11800.0* createtime : -10-10T17:33:37* detailUrl : https://item./product/4338107.html?utm#_source=androidapp&utm#_medium=appshare&utm#_campaign=t#_335139774&utm#_term=QQfriends* images : /n0/jfs/t6700/155/2098998076/156185/6cf95035/595dd5a5Nc3a7dab5.jpg!q70.jpg* num : 2* pid : 57* price : 5199.0* pscid : 40* selected : 0* sellerid : 1* subhead : 【i5 MX150 2G显存】全高清窄边框 8G内存 256固态硬盘 支持指纹识别 预装WIN10系统* title : 小米(MI)Air 13.3英寸全金属轻薄笔记本(i5-7200U 8G 256G PCle SSD MX150 2G独显 FHD 指纹识别 Win10)银*/private double bargainPrice;private String createtime;private String detailUrl;private String images;private int num;private int pid;private double price;private int pscid;private int selected;private int sellerid;private String subhead;private String title;public double getBargainPrice() {return bargainPrice;}public void setBargainPrice(double bargainPrice) {this.bargainPrice = bargainPrice;}public String getCreatetime() {return createtime;}public void setCreatetime(String createtime) {this.createtime = createtime;}public String getDetailUrl() {return detailUrl;}public void setDetailUrl(String detailUrl) {this.detailUrl = detailUrl;}public String getImages() {return images;}public void setImages(String images) {this.images = images;}public int getNum() {return num;}public void setNum(int num) {this.num = num;}public int getPid() {return pid;}public void setPid(int pid) {this.pid = pid;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public int getPscid() {return pscid;}public void setPscid(int pscid) {this.pscid = pscid;}public int getSelected() {return selected;}public void setSelected(int selected) {this.selected = selected;}public int getSellerid() {return sellerid;}public void setSellerid(int sellerid) {this.sellerid = sellerid;}public String getSubhead() {return subhead;}public void setSubhead(String subhead) {this.subhead = subhead;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}}}

GoodsBean类

private boolean goodscheck;private double bargainPrice;private String images;private String title;private String subhead;private int num;private boolean btn;private int pid;public GoodsBean(boolean goodscheck, double bargainPrice, String images, String title, String subhead, int num, boolean btn) {this.goodscheck = goodscheck;this.bargainPrice = bargainPrice;this.images = images;this.title = title;this.subhead = subhead;this.num = num;this.btn = btn;}public GoodsBean(boolean goodscheck, double bargainPrice, String images, String title, String subhead, int num, int pid) {this.goodscheck = goodscheck;this.bargainPrice = bargainPrice;this.images = images;this.title = title;this.subhead = subhead;this.num = num;this.pid = pid;}public int getPid() {return pid;}public void setPid(int pid) {this.pid = pid;}public boolean isBtn() {return btn;}public void setBtn(boolean btn) {this.btn = btn;}public int getNum() {return num;}public void setNum(int num) {this.num = num;}public boolean isGoodscheck() {return goodscheck;}public void setGoodscheck(boolean goodscheck) {this.goodscheck = goodscheck;}public double getBargainPrice() {return bargainPrice;}public void setBargainPrice(double bargainPrice) {this.bargainPrice = bargainPrice;}public String getImages() {return images;}public void setImages(String images) {this.images = images;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getSubhead() {return subhead;}public void setSubhead(String subhead) {this.subhead = subhead;}@Overridepublic String toString() {return "GoodsBean{" +"goodscheck=" + goodscheck +", bargainPrice=" + bargainPrice +", images='" + images + '\'' +", title='" + title + '\'' +", subhead='" + subhead + '\'' +", num=" + num +", btn=" + btn +'}';}

GroupBean类

private boolean groupcheck;private String sellerName;private String sellerid;public GroupBean(boolean groupcheck, String sellerName, String sellerid) {this.groupcheck = groupcheck;this.sellerName = sellerName;this.sellerid = sellerid;}public boolean isGroupcheck() {return groupcheck;}public void setGroupcheck(boolean groupcheck) {this.groupcheck = groupcheck;}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;}@Overridepublic String toString() {return "GroupBean{" +"groupcheck=" + groupcheck +", sellerName='" + sellerName + '\'' +", sellerid=" + sellerid +'}';}

view包里

CarView类

public interface CarView extends IView{void CarSuccess(CarBean carBean);void CarFaild(Throwable throwable);}

IView类

public interface IView {}

utils包

RetrofitManager类

public class RetrofitManager {private static final String Url="http://120.27.23.105/";private static Retrofit mRetrofit;private static class Car_Holder{private static final RetrofitManager instance=new RetrofitManager(Url);}public static RetrofitManager getinstance() {return Car_Holder.instance;}public RetrofitManager(String url) {mRetrofit= buildRetrofit();}//OkHttpClientprivate OkHttpClient buildOkHttpClient(){return new OkHttpClient.Builder().connectTimeout(10000, TimeUnit.MILLISECONDS).build();}//Retrofitprivate Retrofit buildRetrofit(){return new Retrofit.Builder().client(buildOkHttpClient()).baseUrl(Url).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJava2CallAdapterFactory.create()).build();}public <T> T create(Class<T> tClass){return mRetrofit.create(tClass);}}

AddDeleteView类

public class AddDeleteView extends LinearLayout {private OnAddDelClickListener listener;private EditText etNumber;//对外提供一个点击的回调接口public interface OnAddDelClickListener {void onAddClick(View v);void onDelClick(View v);}public void setOnAddDelClickListener(OnAddDelClickListener listener) {if (listener != null) {this.listener = listener;}}public AddDeleteView(Context context) {this(context, null);}public AddDeleteView(Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);}public AddDeleteView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);initView(context, attrs, defStyleAttr);}private void initView(Context context, AttributeSet attrs, int defStyleAttr) {View.inflate(context, R.layout.adddelete, this);TextView txtDelete = (TextView) findViewById(R.id.tv_delete);TextView txtAdd = (TextView) findViewById(R.id.tv_add);etNumber = (EditText) findViewById(R.id.ed_num);TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AddDeleteView);String leftText = typedArray.getString(R.styleable.AddDeleteView_left_item);String rightText = typedArray.getString(R.styleable.AddDeleteView_right_item);String middleText = typedArray.getString(R.styleable.AddDeleteView_middle_item);txtDelete.setText(leftText);txtAdd.setText(rightText);etNumber.setText(middleText);//回收typedArray.recycle();txtDelete.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View view) {listener.onDelClick(view);}});txtAdd.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View view) {listener.onAddClick(view);}});}//对外提供一个修改数字的方法public void setNumber(int number) {if (number > 0) {etNumber.setText(number + "");}}//对外提供一个获取当前数字的方法public int getNumber() {String string = etNumber.getText().toString();int i = Integer.parseInt(string);return i;}}

CarApi类

public interface CarApi {@GET("product/getCarts")Observable<CarBean> selectcar(@Query("uid") String uid);}

MyApp类

public class MyApp extends Application {@Overridepublic void onCreate() {super.onCreate();Fresco.initialize(this);}}

model包

CarModel类

public class CarModel implements IModel {//查询购物车public Observable<CarBean> selectcar(){CarApi carApi = RetrofitManager.getinstance().create(CarApi.class);return carApi.selectcar("1600");}}

IModel类

public interface IModel {}

adapter包

CarAdapter类

public class CarAdapter extends BaseExpandableListAdapter {Context context;List<GroupBean> grouplist;ArrayList<ArrayList<GoodsBean>> goodslist;MainActivity carActivity;public CarAdapter(Context context, List<GroupBean> grouplist, ArrayList<ArrayList<GoodsBean>> goodslist, MainActivity carActivity) {this.context = context;this.grouplist = grouplist;this.goodslist = goodslist;this.carActivity = carActivity;}@Overridepublic int getGroupCount() {return grouplist.size();}@Overridepublic int getChildrenCount(int i) {return goodslist.get(i).size();}@Overridepublic Object getGroup(int i) {return grouplist.get(i);}@Overridepublic Object getChild(int i, int i1) {return goodslist.get(i).get(i1);}@Overridepublic long getGroupId(int i) {return i;}@Overridepublic long getChildId(int i, int i1) {return i1;}@Overridepublic boolean hasStableIds() {return false;}//父布局@Overridepublic View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {View group_view = View.inflate(context, R.layout.gwc_group_item, null);final CheckBox check_group = group_view.findViewById(R.id.check_gwc_group);TextView group = group_view.findViewById(R.id.tv_gwc_group);check_group.setChecked(grouplist.get(i).isGroupcheck());group.setText(grouplist.get(i).getSellerName());check_group.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {GroupBean groupBean = grouplist.get(i);groupBean.setGroupcheck(check_group.isChecked());for (int j = 0; j <grouplist.size() ; j++) {boolean groupcheck = grouplist.get(j).isGroupcheck();if (!groupcheck){carActivity.checkAll.setChecked(false);break;}else {carActivity.checkAll.setChecked(true);}}ArrayList<GoodsBean> goodsBeen = goodslist.get(i);for (int i = 0; i <goodsBeen.size() ; i++) {goodsBeen.get(i).setGoodscheck(check_group.isChecked());}//计算价格carActivity.changesum(goodslist);notifyDataSetChanged();}});return group_view;}//子布局@Overridepublic View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {View view = View.inflate(context, R.layout.gwc_goods_item, null);TextView tv_goods = (TextView) view.findViewById(R.id.tv_gwc_goods);TextView price = (TextView) view.findViewById(R.id.tv_gwc_price);final CheckBox check_goods = (CheckBox) view.findViewById(R.id.check_gwc_goods);ImageView img_goods = (ImageView) view.findViewById(R.id.img_gwc_goods);Button btn_delete = (Button) view.findViewById(R.id.btn_gwc_delete);final AddDeleteView adv = (AddDeleteView) view.findViewById(R.id.add_delete);tv_goods.setText(goodslist.get(groupPosition).get(childPosition).getTitle());price.setText(goodslist.get(groupPosition).get(childPosition).getBargainPrice() + "");check_goods.setChecked(goodslist.get(groupPosition).get(childPosition).isGoodscheck());String images = goodslist.get(groupPosition).get(childPosition).getImages();String[] split = images.split("\\|");Glide.with(img_goods.getContext()).load(split[0]).into(img_goods);adv.setNumber(goodslist.get(groupPosition).get(childPosition).getNum());adv.setNumber(goodslist.get(groupPosition).get(childPosition).getNum());if (goodslist.get(groupPosition).get(childPosition).isBtn()) {btn_delete.setVisibility(View.VISIBLE);} else {btn_delete.setVisibility(View.INVISIBLE);}adv.setOnAddDelClickListener(new AddDeleteView.OnAddDelClickListener() {@Overridepublic void onAddClick(View v) {int number = adv.getNumber();number++;adv.setNumber(number);goodslist.get(groupPosition).get(childPosition).setNum(number);carActivity.changesum(goodslist);}@Overridepublic void onDelClick(View v) {int number = adv.getNumber();if (number > 1) {number--;}adv.setNumber(number);goodslist.get(groupPosition).get(childPosition).setNum(number);carActivity.changesum(goodslist);}});btn_delete.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {carActivity.deleteShop(goodslist.get(groupPosition).get(childPosition).getPid());int size = goodslist.get(groupPosition).size();if (goodslist.get(groupPosition).get(childPosition).isGoodscheck()) {if (size == 1) {goodslist.remove(groupPosition);grouplist.remove(groupPosition);} else {goodslist.get(groupPosition).remove(childPosition);}carActivity.changesum(goodslist);notifyDataSetChanged();} else {Toast.makeText(context, "请选择商品。。。", Toast.LENGTH_SHORT).show();}}});check_goods.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//定义一个默认flagboolean flag = false;//获得当前二级列表复选框的状态boolean cchecked = check_goods.isChecked();//把当前状态存储到二级列表List集合里面goodslist.get(groupPosition).get(childPosition).setGoodscheck(cchecked);//循环遍历二级列表List集合for (List<GoodsBean> i1 : goodslist) {// 按照所有二级列表的item的个数进行循环遍历for (int r = 0; r < i1.size(); r++) {//获取当前二级列表的状态boolean childCb1 = i1.get(r).isGoodscheck();//取反设置 如果为true改为false 如果为false改为trueif (!childCb1) {//如果当前二级列表中有一条复选框是false 全选为falsecarActivity.checkAll.setChecked(false);//如果当前二级列表中有一条复选框是false 一级列表为falsegrouplist.get(groupPosition).setGroupcheck(false);//flag为true跳出循环flag = true;break;} else {//如果所有的二级列表都为true 全选为truecarActivity.checkAll.setChecked(true);//如果所有的二级列表都为true 一级列表为truegrouplist.get(groupPosition).setGroupcheck(true);}}//falg为true时跳出循环if (flag) {break;}}//当前二级列表的总长度int size = goodslist.get(groupPosition).size();//按照当前二级列表的总长度循环for (int x = 0; x < size; x++) {//获得当前二级列表中每一个item的选中状态boolean childCb1 = goodslist.get(groupPosition).get(x).isGoodscheck();//判断if (!childCb1) {//有一个flase 一级列表就设置false 跳出循环grouplist.get(groupPosition).setGroupcheck(false);break;} else {grouplist.get(groupPosition).setGroupcheck(true);}}//计算价格carActivity.changesum(goodslist);//刷新适配器notifyDataSetChanged();}});return view;}@Overridepublic boolean isChildSelectable(int i, int i1) {return true;}}

presenter包

CarPresenter类

public class CarPresenter extends IPresenter<CarView>{private CarModel carModel;public CarPresenter(CarView view) {super(view);}@Overrideprotected void inti() {carModel=new CarModel();}//查询购物车public void selectcar(){Observable<CarBean> selectcar = carModel.selectcar();selectcar//在io子线程联网.subscribeOn(Schedulers.io())//在主线程更新UI.observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<CarBean>() {@Overridepublic void accept(CarBean carBean) throws Exception {view.CarSuccess(carBean);}}, new Consumer<Throwable>() {@Overridepublic void accept(Throwable throwable) throws Exception {view.CarFaild(throwable);}});}}

IPresenter类

public abstract class IPresenter<T extends IView> {protected T view;public IPresenter(T view) {this.view = view;inti();}protected abstract void inti();}

MainActivity主页面

public class MainActivity extends AppCompatActivity implements CarView{@InjectView(R.id.tv_bianji)TextView tvBianji;@InjectView(R.id.exlist)ExpandableListView exlist;@InjectView(R.id.check_all)public CheckBox checkAll;@InjectView(R.id.tv_zjprice)TextView price;TextView tvZjprice;@InjectView(R.id.tv_count)TextView counts;TextView tvCount;@InjectView(R.id.btn_js)Button btnJs;private CarPresenter carPresenter;private boolean flagedit = true;ArrayList<GroupBean> groupBeen = new ArrayList<>();ArrayList<ArrayList<GoodsBean>> goods = new ArrayList<>();private CarAdapter carAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ButterKnife.inject(this);carPresenter = new CarPresenter(this);carPresenter.selectcar();carAdapter = new CarAdapter(this, groupBeen, goods, this);exlist.setAdapter(carAdapter);for (int i = 0; i < carAdapter.getGroupCount(); i++) {exlist.expandGroup(i);}carAdapter.notifyDataSetChanged();}@OnClick({R.id.tv_bianji, R.id.check_all, R.id.btn_js})public void onViewClicked(View view) {switch (view.getId()) {case R.id.tv_bianji:String trim = tvBianji.getText().toString().trim();if (trim.equals("编辑")) {tvBianji.setText("完成");} else {tvBianji.setText("编辑");}for (List<GoodsBean> i1 : goods) {for (int r = 0; r < i1.size(); r++) {i1.get(r).setBtn(flagedit);}}flagedit = !flagedit;carAdapter.notifyDataSetChanged();break;case R.id.check_all:boolean checked = checkAll.isChecked();for (int i = 0; i < groupBeen.size(); i++) {groupBeen.get(i).setGroupcheck(checked);}for (int q = 0; q < goods.size(); q++) {ArrayList<GoodsBean> goodsBeen = goods.get(q);for (int j = 0; j < goodsBeen.size(); j++) {goodsBeen.get(j).setGoodscheck(checked);}}changesum(goods);carAdapter.notifyDataSetChanged();break;case R.id.btn_js:int index = 0;for (int q = 0; q < goods.size(); q++) {ArrayList<GoodsBean> goodsBeen = goods.get(q);for (int j = 0; j < goodsBeen.size(); j++) {boolean goodscheck = goodsBeen.get(j).isGoodscheck();if (goodscheck) {index++;}}}if (index == 0) {Toast.makeText(this, "请选择商品,谢谢", Toast.LENGTH_SHORT).show();} else {Toast.makeText(this, "钱就是另一回事了", Toast.LENGTH_SHORT).show();}break;}}@Overridepublic void CarSuccess(CarBean carBean) {List<CarBean.DataBean> data = carBean.getData();for (int i = 0; i < data.size(); i++) {groupBeen.add(new GroupBean(false, data.get(i).getSellerName(), data.get(i).getSellerid()));List<CarBean.DataBean.ListBean> list = data.get(i).getList();ArrayList<GoodsBean> goodsBeen = new ArrayList<>();for (int j = 0; j < list.size(); j++) {goodsBeen.add(new GoodsBean(false, list.get(j).getBargainPrice(), list.get(j).getImages(), list.get(j).getTitle(), list.get(j).getSubhead(), list.get(j).getNum(), list.get(j).getPid()));}goods.add(goodsBeen);}for (int i = 0; i < carAdapter.getGroupCount(); i++) {exlist.expandGroup(i);}carAdapter.notifyDataSetChanged();}@Overridepublic void CarFaild(Throwable throwable) {}DecimalFormat df = new DecimalFormat("######0.00");public void changesum(ArrayList<ArrayList<GoodsBean>> childBeen) {int count = 0;double sum = 0;for (List<GoodsBean> i1 : childBeen) {for (int r = 0; r < i1.size(); r++) {boolean childCb1 = i1.get(r).isGoodscheck();if (childCb1) {double price = i1.get(r).getBargainPrice();int num = i1.get(r).getNum();sum += price * num;count++;}}}price.setText("¥" + df.format(sum));counts.setText(count + "");}public void deleteShop(int pid) {Toast.makeText(this, "aadas", Toast.LENGTH_SHORT).show();}}

希望进来看的能够给你们帮助,你要是有更好的方法,可以发信息给我。互相可以探讨一下。我这代码只是给你们参考。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。