MVP实现二级购物车

来源:互联网 发布:淘宝店的货源哪里来 编辑:程序博客网 时间:2024/05/22 00:29

效果图 

使用接口:http://120.27.23.105/product/getCarts?uid=100


导入依赖

[html] view plain copy
  1. compile 'com.squareup.okhttp3:okhttp:3.9.0'  
  2.    compile 'com.google.code.gson:gson:2.8.2'  
  3.    compile 'com.android.support:recyclerview-v7:25.3.1'  
  4.    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'  
清单文件中加入权限
[html] view plain copy
  1. <uses-permission android:name="android.permission.INTERNET"/>  
配置application的name属性,ImageLoader初始化
[html] view plain copy
  1. <application  
  2.     android:name=".appli.App"  
  3. </application>  
[html] view plain copy
  1. public class App extends Application{  
  2.     @Override  
  3.     public void onCreate() {  
  4.         super.onCreate();  
  5.         ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this).build();  
  6.         ImageLoader.getInstance().init(configuration);  
  7.     }  
  8. }  

布局中需要用到的的布局,在drawable下面新建qujiesuan.xml

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <corners android:radius="200dp"/>  
  5.     <solid android:color="#e53e42"/>  
  6.     <size android:height="60dp" android:width="130dp"/>  
  7. </shape>  
drawable下面还需要加入三张图片


接下来就是activity_main.xml里面的布局,上面是recyclerView下面是一系列的功能


activity_main.xml

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical"  
  7.     >  
  8.   
  9.     <android.support.v7.widget.RecyclerView  
  10.         android:id="@+id/recycler_View"  
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="0dp"  
  13.         android:layout_weight="1"  
  14.         />  
  15.   
  16.     <LinearLayout  
  17.         android:gravity="center_vertical"  
  18.         android:padding="10dp"  
  19.         android:orientation="horizontal"  
  20.         android:layout_alignParentBottom="true"  
  21.         android:layout_width="match_parent"  
  22.         android:layout_height="wrap_content">  
  23.   
  24.         <CheckBox  
  25.             android:background="@drawable/shopcart_unselected"  
  26.             android:button="@null"  
  27.             android:id="@+id/quanxuan"  
  28.             android:layout_width="wrap_content"  
  29.             android:layout_height="wrap_content" />  
  30.   
  31.         <TextView  
  32.             android:textStyle="bold"  
  33.             android:layout_marginLeft="10dp"  
  34.             android:textSize="23sp"  
  35.             android:layout_width="wrap_content"  
  36.             android:layout_height="wrap_content"  
  37.             android:text="全选"  
  38.             />  
  39.   
  40.         <LinearLayout  
  41.             android:padding="10dp"  
  42.             android:layout_marginLeft="10dp"  
  43.             android:orientation="vertical"  
  44.             android:layout_width="0dp"  
  45.             android:layout_weight="1"  
  46.             android:layout_height="wrap_content">  
  47.   
  48.             <TextView  
  49.                 android:textColor="#e53e42"  
  50.                 android:id="@+id/total_price"  
  51.                 android:layout_width="wrap_content"  
  52.                 android:layout_height="wrap_content"  
  53.                 android:textSize="20sp"  
  54.                 android:text="总价 : ¥0元"  
  55.                 />  
  56.   
  57.             <TextView  
  58.                 android:id="@+id/total_num"  
  59.                 android:layout_width="wrap_content"  
  60.                 android:layout_height="wrap_content"  
  61.                 android:textSize="20sp"  
  62.                 android:text="共0件商品"  
  63.                 />  
  64.   
  65.         </LinearLayout>  
  66.   
  67.         <TextView  
  68.             android:gravity="center"  
  69.             android:textSize="25sp"  
  70.             android:text="去结算"  
  71.             android:textColor="#fff"  
  72.             android:background="@drawable/qujiesuan"  
  73.             android:layout_width="wrap_content"  
  74.             android:layout_height="wrap_content" />  
  75.     </LinearLayout>  
  76.   
  77. </LinearLayout>  


