RXjava+Retrofit二级购物车
16lz
2021-01-26
//依赖
apply plugin: 'com.android.application'android { compileSdkVersion 26 buildToolsVersion "26.0.1" defaultConfig { applicationId "com.example.a123.zhoukao2" minSdkVersion 15 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }}dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' compile 'io.reactivex.rxjava2:rxjava:2.1.1' compile 'io.reactivex.rxjava2:rxandroid:2.0.1' compile 'com.squareup.retrofit2:retrofit:2.0.0' compile 'com.squareup.retrofit2:converter-gson:2.0.2'//解析使用 compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'//与RxJava结合时使用 compile 'com.android.support:recyclerview-v7:26.+' compile 'com.facebook.fresco:fresco:1.3.0' compile 'com.squareup.okhttp3:okhttp:3.9.0'}
//AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.a123.zhoukao2"> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <application android:name=".App" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> intent-filter> activity> application>manifest>
//activity_main.xml
<?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.a123.zhoukao2.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_above="@+id/linearLayout"> <ExpandableListView android:layout_width="match_parent" android:layout_height="match_parent" android:groupIndicator="@null" android:id="@+id/ev"> ExpandableListView> LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:id="@+id/linearLayout"> <CheckBox android:layout_width="0dp" android:text="全选" android:layout_weight="1" android:checked="true" android:layout_height="wrap_content" android:id="@+id/cb_all"/> <TextView android:layout_width="0dp" android:gravity="right" android:layout_weight="1" android:layout_height="wrap_content" android:text="总价:" /> <TextView android:layout_width="0dp" android:layout_marginRight="10dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="0.0" android:id="@+id/text_sum"/> <Button android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="删除" android:id="@+id/btn_del"/> LinearLayout>RelativeLayout>
//view_add.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:src="@drawable/down" android:layout_width="25dp" android:layout_height="25dp" android:id="@+id/delete"/> <View android:id="@+id/view" android:layout_width="20dp" android:layout_height="1dp" android:layout_marginTop="2dp" android:background="#5c595c" android:layout_toRightOf="@id/delete"/> <TextView android:id="@+id/count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" android:layout_toRightOf="@id/delete" android:layout_marginTop="5dp" android:layout_marginLeft="5dp"/> <View android:layout_width="20dp" android:layout_height="1dp" android:background="#5c595c" android:layout_toRightOf="@id/delete" android:layout_marginTop="23dp"/> <ImageView android:id="@+id/add" android:src="@drawable/up" android:layout_width="25dp" android:layout_height="25dp" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/view" android:layout_toEndOf="@+id/view" />RelativeLayout>
//view_child.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" xmlns:Fresco="http://schemas.android.com/apk/res-auto"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <CheckBox android:id="@+id/cb_child" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="35dp" /> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/child_img" android:layout_width="100dp" android:layout_height="100dp" Fresco:placeholderImage="@mipmap/ic_launcher_round" Fresco:failureImage="@mipmap/ic_launcher_round" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:text="xxx" android:id="@+id/child_tit"/> <LinearLayout android:layout_marginTop="10dp" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="xxx" android:id="@+id/child_price"/> <com.example.a123.zhoukao2.view.AddView android:id="@+id/addview" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"> com.example.a123.zhoukao2.view.AddView> LinearLayout> LinearLayout> LinearLayout>LinearLayout>
//view_father.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:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:id="@+id/cb_father_car"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="xxx" android:id="@+id/title_father_car"/> LinearLayout>LinearLayout>
//CarAdapter
package com.example.a123.zhoukao2.adaper;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseExpandableListAdapter;import android.widget.CheckBox;import android.widget.TextView;import android.widget.Toast;import com.example.a123.zhoukao2.MainActivity;import com.example.a123.zhoukao2.R;import com.example.a123.zhoukao2.bean.childBean;import com.example.a123.zhoukao2.bean.fatherBean;import com.example.a123.zhoukao2.view.AddView;import com.facebook.drawee.view.SimpleDraweeView;import java.util.List;/** * Created by 123 on 2018/1/6. */public class CarAdapter extends BaseExpandableListAdapter { private Context context; private List list; private List> lists; private MainActivity f; public CarAdapter(Context context, List list, List> lists,MainActivity f) { this.context = context; this.list = list; this.lists = lists; this.f=f; } @Override public int getGroupCount() { return list.size(); } @Override public int getChildrenCount(int i) { return lists.get(i).size(); } @Override public Object getGroup(int i) { return list.get(i); } @Override public Object getChild(int i, int i1) { return lists.get(i).get(i1); } @Override public long getGroupId(int i) { return i; } @Override public long getChildId(int i, int i1) { return i1; } @Override public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) { final fatherBean fatherbean = list.get(i); final List childbeen = lists.get(i); view = View.inflate(this.context, R.layout.view_father, null); TextView textView = (TextView) view.findViewById(R.id.title_father_car); final CheckBox checkBox = (CheckBox) view.findViewById(R.id.cb_father_car); textView.setText(fatherbean.getName()); checkBox.setChecked(fatherbean.isflag()); //复选框点击事件 checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { boolean checked = checkBox.isChecked(); fatherbean.setIsflag(checked); for (int j=0;j//计算总价方法 f.mouth(f); boolean setcheck = f.setcheck(); f.cb.setChecked(setcheck); notifyDataSetChanged(); } }); return view; } @Override public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) { final childBean bean = lists.get(i).get(i1); //加载布局 view= View.inflate(context, R.layout.view_child,null); //获取构件 SimpleDraweeView img = (SimpleDraweeView) view.findViewById(R.id.child_img); TextView title = (TextView) view.findViewById(R.id.child_tit); AddView addView = (AddView) view.findViewById(R.id.addview); final TextView price = (TextView) view.findViewById(R.id.child_price); final CheckBox checkBox = (CheckBox) view.findViewById(R.id.cb_child); //修改内容 addView.setNumber(bean.getNum()); price.setText(bean.getPrice()+""); title.setText(bean.getName()); checkBox.setChecked(lists.get(i).get(i1).isflag()); img.setImageURI(bean.getImgurl()); //加减器点击事件 addView.setOnAddDeleteClick(new AddView.OnAddDeleteClickListener() { @Override public void onAddClick(View v) { int num = bean.getNum(); num++; bean.setNum(num); //计算总价方法 f.mouth(f); notifyDataSetChanged(); } @Override public void onDelClick(View v) { int num = bean.getNum(); if (num==1){ Toast.makeText(context,"数量不能小于1",Toast.LENGTH_SHORT).show(); } if(num>1){ num--; } bean.setNum(num); //计算总价方法 f.mouth(f); notifyDataSetChanged(); } }); checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { boolean checked = checkBox.isChecked(); lists.get(i).get(i1).setIsflag(checked); boolean b=true; if (checked){ for (int j=0;jboolean flag = lists.get(i).get(j).isflag(); if (flag==false){ b=false; } } //修改父级的选中状态 if (b){ list.get(i).setIsflag(true); } }else{ list.get(i).setIsflag(false); } //计算总价方法 f.mouth(f); boolean setcheck = f.setcheck(); f.cb.setChecked(setcheck); notifyDataSetChanged(); } }); return view; } @Override public boolean isChildSelectable(int i, int i1) { return true; } @Override public boolean hasStableIds() { return true; }}
//CarBean
package com.example.a123.zhoukao2.bean;import java.util.List;/** * Created by 123 on 2018/1/6. */public class CarBean { /** * msg : 请求成功 * code : 0 * data : [{"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":4,"pid":2,"price":299,"pscid":1,"selected":0,"sellerid":18,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家18","sellerid":"18"}] */ private String msg; private String code; private List data; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public List getData() { return data; } public void setData(List data) { this.data = data; } public static class DataBean { /** * 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 */ private String sellerName; private String sellerid; private List list; public String getSellerName() { return sellerName; } public void setSellerName(String sellerName) { this.sellerName = sellerName; } public String getSellerid() { return sellerid; } public void setSellerid(String sellerid) { this.sellerid = sellerid; } public List getList() { return list; } public void setList(List list) { this.list = list; } public static class ListBean { /** * 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.0 * pscid : 1 * selected : 0 * sellerid : 17 * 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; 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; } } }}
//childBean
package com.example.a123.zhoukao2.bean;/** * Created by 123 on 2018/1/6. */public class childBean { private String name; private String imgurl; private String pid ; private boolean isflag; private double price; private int num; public childBean(String name, String imgurl, String pid, double price, boolean isflag,int num) { this.name = name; this.imgurl = imgurl; this.pid = pid; this.isflag = isflag; this.price = price; this.num=num; } 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 boolean isflag() { return isflag; } public void setIsflag(boolean isflag) { this.isflag = isflag; } public childBean(String name, String imgurl, String pid) { this.name = name; this.imgurl = imgurl; this.pid = pid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getImgurl() { return imgurl; } public void setImgurl(String imgurl) { this.imgurl = imgurl; } public String getPid() { return pid; } public void setPid(String pid) { this.pid = pid; }}
//delBean
package com.example.a123.zhoukao2.bean;/** * Created by 123 on 2018/1/6. */public class delBean { /** * msg : 加购成功 * code : 0 */ private String msg; private String code; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; }}
//fatherBean
package com.example.a123.zhoukao2.bean;/** * Created by 123 on 2018/1/6. */public class fatherBean { private String name; private boolean isflag; public fatherBean(String name, boolean isflag) { this.name = name; this.isflag = isflag; } public boolean isflag() { return isflag; } public void setIsflag(boolean isflag) { this.isflag = isflag; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
//ApiService
package com.example.a123.zhoukao2.model;import com.example.a123.zhoukao2.bean.CarBean;import com.example.a123.zhoukao2.bean.delBean;import java.util.Map;import io.reactivex.Flowable;import retrofit2.http.GET;import retrofit2.http.QueryMap;/** * Created by 123 on 2018/1/6. */public interface ApiService { //http://120.27.23.105/product/getCarts 购物车 @GET("product/getCarts") Flowable getcars(@QueryMap Map map); //http://120.27.23.105/product/deleteCart 删除 @GET("product/deleteCart") Flowable delete(@QueryMap Map map);}
//IModel
package com.example.a123.zhoukao2.model;import java.util.Map;/** * Created by 123 on 2018/1/6. */public interface IModel { void get(String baseurl, Map map, String tag);}
//Model
package com.example.a123.zhoukao2.model;import com.example.a123.zhoukao2.bean.CarBean;import com.example.a123.zhoukao2.bean.delBean;import com.example.a123.zhoukao2.presenter.MyPresenter;import java.util.Map;import io.reactivex.Flowable;/** * Created by 123 on 2018/1/6. */public class Model implements IModel { private MyPresenter presenter; public Model(MyPresenter presenter) { this.presenter = presenter; } @Override public void get(String baseurl, Map map, String tag) { ApiService service = RetrofitUtils.getInstance(baseurl).getretrofit().create(ApiService.class); if (tag.equals("getcars")){//购物车 Flowable flowable = service.getcars(map); presenter.getData(flowable,tag); }else if (tag.equals("del")){//删除 Flowable flowable = service.delete(map); presenter.getData(flowable,tag); } }}
//RetrofitUtils
package com.example.a123.zhoukao2.model;import retrofit2.Retrofit;import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;import retrofit2.converter.gson.GsonConverterFactory;/** * Created by 123 on 2018/1/6. */public class RetrofitUtils { private static volatile RetrofitUtils instance; private final Retrofit retrofit; private RetrofitUtils(String baseurl) { retrofit = new Retrofit.Builder() .baseUrl(baseurl) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build(); } public static RetrofitUtils getInstance(String baseurl) { if (instance == null) { synchronized (RetrofitUtils.class) { if (instance == null) { instance = new RetrofitUtils(baseurl); } } } return instance; } public Retrofit getretrofit(){ return retrofit; }}
//IPresenter
package com.example.a123.zhoukao2.presenter;import java.util.Map;/** * Created by 123 on 2018/1/6. */public interface IPresenter { void get(String baseurl, Map map, String tag);}
//MyPresenter
package com.example.a123.zhoukao2.presenter;import com.example.a123.zhoukao2.model.Model;import com.example.a123.zhoukao2.view.IView;import java.util.Map;import io.reactivex.Flowable;import io.reactivex.android.schedulers.AndroidSchedulers;import io.reactivex.schedulers.Schedulers;import io.reactivex.subscribers.DisposableSubscriber;/** * Created by 123 on 2018/1/6. */public class MyPresenter implements IPresenter{ private IView iv; private DisposableSubscriber subscriber; public MyPresenter(IView iv) { this.iv = iv; } @Override public void get(String baseurl, Map map, String tag) { Model model = new Model(this); model.get(baseurl,map,tag); } public void getData(Flowable flowable, final String tag){ subscriber = (DisposableSubscriber) flowable.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(new DisposableSubscriber() { @Override public void onNext(Object o) { iv.onSuccess(o,tag); } @Override public void onError(Throwable t) { iv.onFailed((Exception) t); } @Override public void onComplete() { } }); } //防止内存泄漏 public void detatch(){ if (iv != null) { iv = null; } if(subscriber !=null){ if(!subscriber.isDisposed()){ subscriber.dispose(); } } }}
//App
package com.example.a123.zhoukao2;import android.app.Application;import com.facebook.drawee.backends.pipeline.Fresco;/** * Created by 123 on 2018/1/6. */public class App extends Application{ @Override public void onCreate() { super.onCreate(); Fresco.initialize(this); }}
//MainActivity
package com.example.a123.zhoukao2;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.CheckBox;import android.widget.ExpandableListView;import android.widget.TextView;import com.example.a123.zhoukao2.adaper.CarAdapter;import com.example.a123.zhoukao2.bean.CarBean;import com.example.a123.zhoukao2.bean.childBean;import com.example.a123.zhoukao2.bean.delBean;import com.example.a123.zhoukao2.bean.fatherBean;import com.example.a123.zhoukao2.presenter.MyPresenter;import com.example.a123.zhoukao2.view.IView;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class MainActivity extends AppCompatActivity implements IView { private ExpandableListView ev; public CheckBox cb; public TextView sum; private Button delete; private MyPresenter presenter; private List list; private List> lists; private CarAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ev = (ExpandableListView) findViewById(R.id.ev); cb = (CheckBox) findViewById(R.id.cb_all); sum = (TextView) findViewById(R.id.text_sum); delete = (Button) findViewById(R.id.btn_del); list=new ArrayList<>(); lists=new ArrayList<>(); //新建适配器 adapter = new CarAdapter(this, list, lists,this); ev.setAdapter(adapter); //全选按钮点击事件 cb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { for(int i=0;ifor(int i=0;ifor(int j=0;jint n=0; for (int j=0;j beanList = lists.get(j); for (int k=0;kif(beanList.get(k).isflag()){ int num = beanList.get(k).getNum(); double price1 = beanList.get(k).getPrice(); n+=price1*num; } } } sum.setText("总价为:"+n); //刷新适配器 adapter.notifyDataSetChanged(); } }); //删除按钮点击事件 delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { for(int i=0;i beanList = lists.get(i); for (int j=0;jif (bean.isflag()){ String pid = bean.getPid(); lists.get(i).remove(j); Map map1=new HashMap(); map1.put("uid","4479"); map1.put("pid",pid); presenter.get("http://120.27.23.105/",map1,"del"); mouth(MainActivity.this); } } if (lists.get(i).size()==0){ lists.remove(i); list.remove(i); i--; } } adapter.notifyDataSetChanged(); } }); presenter = new MyPresenter(this); Map map=new HashMap<>(); map.put("source","android"); map.put("uid","10985"); presenter.get("http://120.27.23.105/",map,"getcars"); } //判断是否全选 public boolean setcheck(){ boolean b=true; for (int i=0;iif (!list.get(i).isflag()){ b=list.get(i).isflag(); break; } List beanList = lists.get(i); for(int j=0;jif (!beanList.get(j).isflag()){ b=beanList.get(j).isflag(); break; } } } return b; } //计算总价的方法 public void mouth(MainActivity f){ float n=0; for (int j=0;j beanList = lists.get(j); for (int k=0;kif(beanList.get(k).isflag()){ int num = beanList.get(k).getNum(); double price1 = beanList.get(k).getPrice(); n+=price1*num; } } } f.sum.setText(""+n); } @Override public void onSuccess(Object o, String tag) { if (tag.equals("getcars")){ if (o!=null){ CarBean a = (CarBean)o; List beanList = a.getData(); //建立数据源 for(int i=0;inew fatherBean(name,true)); List listBeen = beanList.get(i).getList(); List l=new ArrayList<>(); for(int j=0;jdouble price = listBeen.get(j).getPrice(); String s = listBeen.get(j).getImages(); String[] strings = s.split("!"); String title = listBeen.get(j).getTitle(); int id = listBeen.get(j).getPid(); int num = listBeen.get(j).getNum(); l.add(new childBean(title,strings[0],id+"",price,true,num)); } lists.add(l); } //默认展开 for(int s = 0; s < adapter.getGroupCount(); s++){ ev.expandGroup(s); } mouth(this); //刷新适配器 adapter.notifyDataSetChanged(); } }else if(tag.equals("del")){ delBean bean = (delBean) o; } } @Override public void onFailed(Exception e) { }}
//AddView
package com.example.a123.zhoukao2.view;import android.content.Context;import android.util.AttributeSet;import android.view.View;import android.widget.ImageView;import android.widget.RelativeLayout;import android.widget.TextView;import com.example.a123.zhoukao2.R;/** * Created by 123 on 2018/1/6. */public class AddView extends RelativeLayout { private TextView count; public AddView(Context context) { this(context,null); } public AddView(Context context, AttributeSet attrs) { this(context, attrs,0); } public AddView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context, attrs, defStyleAttr); } private OnAddDeleteClickListener listener; //对外提供一个点击的回调接口 public interface OnAddDeleteClickListener{ void onAddClick(View v); void onDelClick(View v); } public void setOnAddDeleteClick(OnAddDeleteClickListener listener){ this.listener = listener; } private void initView(final Context context, AttributeSet attrs, int defStyleAttr) { View view = View.inflate(context, R.layout.view_add, this); ImageView delete = (ImageView)view.findViewById(R.id.delete); count = (TextView)view.findViewById(R.id.count); ImageView add = (ImageView)view.findViewById(R.id.add); delete.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { listener.onDelClick(view); } }); add.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { listener.onAddClick(view); } }); } //对外提供设置EditText值的方法 public void setNumber(int number){ count.setText(number + ""); } //得到控件原来的值,并转成int类型 public int getNumber(){ int number = Integer.parseInt(count.getText().toString().trim()); return number; }}
//IView
package com.example.a123.zhoukao2.view;/** * Created by 123 on 2018/1/6. */public interface IView { void onSuccess(Object o, String tag); void onFailed(Exception e);}
更多相关文章
- Android(安卓)EditText简单实现右侧点击清除
- Android(安卓)获取手机应用信息
- 关于Listview的一个没有notifyDataSetChanged导致的错误
- Robotium 常用方法
- Android上下文Context全解析
- Android:UI更新方法二:View.postInvalidate+Thread+Runnable
- 运用BitmapFactory.Options来改善程序加载图片效率和避免内存溢
- Android(安卓)ViewPager动画切换
- 怎样实现android http-post方法