自定义组合控件,,

custom_jiajian.xml

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:orientation="horizontal"  
  5.     android:gravity="center_vertical"  
  6.     android:layout_height="wrap_content">  
  7.   
  8.     <Button  
  9.         android:background="#fff"  
  10.         android:textSize="20sp"  
  11.         android:id="@+id/reverse"  
  12.         android:text="一"  
  13.         android:layout_width="50dp"  
  14.         android:layout_height="wrap_content" />  
  15.   
  16.     <EditText  
  17.         android:textStyle="bold"  
  18.         android:textSize="23sp"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:text="1"  
  22.         android:id="@+id/count"  
  23.         />  
  24.   
  25.     <Button  
  26.         android:id="@+id/add"  
  27.         android:background="#fff"  
  28.         android:textSize="25sp"  
  29.         android:text="+"  
  30.         android:layout_width="50dp"  
  31.         android:layout_height="wrap_content" />  
  32. </LinearLayout>  

适配器的条目的布局,recy_cart_item.xml

com.example.day20_mvp_cart.customView.CustomJiaJian 别忘了改成自己的包名下的,否则会报错

recy_cart_item.xml,需要引入 自定义组合控件,在创建了CustomJiaJian类以后才可以引入

recy_cart_item.xml适配器的布局

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:orientation="vertical"  
  5.     android:padding="15dp"  
  6.     android:layout_height="match_parent">  
  7.   
  8.     <LinearLayout  
  9.         android:orientation="horizontal"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content">  
  12.   
  13.         <CheckBox  
  14.             android:id="@+id/shop_checkbox"  
  15.             android:layout_width="50dp"  
  16.             android:layout_height="50dp" />  
  17.   
  18.         <TextView  
  19.             android:layout_marginLeft="20dp"  
  20.             android:text="良品铺子"  
  21.             android:layout_width="wrap_content"  
  22.             android:layout_height="wrap_content"  
  23.             android:textSize="23sp"  
  24.             android:textStyle="bold"  
  25.             android:id="@+id/shop_name"  
  26.             />  
  27.     </LinearLayout>  
  28.   
  29.     <LinearLayout  
  30.         android:gravity="center_vertical"  
  31.         android:orientation="horizontal"  
  32.         android:layout_width="match_parent"  
  33.         android:layout_height="wrap_content">  
  34.   
  35.         <CheckBox  
  36.             android:id="@+id/item_checkbox"  
  37.             android:layout_width="wrap_content"  
  38.             android:layout_height="wrap_content" />  
  39.   
  40.         <ImageView  
  41.             android:id="@+id/item_face"  
  42.             android:src="@mipmap/ic_launcher"  
  43.             android:layout_width="120dp"  
  44.             android:layout_height="120dp" />  
  45.   
  46.         <LinearLayout  
  47.             android:layout_marginLeft="10dp"  
  48.             android:orientation="vertical"  
  49.             android:layout_width="0dp"  
  50.             android:layout_weight="1"  
  51.             android:layout_height="wrap_content">  
  52.   
  53.             <TextView  
  54.                 android:id="@+id/item_name"  
  55.                 android:textSize="20sp"  
  56.                 android:text="三只松鼠"  
  57.                 android:layout_width="wrap_content"  
  58.                 android:layout_weight="1"  
  59.                 android:layout_height="0dp"  
  60.                 />  
  61.   
  62.             <TextView  
  63.                 android:textColor="#f00"  
  64.                 android:id="@+id/item_price"  
  65.                 android:textSize="23sp"  
  66.                 android:text="299"  
  67.                 android:layout_width="wrap_content"  
  68.                 android:layout_height="0dp"  
  69.                 android:layout_weight="1"  
  70.                 />  
  71.   
  72.   
  73.             <com.example.day171122_cart.customview.CustomJiaJian  
  74.                 android:id="@+id/custom_jiajian"  
  75.                 android:layout_width="wrap_content"  
  76.                 android:layout_height="0dp"  
  77.                 android:layout_weight="1"  
  78.                 />  
  79.         </LinearLayout>  
  80.   
  81.         <ImageView  
  82.             android:id="@+id/item_delete"  
  83.             android:layout_marginRight="10dp"  
  84.             android:src="@drawable/shopcart_delete"  
  85.             android:layout_width="30dp"  
  86.             android:layout_height="30dp" />  
  87.     </LinearLayout>  
  88. </LinearLayout>  

接着写代码里面的,首先将自定义组合控件的类填充在视图里

自定义组合控件的类,CustomJiaJian.java继承LinearLayout,inflate填充布局,+和-的点击事件回调给adapter


CustomJiaJian.java

[html] view plain copy
  1. public class CustomJiaJian extends LinearLayout{  
  2.     private Button reverse;  
  3.     private Button add;  
  4.     private EditText countEdit;  
  5.     private int mCount =1;  
  6.     public CustomJiaJian(Context context) {  
  7.         super(context);  
  8.     }  
  9.   
  10.     public CustomJiaJian(Context context, AttributeSet attrs) {  
  11.         super(context, attrs);  
  12.         View view = View.inflate(context, R.layout.custom_jiajian,this);  
  13.   
  14.         reverse = (Button) view.findViewById(R.id.reverse);  
  15.         add = (Button) view.findViewById(R.id.add);  
  16.         countEdit = (EditText) view.findViewById(R.id.count);  
  17.   
  18.         reverse.setOnClickListener(new OnClickListener() {  
  19.             @Override  
  20.             public void onClick(View v) {  
  21.                 String content = countEdit.getText().toString().trim();  
  22.                 int count = Integer.valueOf(content);  
  23.   
  24.                 if(count>1){  
  25.                     mCount = count-1;  
  26.                     countEdit.setText(mCount+"");  
  27.                     //回调给adapter里面  
  28.                     if(customListener!=null){  
  29.                         customListener.jiajian(mCount);  
  30.                     }  
  31.                 }  
  32.             }  
  33.         });  
  34.   
  35.         add.setOnClickListener(new OnClickListener() {  
  36.             @Override  
  37.             public void onClick(View v) {  
  38.                 String content = countEdit.getText().toString().trim();  
  39.                 int count = Integer.valueOf(content)+1;  
  40.                 mCount = count;  
  41.   
  42.                 countEdit.setText(mCount+"");  
  43.   
  44.                 //接口回调给adapter  
  45.                 if(customListener!=null){  
  46.                     customListener.jiajian(mCount);  
  47.                 }  
  48.             }  
  49.         });  
  50.   
  51.     }  
  52.   
  53.     public CustomJiaJian(Context context, AttributeSet attrs, int defStyleAttr) {  
  54.         super(context, attrs, defStyleAttr);  
  55.     }  
  56.   
  57.     CustomListener customListener;  
  58.     public void setCustomListener(CustomListener customListener){  
  59.         this.customListener = customListener;  
  60.     }  
  61.   
  62.     //加减的接口  
  63.     public interface CustomListener{  
  64.         public void jiajian(int count);  
  65.         public void shuRuZhi(int count);  
  66.     }  
  67.   
  68.     //这个方法是供recyadapter设置 数量时候调用的  
  69.     public void setEditText(int num) {  
  70.         if(countEdit !=null) {  
  71.             countEdit.setText(num + "");  
  72.         }  
  73.     }  
  74. }  


下面就是okhttp二次封装的类,这里就不详细叙述了,详见okhttp封装类,拦截器地址

根据最上面给出的接口.访问到的数据,生成一个实体类CartBean


在CartBean类里面自己添加三个字段,isFirst,item_check,shop_check

[html] view plain copy
  1. //自己添加的三个字段  
  2.            private int isFirst = 1;//1为显示商铺, 2为隐藏商铺  
  3.            private boolean item_check;//每个商品的选中状态  
  4.            private boolean shop_check;//商店的选中状态  
  5.   
  6.            public int getIsFirst() {  
  7.                return isFirst;  
  8.            }  
  9.   
  10.            public void setIsFirst(int isFirst) {  
  11.                this.isFirst = isFirst;  
  12.            }  
  13.   
  14.            public boolean isItem_check() {  
  15.                return item_check;  
  16.            }  
  17.   
  18.            public void setItem_check(boolean item_check) {  
  19.                this.item_check = item_check;  
  20.            }  
  21.   
  22.            public boolean isShop_check() {  
  23.                return shop_check;  
  24.            }  
  25.   
  26.            public void setShop_check(boolean shop_check) {  
  27.                this.shop_check = shop_check;  
  28.            }  

接下来就是使用MVP架构模式..首先创建presentermodel层,View这里默认就是MainActivity

写两个接口,一个是ModelCallBack是model层的回调接口,ViewCallBack是view层的回调接口

ModelCallBack.java

[html] view plain copy
  1. public interface ModelCallBack {  
  2.     public void success(CartBean cartBean);  
  3.     public void failure(Exception e);  
  4. }  

ViewCallBack.java
[html] view plain copy
  1. public interface ViewCallBack {  
  2.     public void success(CartBean cartBean);  
  3.     public void failure(Exception e);  
  4. }  

下面是重要的MainActivity.java, 调用presenter层的方法,实现view层的接口的方法,还调用适配器adapter里面的接口

[html] view plain copy
  1. public class MainActivity extends AppCompatActivity implements ViewCallBack{  
  2.   
  3.     private RecyclerView recyclerView;  
  4.     private TextView total_price;  
  5.     private TextView total_num;  
  6.     private CheckBox quanxuan;  
  7.     private MyPresenter myPresenter;  
  8.     private RecyAdapter recyAdapter;  
  9.   
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.activity_main);  
  14.         //http://120.27.23.105/product/getCarts?uid=100  
  15.   
  16.         recyclerView = (RecyclerView) findViewById(R.id.recycler_View);  
  17.         total_price = (TextView) findViewById(R.id.total_price);  
  18.         total_num = (TextView) findViewById(R.id.total_num);  
  19.         quanxuan = (CheckBox) findViewById(R.id.quanxuan);  
  20.   
  21.         quanxuan.setTag(1);//1为不选中  
  22.         LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this,LinearLayoutManager.VERTICAL,false);  
  23.         //new出适配器  
  24.         recyAdapter = new RecyAdapter(this);  
  25.         myPresenter = new MyPresenter(this);  
  26.         //调用presenter里面的请求数据的方法  
  27.         myPresenter.getData();  
  28.   
  29.         recyclerView.setLayoutManager(manager);  
  30.         recyclerView.setAdapter(recyAdapter);  
  31.   
  32.         //调用recyAdapter里面的接口,设置 全选按钮 总价 总数量  
  33.         recyAdapter.setUpdateListener(new RecyAdapter.UpdateListener() {  
  34.             @Override  
  35.             public void setTotal(String total, String num, boolean allCheck) {  
  36.                 //设置ui的改变  
  37.                 total_num.setText("共"+num+"件商品");//总数量  
  38.                 total_price.setText("总价 :¥"+total+"元");//总价  
  39.                 if(allCheck){  
  40.                     quanxuan.setTag(2);  
  41.                     quanxuan.setBackgroundResource(R.drawable.shopcart_selected);  
  42.                 }else{  
  43.                     quanxuan.setTag(1);  
  44.                     quanxuan.setBackgroundResource(R.drawable.shopcart_unselected);  
  45.                 }  
  46.                 quanxuan.setChecked(allCheck);  
  47.             }  
  48.         });  
  49.   
  50.         //这里只做ui更改, 点击全选按钮,,调到adapter里面操作  
  51.         quanxuan.setOnClickListener(new View.OnClickListener() {  
  52.             @Override  
  53.             public void onClick(View v) {  
  54.                 //调用adapter里面的方法 ,,把当前quanxuan状态传递过去  
  55.   
  56.                 int tag = (int) quanxuan.getTag();  
  57.                 if(tag==1){  
  58.                     quanxuan.setTag(2);  
  59.                     quanxuan.setBackgroundResource(R.drawable.shopcart_selected);  
  60.                 }else{  
  61.                     quanxuan.setTag(1);  
  62.                     quanxuan.setBackgroundResource(R.drawable.shopcart_unselected);  
  63.                 }  
  64.   
  65.                 recyAdapter.quanXuan(quanxuan.isChecked());  
  66.             }  
  67.         });  
  68.   
  69.     }  
  70.   
  71.   
  72.     //实现接口,重写的方法  
  73.     @Override  
  74.     public void success(CartBean cartBean) {  
  75.         //拿到返回来的数据 ,, 传给适配器数据  
  76.         recyAdapter.add(cartBean);  
  77.     }  
  78.   
  79.     @Override  
  80.     public void failure(final Exception e) {  
  81.         runOnUiThread(new Runnable() {  
  82.             @Override  
  83.             public void run() {  
  84.                 Toast.makeText(MainActivity.this,"error",Toast.LENGTH_SHORT).show();  
  85.             }  
  86.         });  
  87.     }  
  88.   
  89.     @Override  
  90.     protected void onDestroy() {  
  91.         super.onDestroy();  
  92.         //调用p层的解除绑定  
  93.         myPresenter.detach();  
  94.     }  
  95. }  

MyPresenter.java,presenter层里面 调用model层的方法,,防止内存泄露解绑

[html] view plain copy
  1. public class MyPresenter {  
  2.   
  3.     MyModel myModel = new MyModel();  
  4.     ViewCallBack viewCallBack;  
  5.     public MyPresenter(ViewCallBack viewCallBack) {  
  6.         this.viewCallBack = viewCallBack;  
  7.     }  
  8.   
  9.     //调用model 层的请求数据  
  10.     public void getData(){  
  11.         myModel.getData(new ModelCallBack() {  
  12.             @Override  
  13.             public void success(CartBean cartBean) {  
  14.                 if(viewCallBack!=null) {  
  15.                     viewCallBack.success(cartBean);  
  16.                 }  
  17.             }  
  18.   
  19.             @Override  
  20.             public void failure(Exception e) {  
  21.                 if(viewCallBack!=null) {  
  22.                     viewCallBack.failure(e);  
  23.                 }  
  24.             }  
  25.         });  
  26.     }  
  27.   
  28.     /**  
  29.      * 防止内存泄露  
  30.      * */  
  31.     public void detach(){  
  32.         viewCallBack=null;  
  33.     }  
  34. }  



MyModel.java,model层里面 调用okhttp的封装类 单例模式,请求网络数据.返回一个bean类,类型改成CartBean

上面已经放过了MainActivity接收到model传给presenter.presenter传给view 的CartBean以后,将数据添加给适配器,

[html] view plain copy
  1. public class MyModel {  
  2.     public void getData(final ModelCallBack modelCallBack){  
  3.         //访问接口  
  4.         String path = "http://120.27.23.105/product/getCarts?uid=100";  
  5.         OkhttpUtils.getInstance().asy(null, path, new AbstractUiCallBack<CartBean>() {  
  6.   
  7.             @Override  
  8.             public void success(CartBean cartBean) {  
  9.                 modelCallBack.success(cartBean);  
  10.             }  
  11.   
  12.             @Override  
  13.             public void fail(Exception e) {  
  14.                 modelCallBack.failure(e);  
  15.             }  
  16.         });  
  17.     }  
  18. }  


下面是适配器RecyAdapter.java 这里面的操作量就很大,包括了对每个条目的一系列操作,删除,选中,和对自定义视图加减号改变数量等,和是否显示和隐藏一级商家的信息,求总价,总数量等,部分操作也用到了接口回调出去

[html] view plain copy
  1. public class RecyAdapter extends RecyclerView.Adapter<RecyAdapter.MyViewHolder>{  
  2.   
  3.     Context context;  
  4.     //创建大的集合  
  5.     private List<CartBean.DataBean.ListBean> list;  
  6.   
  7.     //存放商家的id和商家的名称的map集合  
  8.     private Map<String,String> map = new HashMap<>();  
  9.   
  10.     public RecyAdapter(Context context) {  
  11.         this.context = context;  
  12.     }  
  13.   
  14.     /**  
  15.      * 添加数据并更新显示  
  16.      * */  
  17.     public void add(CartBean cartBean){  
  18.         //传进来的是bean对象  
  19.         if(list == null){  
  20.             list = new ArrayList<>();  
  21.         }  
  22.         //第一层遍历商家和商品  
  23.         for (CartBean.DataBean shop : cartBean.getData()){  
  24.             //把商品的id和商品的名称添加到map集合里 ,,为了之后方便调用  
  25.             map.put(shop.getSellerid(),shop.getSellerName());  
  26.             //第二层遍历里面的商品  
  27.             for (int i=0;i<shop.getList().size();i++){  
  28.                 //添加到list集合里  
  29.                 list.add(shop.getList().get(i));  
  30.             }  
  31.         }  
  32.         //调用方法 设置显示或隐藏 商铺名  
  33.         setFirst(list);  
  34.         notifyDataSetChanged();  
  35.     }  
  36.   
  37.     /**  
  38.      * 设置数据源,控制是否显示商家  
  39.      * */  
  40.     private void setFirst(List<CartBean.DataBean.ListBean> list) {  
  41.   
  42.         if(list.size()>0){  
  43.             //如果是第一条数据就设置isFirst为1  
  44.             list.get(0).setIsFirst(1);  
  45.             //从第二条开始遍历  
  46.             for (int i=1;i<list.size();i++){  
  47.                 //如果和前一个商品是同一家商店的  
  48.                 if (list.get(i).getSellerid() == list.get(i-1).getSellerid()){  
  49.                     //设置成2不显示商铺  
  50.                     list.get(i).setIsFirst(2);  
  51.                 }else{//设置成1显示商铺  
  52.                     list.get(i).setIsFirst(1);  
  53.                     //如果当前条目选中,把当前的商铺也选中  
  54.                     if (list.get(i).isItem_check()==true){  
  55.                         list.get(i).setShop_check(list.get(i).isItem_check());  
  56.                     }  
  57.                 }  
  58.             }  
  59.         }  
  60.     }  
  61.   
  62.     @Override  
  63.     public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {  
  64.         View view = View.inflate(context, R.layout.recy_cart_item,null);  
  65.         MyViewHolder myViewHolder = new MyViewHolder(view);  
  66.         return myViewHolder;  
  67.     }  
  68.   
  69.     @Override  
  70.     public void onBindViewHolder(final MyViewHolder holder, final int position) {  
  71.   
  72.         /**  
  73.          * 设置商铺的 shop_checkbox和商铺的名字 显示或隐藏  
  74.          * */  
  75.         if(list.get(position).getIsFirst()==1){  
  76.             //显示商家  
  77.             holder.shop_checkbox.setVisibility(View.VISIBLE);  
  78.             holder.shop_name.setVisibility(View.VISIBLE);  
  79.             //设置shop_checkbox的选中状态  
  80.             holder.shop_checkbox.setChecked(list.get(position).isShop_check());  
  81.             holder.shop_name.setText(map.get(String.valueOf(list.get(position).getSellerid())));  
  82.         }else{//2  
  83.             //隐藏商家  
  84.             holder.shop_name.setVisibility(View.GONE);  
  85.             holder.shop_checkbox.setVisibility(View.GONE);  
  86.         }  
  87.   
  88.         //拆分images字段  
  89.         String[] split = list.get(position).getImages().split("\\|");  
  90.         //设置商品的图片  
  91.         ImageLoader.getInstance().displayImage(split[0],holder.item_face);  
  92.         //控制商品的item_checkbox,,根据字段改变  
  93.         holder.item_checkbox.setChecked(list.get(position).isItem_check());  
  94.         holder.item_name.setText(list.get(position).getTitle());  
  95.         holder.item_price.setText(list.get(position).getPrice()+"");  
  96.         //调用customjiajian里面的方法设置 加减号中间的数字  
  97.         holder.customJiaJian.setEditText(list.get(position).getNum());  
  98.   
  99.         //商铺的shop_checkbox点击事件 ,控制商品的item_checkbox  
  100.         holder.shop_checkbox.setOnClickListener(new View.OnClickListener() {  
  101.             @Override  
  102.             public void onClick(View v) {  
  103.                 //先改变数据源中的shop_check  
  104.                 list.get(position).setShop_check(holder.shop_checkbox.isChecked());  
  105.   
  106.                 for (int i=0;i<list.size();i++){  
  107.                     //如果是同一家商铺的 都给成相同状态  
  108.                     if(list.get(position).getSellerid()==list.get(i).getSellerid()){  
  109.                         //当前条目的选中状态 设置成 当前商铺的选中状态  
  110.                         list.get(i).setItem_check(holder.shop_checkbox.isChecked());  
  111.                     }  
  112.                 }  
  113.                 //刷新适配器  
  114.                 notifyDataSetChanged();  
  115.                 //调用求和的方法  
  116.                 sum(list);  
  117.             }  
  118.         });  
  119.   
  120.         //商品的item_checkbox点击事件,控制商铺的shop_checkbox  
  121.         holder.item_checkbox.setOnClickListener(new View.OnClickListener() {  
  122.             @Override  
  123.             public void onClick(View v) {  
  124.                 //先改变数据源中的item_checkbox  
  125.                 list.get(position).setItem_check(holder.item_checkbox.isChecked());  
  126.   
  127.                 //反向控制商铺的shop_checkbox  
  128.                 for (int i=0;i<list.size();i++){  
  129.                     for (int j=0;j<list.size();j++){  
  130.                         //如果两个商品是同一家店铺的 并且 这两个商品的item_checkbox选中状态不一样  
  131.                         if(list.get(i).getSellerid()==list.get(j).getSellerid() && !list.get(j).isItem_check()){  
  132.                             //就把商铺的shop_checkbox改成false  
  133.                             list.get(i).setShop_check(false);  
  134.                             break;  
  135.                         }else{  
  136.                             //同一家商铺的商品 选中状态都一样,就把商铺shop_checkbox状态改成true  
  137.                             list.get(i).setShop_check(true);  
  138.                         }  
  139.                     }  
  140.                 }  
  141.   
  142.                 //更新适配器  
  143.                 notifyDataSetChanged();  
  144.                 //调用求和的方法  
  145.                 sum(list);  
  146.             }  
  147.         });  
  148.   
  149.         //删除条目的点击事件  
  150.         holder.item_delete.setOnClickListener(new View.OnClickListener() {  
  151.             @Override  
  152.             public void onClick(View v) {  
  153.                 list.remove(position);//移除集合中的当前数据  
  154.                 //删除完当前的条目 重新判断商铺的显示隐藏  
  155.                 setFirst(list);  
  156.   
  157.                 //调用重新求和  
  158.                 sum(list);  
  159.                 notifyDataSetChanged();  
  160.             }  
  161.         });  
  162.   
  163.         //加减号的监听,  
  164.         holder.customJiaJian.setCustomListener(new CustomJiaJian.CustomListener() {  
  165.             @Override  
  166.             public void jiajian(int count) {  
  167.                 //改变数据源中的数量  
  168.                 list.get(position).setNum(count);  
  169.                 notifyDataSetChanged();  
  170.                 sum(list);  
  171.             }  
  172.   
  173.             @Override  
  174.             //输入值 求总价  
  175.             public void shuRuZhi(int count) {  
  176.                 list.get(position).setNum(count);  
  177.                 notifyDataSetChanged();  
  178.                 sum(list);  
  179.             }  
  180.         });  
  181.     }  
  182.   
  183.     /**  
  184.      * 计算总价的方法  
  185.      * */  
  186.     private void sum(List<CartBean.DataBean.ListBean> list){  
  187.         int totalNum = 0;//初始的总价为0  
  188.         float totalMoney = 0.0f;  
  189.         boolean allCheck = true;  
  190.         for (int i=0;i<list.size();i++){  
  191.             //把 已经选中的 条目 计算价格  
  192.             if (list.get(i).isItem_check()){  
  193.                 totalNum += list.get(i).getNum();  
  194.                 totalMoney += list.get(i).getNum() * list.get(i).getPrice();  
  195.             }else{  
  196.                 //如果有个未选中,就标记为false  
  197.                 allCheck = false;  
  198.             }  
  199.         }  
  200.   
  201.         //接口回调出去 把总价 总数量 和allcheck 传给view层  
  202.         updateListener.setTotal(totalMoney+"",totalNum+"",allCheck);  
  203.     }  
  204.   
  205.     //view层调用这个方法, 点击quanxuan按钮的操作  
  206.     public void quanXuan(boolean checked) {  
  207.         for (int i=0;i<list.size();i++){  
  208.             list.get(i).setShop_check(checked);  
  209.             list.get(i).setItem_check(checked);  
  210.   
  211.         }  
  212.         notifyDataSetChanged();  
  213.         sum(list);  
  214.     }  
  215.   
  216.     @Override  
  217.     public int getItemCount() {  
  218.         return list==null?0:list.size();  
  219.     }  
  220.   
  221.   
  222.   
  223.     public static class MyViewHolder extends RecyclerView.ViewHolder {  
  224.   
  225.         private final CheckBox shop_checkbox;  
  226.         private final TextView shop_name;  
  227.         private final CheckBox item_checkbox;  
  228.         private final TextView item_name;  
  229.         private final TextView item_price;  
  230.         private final CustomJiaJian customJiaJian;  
  231.         private final ImageView item_delete;  
  232.         private final ImageView item_face;  
  233.   
  234.         public MyViewHolder(View itemView) {  
  235.             super(itemView);  
  236.             shop_checkbox = (CheckBox) itemView.findViewById(R.id.shop_checkbox);  
  237.             shop_name = (TextView) itemView.findViewById(R.id.shop_name);  
  238.             item_checkbox = (CheckBox) itemView.findViewById(R.id.item_checkbox);  
  239.             item_name = (TextView) itemView.findViewById(R.id.item_name);  
  240.             item_price = (TextView) itemView.findViewById(R.id.item_price);  
  241.             customJiaJian = (CustomJiaJian) itemView.findViewById(R.id.custom_jiajian);  
  242.             item_delete = (ImageView) itemView.findViewById(R.id.item_delete);  
  243.             item_face = (ImageView) itemView.findViewById(R.id.item_face);  
  244.         }  
  245.     }  
  246.   
  247.     UpdateListener updateListener;  
  248.     public void setUpdateListener(UpdateListener updateListener){  
  249.         this.updateListener = updateListener;  
  250.     }  
  251.     //接口  
  252.     public interface UpdateListener{  
  253.         public void setTotal(String total,String num,boolean allCheck);  
  254.     }  

原创粉丝点击