一级列表购物车的简单实现(网络接口)

来源:互联网 发布:vb select选中触发事件 编辑:程序博客网 时间:2024/04/29 11:54

导入依赖

[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.0.0'  
  4.    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'  

values下面的colors.xml,方便布局里面的统一调用

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <color name="colorPrimary">#3F51B5</color>  
  4.     <color name="colorPrimaryDark">#303F9F</color>  
  5.     <color name="colorAccent">#FF4081</color>  
  6.   
  7.     <!--背景颜色-->  
  8.     <color name="toolbar_color">#f8f8f8</color>  
  9.     <color name="background_color">#f6f6f6</color>  
  10.     <color name="edittext_noenter_color">#f5f5f5</color>  
  11.   
  12.     <!--辅助色-->  
  13.     <color name="support_yellow">#f9a340</color>  
  14.     <color name="btn_unselect_color">#f4736e</color>  
  15.   
  16.     <!--分割线-->  
  17.     <color name="splitline_color">#dddddd</color>  
  18.   
  19.     <!--文字-->  
  20.     <color name="notice_text_color">#999999</color>  
  21.     <color name="default_text_color">#666666</color>  
  22.     <color name="main_black_text">#333333</color>  
  23.     <color name="main_red_text">#e53e42</color>  
  24.     <color name="main_white_text">#ffffff</color>  
  25.     <color name="protocol_text">#3C68FE</color>  
  26.   
  27.     <!--图标-->  
  28.     <color name="abate_icon_color">#bbbbbb</color>  
  29.     <color name="default_icon_color">#999999</color>  
  30.     <color name="pressed_icon_color">#e53e42</color>  
  31.   
  32.     <!--CycleViewPager-->  
  33.     <color name="cycle_image_bg">#44222222</color>  
  34.   
  35.     <!-- 透明 -->  
  36.     <color name="transparent">#00000000</color>;  
  37.   </resources>  
去结算 这个文字的背景 自己画的小圆点

[html] view plain copy
  1. <shape xmlns:android="http://schemas.android.com/apk/res/android">  
  2.   
  3.     <corners android:radius="200dp"></corners>  
  4.     <solid android:color="@color/pressed_icon_color"></solid>  
  5. </shape>  

activity_main.xml里面的布局 ,上面是一个recyclerview ,下面是一个linearlayout,装着全选 ,总价,总数量,去结算等

[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:orientation="vertical"  
  6.     android:layout_height="match_parent"  
  7.     >  
  8.   
  9.     <android.support.v7.widget.RecyclerView  
  10.                 android:id="@+id/second_recyview"  
  11.                 android:layout_width="match_parent"  
  12.                 android:layout_height="0dp"  
  13.                 android:layout_weight="1"  
  14.                 />  
  15.   
  16.     <LinearLayout  
  17.             android:layout_alignBottom="@+id/linear"  
  18.             android:padding="10dp"  
  19.             android:layout_width="match_parent"  
  20.             android:layout_height="wrap_content"  
  21.             android:orientation="horizontal"  
  22.             android:background="@color/main_white_text"  
  23.             android:gravity="center_vertical"  
  24.             android:id="@+id/ll_pay"  
  25.             android:layout_alignParentBottom="true"  
  26.             >  
  27.   
  28.         <CheckBox  
  29.             android:button="@null"  
  30.             android:id="@+id/quanxuan"  
  31.             android:background="@drawable/shopcart_unselected"  
  32.             android:layout_width="wrap_content"  
  33.             android:layout_height="wrap_content" />  
  34.   
  35.             <TextView  
  36.                 android:textSize="23sp"  
  37.                 android:id="@+id/all_select"  
  38.                 android:layout_width="wrap_content"  
  39.                 android:layout_height="wrap_content"  
  40.                 android:layout_marginLeft="10dp"  
  41.                 android:text="全选"  
  42.                 android:drawablePadding="5dp"  
  43.                 />  
  44.   
  45.             <LinearLayout  
  46.                 android:layout_width="0dp"  
  47.                 android:layout_height="wrap_content"  
  48.                 android:layout_weight="1"  
  49.                 android:orientation="vertical"  
  50.                 >  
  51.   
  52.                 <TextView  
  53.                     android:id="@+id/totalprice"  
  54.                     android:layout_width="wrap_content"  
  55.                     android:layout_height="wrap_content"  
  56.                     android:paddingLeft="10dp"  
  57.                     android:paddingTop="10dp"  
  58.                     android:text="总价:¥0"  
  59.                     android:textColor="@color/main_red_text"  
  60.                     android:textSize="20sp"  
  61.                     />  
  62.   
  63.                 <TextView  
  64.                     android:id="@+id/totalnum"  
  65.                     android:layout_width="wrap_content"  
  66.                     android:layout_height="wrap_content"  
  67.                     android:paddingLeft="10dp"  
  68.                     android:text="共0件商品"  
  69.                     android:textSize="18sp"  
  70.                     android:paddingBottom="10dp"  
  71.                     />  
  72.   
  73.             </LinearLayout>  
  74.   
  75.             <TextView  
  76.                 android:id="@+id/qujiesuan"  
  77.                 android:layout_width="wrap_content"  
  78.                 android:layout_height="wrap_content"  
  79.                 android:background="@drawable/login_btn"  
  80.                 android:text="去结算"  
  81.                 android:textSize="23sp"  
  82.                 android:paddingLeft="30dp"  
  83.                 android:paddingRight="30dp"  
  84.                 android:paddingTop="10dp"  
  85.                 android:paddingBottom="10dp"  
  86.                 android:textColor="@color/main_white_text"  
  87.                 android:layout_marginRight="10dp"  
  88.                 />  
  89.   
  90.         </LinearLayout>  
  91.   
  92. </LinearLayout>  
recyclerview的适配器的布局 ,,里面引入了自定义的customview的布局

[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:layout_height="match_parent">  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:gravity="center_vertical"  
  11.         >  
  12.   
  13.         <LinearLayout  
  14.             android:layout_width="match_parent"  
  15.             android:layout_height="wrap_content"  
  16.             android:orientation="vertical"  
  17.             >  
  18.   
  19.             <View  
  20.                 android:layout_width="match_parent"  
  21.                 android:layout_height="1dp"  
  22.                 android:background="@color/background_color"  
  23.                 ></View>  
  24.   
  25.             <LinearLayout  
  26.                 android:layout_width="match_parent"  
  27.                 android:layout_height="wrap_content">  
  28.   
  29.                 <ImageView  
  30.                     android:id="@+id/shopcart_cloth_select"  
  31.                     android:layout_width="wrap_content"  
  32.                     android:layout_height="wrap_content"  
  33.                     android:src="@drawable/shopcart_selected"  
  34.                     android:layout_marginLeft="15dp"  
  35.                     android:layout_marginRight="15dp"  
  36.                     android:visibility="invisible"  
  37.                     />  
  38.   
  39.                 <TextView  
  40.                     android:id="@+id/tv_item_shopcart_clothname"  
  41.                     android:layout_width="match_parent"  
  42.                     android:layout_height="wrap_content"  
  43.                     android:text="穿秋装情侣字母徽章风衣"  
  44.                     android:paddingLeft="10dp"  
  45.                     android:paddingTop="10dp"  
  46.                     android:textSize="20sp"  
  47.                     />  
  48.             </LinearLayout>  
  49.             <LinearLayout  
  50.                 android:layout_width="match_parent"  
  51.                 android:layout_height="wrap_content"  
  52.                 android:orientation="horizontal"  
  53.                 android:gravity="center_vertical"  
  54.                 >  
  55.   
  56.                 <CheckBox  
  57.                     android:id="@+id/cb_item_shopcart_clothselect"  
  58.                     android:layout_width="30dp"  
  59.                     android:layout_height="30dp"  
  60.                     android:layout_marginLeft="10dp"  
  61.                     />  
  62.   
  63.                 <ImageView  
  64.                     android:src="@mipmap/ic_launcher"  
  65.                     android:id="@+id/iv_item_shopcart_cloth_pic"  
  66.                     android:layout_width="90dp"  
  67.                     android:layout_height="90dp"  
  68.                     android:layout_margin="10dp"  
  69.                     />  
  70.   
  71.                 <LinearLayout  
  72.                     android:layout_width="0dp"  
  73.                     android:layout_height="wrap_content"  
  74.                     android:layout_weight="1"  
  75.                     android:orientation="vertical"  
  76.                     >  
  77.   
  78.                     <TextView  
  79.                         android:id="@+id/tv_item_shopcart_cloth_price"  
  80.                         android:layout_width="wrap_content"  
  81.                         android:layout_height="wrap_content"  
  82.                         android:text="¥185"  
  83.                         android:textColor="@color/main_red_text"  
  84.                         android:textSize="20sp"  
  85.                         />  
  86.   
  87.                     <LinearLayout  
  88.                         android:layout_width="wrap_content"  
  89.                         android:layout_height="wrap_content"  
  90.                         android:layout_marginTop="5dp"  
  91.                         android:layout_marginBottom="5dp"  
  92.                         >  
  93.   
  94.                         <TextView  
  95.                             android:id="@+id/tv_item_shopcart_cloth_color"  
  96.                             android:layout_width="wrap_content"  
  97.                             android:layout_height="wrap_content"  
  98.                             android:text="颜色:黑色"  
  99.                             android:textSize="18sp"  
  100.                             />  
  101.   
  102.                         <TextView  
  103.                             android:id="@+id/tv_item_shopcart_cloth_size"  
  104.                             android:layout_width="wrap_content"  
  105.                             android:layout_height="wrap_content"  
  106.                             android:text="尺寸:XL"  
  107.                             android:textSize="18sp"  
  108.                             android:layout_marginLeft="10dp"  
  109.                             />  
  110.   
  111.                     </LinearLayout>  
  112.   
  113.                     <com.example.a171118_zhouliu_disanzhou.custom.CustomView  
  114.                         android:layout_width="100dp"  
  115.                         android:layout_height="50dp"  
  116.                         android:id="@+id/custom_View"  
  117.                         android:layout_marginLeft="20dp"/>  
  118.   
  119.                 </LinearLayout>  
  120.   
  121.                 <View  
  122.                     android:layout_width="1dp"  
  123.                     android:layout_height="match_parent"  
  124.                     android:layout_marginTop="10dp"  
  125.                     android:layout_marginBottom="10dp"  
  126.                     android:background="@color/splitline_color"  
  127.                     ></View>  
  128.   
  129.                 <ImageView  
  130.                     android:id="@+id/iv_item_shopcart_cloth_delete"  
  131.                     android:layout_width="wrap_content"  
  132.                     android:layout_height="wrap_content"  
  133.                     android:padding="20dp"  
  134.                     android:src="@drawable/shopcart_delete"  
  135.                     />  
  136.   
  137.             </LinearLayout>  
  138.   
  139.         </LinearLayout>  
  140.   
  141.     </LinearLayout>  
  142.   
  143.     <View  
  144.         android:layout_width="match_parent"  
  145.         android:layout_height="1dp"  
  146.         android:background="@color/background_color"  
  147.         ></View>  
  148.   
  149. </LinearLayout>  

自定义组合控件的customview的布局

[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:layout_height="match_parent">  
  6.   
  7.     <Button  
  8.         android:background="#00FFFFFF"  
  9.         android:gravity="center"  
  10.         android:textStyle="bold"  
  11.         android:textSize="20sp"  
  12.         android:text="一"  
  13.         android:id="@+id/reverse"  
  14.         android:layout_width="50dp"  
  15.         android:layout_height="30dp" />  
  16.   
  17.     <EditText  
  18.         android:id="@+id/count"  
  19.         android:textSize="23sp"  
  20.         android:textStyle="bold"  
  21.         android:text="1"  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="50dp" />  
  24.   
  25.     <Button  
  26.         android:textStyle="bold"  
  27.         android:textSize="22sp"  
  28.         android:id="@+id/add"  
  29.         android:gravity="center"  
  30.         android:background="#00FFFFFF"  
  31.         android:layout_width="50dp"  
  32.         android:layout_height="30dp"  
  33.         android:text="+"/>  
  34. </LinearLayout>  
CustomView类继承LinearLayout,, 填充customView.xml的布局

[html] view plain copy
  1. public class CustomView extends LinearLayout{  
  2.   
  3.     private Button reverse;  
  4.     private Button add;  
  5.     private EditText editText;  
  6.     private int mCount;  
  7.     Context context;  
  8.     public CustomView(Context context) {  
  9.         super(context);  
  10.     }  
  11.   
  12.     public CustomView(Context context, AttributeSet attrs) {  
  13.         super(context, attrs);  
  14.         this.context =context;  
  15.   
  16.         View view = View.inflate(context, R.layout.custom_view,this);  
  17.   
  18.         reverse = (Button) view.findViewById(R.id.reverse);  
  19.         add = (Button) view.findViewById(R.id.add);  
  20.         editText = (EditText) view.findViewById(R.id.count);  
  21.   
  22.        //点击减号,,数量减1  
  23.         reverse.setOnClickListener(new OnClickListener() {  
  24.             @Override  
  25.             public void onClick(View v) {  
  26.                 String content = editText.getText().toString().trim();  
  27.                 //转成int  
  28.                 int count = Integer.valueOf(content);  
  29.                 if(count>1){  
  30.                     mCount = count-1;  
  31.                     editText.setText(mCount+"");//设置数量  
  32.   
  33.                     //接口回调 把mCount暴露出去,,adapter里面调用  
  34.                    if(clickListener!=null){  
  35.                        clickListener.click(mCount);  
  36.                    }  
  37.                 }  
  38.             }  
  39.         });  
  40.   
  41.         //点击加号 数量加1  
  42.         add.setOnClickListener(new OnClickListener() {  
  43.             @Override  
  44.             public void onClick(View v) {  
  45.                 String content = editText.getText().toString().trim();  
  46.                 int count = Integer.valueOf(content);  
  47.   
  48.                 mCount = count+1;//赋值给mcount  
  49.                 editText.setText(count+"");  
  50.   
  51.                 if(clickListener!=null){  
  52.                     clickListener.click(mCount);  
  53.                 }  
  54.             }  
  55.         });  
  56.   
  57.   
  58.     }  
  59.   
  60.     ClickListener clickListener;  
  61.     public void setClickListener(ClickListener clickListener){  
  62.         this.clickListener = clickListener;  
  63.     }  
  64.     //设置加减号的监听  
  65.     public interface ClickListener{  
  66.         public void click(int count);//把当前的数量传过去  
  67.     }  
  68.   
  69.     //设置初始的数量值  
  70.     public void getCount(int count){  
  71.         editText.setText(count+"");//设置初始数量  
  72.     }  
  73.   
  74.   
  75.   
  76.     public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {  
  77.         super(context, attrs, defStyleAttr);  
  78.     }  
  79. }  
RecyclerAdapter 适配器里面的,, 实现customview里面的接口

[html] view plain copy
  1. public class SecondRecyAdapter extends RecyclerView.Adapter<SecondRecyAdapter.MyViewholder1>{  
  2.   
  3.     Context context;  
  4.     List<SecondBean.DataBean.ListBean> listShop;  
  5.   
  6.     public SecondRecyAdapter(Context context, List<SecondBean.DataBean.ListBean> listShop) {  
  7.         this.listShop = listShop;  
  8.         this.context =  context;  
  9.     }  
  10.   
  11.   
  12.     @Override  
  13.     public MyViewholder1 onCreateViewHolder(ViewGroup parent, int viewType) {  
  14.         View view = View.inflate(context,R.layout.second_recy_adapter,null);  
  15.         MyViewholder1 myviewholder1 = new MyViewholder1(view);  
  16.         return myviewholder1;  
  17.     }  
  18.   
  19.     @Override  
  20.     public void onBindViewHolder(final MyViewholder1 holder, final int position) {  
  21.   
  22.         String images = listShop.get(position).getImages();  
  23.         String[] split = images.split("\\|");  
  24.   
  25.         //设置商品图片  
  26.         ImageLoader.getInstance().displayImage(split[0],holder.ivPic);  
  27.         //调用customiew类里面的方法,,设置商品名称  
  28.         holder.customView.getCount(listShop.get(position).getNum());  
  29.       // holder.tvShopName.setText(listShop.get(position).getSellerid());  
  30.         holder.tvName.setText(listShop.get(position).getTitle());  
  31.         holder.tvPrice.setText(listShop.get(position).getPrice()+"");  
  32.         //给 每个条目的选中按钮 设置状态,根据数据源里面的check  
  33.         holder.product_select.setChecked(listShop.get(position).isCheck());  
  34.         //主动点击每个条目前面的多选框,点击事件  
  35.         holder.product_select.setOnClickListener(new View.OnClickListener() {  
  36.             @Override  
  37.             public void onClick(View v) {  
  38.   
  39.                //拿到当前多选框的选中状态,,赋值给数据源里面,,holder.product_select.isChecked()  
  40.                listShop.get(position).setCheck(holder.product_select.isChecked());  
  41.                 notifyDataSetChanged();  
  42.   
  43.                 if(checkBoxListener != null){  
  44.                     //secondActivity调用方法  
  45.                     checkBoxListener.checkBox(listShop);  
  46.                 }  
  47.   
  48.             }  
  49.         });  
  50.   
  51.         //customview中的加减号的点击监听  
  52.         holder.customView.setClickListener(new CustomView.ClickListener() {  
  53.             @Override  
  54.             public void click(int count) {  
  55.                 //count是当前的数量  
  56.                 //根据传来的count 改变数据源里的num  
  57.                 listShop.get(position).setNum(count);  
  58.                 notifyDataSetChanged();  
  59.   
  60.                 //接口回调出去..  
  61.                 if(customViewListener!=null){  
  62.                     //把当前集合传过去  
  63.                     customViewListener.customClick(listShop);  
  64.                 }  
  65.             }  
  66.         });  
  67.   
  68.         //删除当前条目的点击事件  
  69.         holder.ivDelete.setOnClickListener(new View.OnClickListener() {  
  70.             @Override  
  71.             public void onClick(View v) {  
  72.                 //改变数据源中的 删除当前条目  
  73.                 listShop.remove(position);  
  74.                 notifyDataSetChanged();  
  75.   
  76.                 //接口回调出去  
  77.                 if(delItemListener!=null){  
  78.                     delItemListener.delItem(listShop);  
  79.                 }  
  80.             }  
  81.         });  
  82.   
  83.   
  84.  }  
  85.   
  86.     @Override  
  87.     public int getItemCount() {  
  88.         return listShop==null?0:listShop.size();  
  89.     }  
  90.   
  91.     public static class MyViewholder1 extends RecyclerView.ViewHolder {  
  92.   
  93.         private final ImageView ivDelete;  
  94.         private final TextView tvPrice;  
  95.         private final TextView tvName;  
  96.         private final ImageView ivSelect;  
  97.         CustomView customView;  
  98.         private final ImageView ivPic;  
  99.         private final CheckBox product_select;//每个条目前面的多选框,  
  100.         // private final TextView tvShopName;//商铺名称  
  101.   
  102.         public MyViewholder1(View itemView) {  
  103.             super(itemView);  
  104.             //找到所有的控件  
  105.             ivDelete = (ImageView) itemView.findViewById(R.id.iv_item_shopcart_cloth_delete);  
  106.             tvPrice = (TextView) itemView.findViewById(R.id.tv_item_shopcart_cloth_price);  
  107.             tvName = (TextView) itemView.findViewById(R.id.tv_item_shopcart_clothname);  
  108.            ivSelect = (ImageView) itemView.findViewById(R.id.shopcart_cloth_select);  
  109.             ivPic = (ImageView) itemView.findViewById(R.id.iv_item_shopcart_cloth_pic);  
  110.             product_select = (CheckBox) itemView.findViewById(R.id.cb_item_shopcart_clothselect);  
  111.             customView = (CustomView) itemView.findViewById(R.id.custom_View);  
  112.            // tvShopName = (TextView) itemView.findViewById(R.id.tv_item_shopcart_shopname);  
  113.   
  114.   
  115.         }  
  116.     }  
  117.   
  118.     //返回当前集合的方法  
  119.     public List<SecondBean.DataBean.ListBean> getList(){  
  120.         return listShop;  
  121.     }  
  122.     //实现多选框的监听  
  123.     CheckBoxListener checkBoxListener;  
  124.     public void setCheckBoxListener(CheckBoxListener checkBoxListener){  
  125.         this.checkBoxListener = checkBoxListener;  
  126.     }  
  127.   
  128.     //每个条目前面的多选框点击的监听  
  129.     public interface CheckBoxListener{  
  130.         public void checkBox(List<SecondBean.DataBean.ListBean> list);  
  131.     }  
  132.   
  133.     //实现加减号 改变数量和总价  
  134.      public interface CustomViewListener{  
  135.         public void customClick(List<SecondBean.DataBean.ListBean> list);  
  136.     }  
  137.     //实现加减号 的监听  
  138.     CustomViewListener customViewListener;  
  139.     public void setCustomViewListener(CustomViewListener customViewListener){  
  140.         this.customViewListener = customViewListener;  
  141.     }  
  142.   
  143.     //实现删除按钮的点击事件  
  144.     public interface DelItemListener{  
  145.         public void delItem(List<SecondBean.DataBean.ListBean> list);  
  146.     }  
  147.     DelItemListener delItemListener;  
  148.     public void setDelItemListener(DelItemListener delItemListener){  
  149.         this.delItemListener = delItemListener;  
  150.     }  
  151.   
  152. }  

Mainactivity里面的,,求总价 求总数量  实现recyAdapter里面的接口

[html] view plain copy
  1. public class SecondActivity extends AppCompatActivity {  
  2.   
  3.     private TextView all_select;//全选按钮  
  4.     private TextView totalPrice;//总价  
  5.     private TextView totalNum;//总件数  
  6.     private List<SecondBean.DataBean.ListBean> listShop = new ArrayList<>();  
  7.     private SecondRecyAdapter secondRecyAdapter;  
  8.   
  9.     boolean mSelect = false;  
  10.     private RecyclerView second_recyview;  
  11.     private boolean shifoucheck;  
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_second);  
  16.   
  17.         //获取控件  
  18.   
  19.         second_recyview = (RecyclerView) findViewById(R.id.second_recyview);  
  20.         all_select = (TextView) findViewById(R.id.quanxuan);//全选按钮  
  21.         totalPrice = (TextView) findViewById(R.id.totalprice);  
  22.         totalNum = (TextView) findViewById(R.id.totalnum);  
  23.   
  24.        // secondRecyAdapter = new SecondRecyAdapter(SecondActivity.this,listShop);;  
  25.         LinearLayoutManager linearLayoutManager = new LinearLayoutManager(SecondActivity.this,LinearLayoutManager.VERTICAL,false);  
  26.         second_recyview.setLayoutManager(linearLayoutManager);  
  27.         //请求网络数据  
  28.         getData();  
  29.         //请求完网络数据 设置适配器  
  30.         if(secondRecyAdapter==null){  
  31.             secondRecyAdapter = new SecondRecyAdapter(SecondActivity.this,listShop);  
  32.             second_recyview.setAdapter(secondRecyAdapter);  
  33.         }else{  
  34.             secondRecyAdapter.notifyDataSetChanged();  
  35.         }  
  36.   
  37.         //进入页面先给总的全选按钮设置一个标记tag tag为1  不选中, tag为2选中  
  38.         all_select.setTag(1);//默认不选中  
  39.   
  40.         //为了通过all_select改变 每条数据的选中状态  
  41.         shifoucheck = false;  
  42.         //全选按钮的点击事件  
  43.         all_select.setOnClickListener(new View.OnClickListener() {  
  44.             @Override  
  45.             public void onClick(View v) {  
  46.                 //设置图片和字在一个控件里面,,图片的大小不会打乱  
  47.                    // Drawable left = getResources().getDrawable(R.drawable.shopcart_selected);  
  48.                    // all_select.setCompoundDrawablesWithIntrinsicBounds(left,null,null,null);  
  49.   
  50.                 //获取all_select 的tag  
  51.                 int tag = (int) all_select.getTag();  
  52.                 if(tag==1){  
  53.                     //如果是没选中,点击以后 改变成选中  
  54.                     all_select.setTag(2);  
  55.                     all_select.setBackgroundResource(R.drawable.shopcart_selected);  
  56.                     //控制所有的条目都改成 选中  
  57.                     shifoucheck = true;  
  58.                 }else{  
  59.                     //如果当前是选中的 就设置成未选中  
  60.                     all_select.setTag(1);  
  61.                     all_select.setBackgroundResource(R.drawable.shopcart_unselected);  
  62.                     shifoucheck = false;  
  63.                 }  
  64.   
  65.                 //然后遍历集合里面的所有条目都设置成选中  
  66.                 for (SecondBean.DataBean.ListBean bean : listShop){  
  67.                     //给数据源里面的check 标记为 上面改变的  
  68.                     bean.setCheck(shifoucheck);  
  69.                 }  
  70.                 secondRecyAdapter.notifyDataSetChanged();//刷新适配器  
  71.                 sum(secondRecyAdapter.getList());//把适配器里面的集合传进去,求和  
  72.           }  
  73.         });  
  74.   
  75.         //调用适配器里面的选中多选框的监听  
  76.         secondRecyAdapter.setCheckBoxListener(new SecondRecyAdapter.CheckBoxListener() {  
  77.             @Override  
  78.             public void checkBox(List<SecondBean.DataBean.ListBean> list) {  
  79.                 //调用求和的方法  
  80.                 sum(list);  
  81.             }  
  82.         });  
  83.   
  84.         //调用适配器里面的 加减号的监听  
  85.         secondRecyAdapter.setCustomViewListener(new SecondRecyAdapter.CustomViewListener() {  
  86.             @Override  
  87.             public void customClick(List<SecondBean.DataBean.ListBean> list) {  
  88.                 sum(list);  
  89.             }  
  90.         });  
  91.   
  92.         //调用适配器里面的 删除当前条目的方法  
  93.         secondRecyAdapter.setDelItemListener(new SecondRecyAdapter.DelItemListener() {  
  94.             @Override  
  95.             public void delItem(List<SecondBean.DataBean.ListBean> list) {  
  96.                 sum(list);  
  97.             }  
  98.         });  
  99.   
  100.     }  
  101.   
  102.   
  103.     //求总价的总数量的方法  
  104.     public void sum(List<SecondBean.DataBean.ListBean> listShop){  
  105.         float price = 0;  
  106.          int count = 0;  
  107.         //反着全选的控制,,,控制下面的总的 全选按钮  
  108.         boolean allCheck = true;  
  109.         for (SecondBean.DataBean.ListBean bean : listShop){  
  110.             if(bean.isCheck()){  
  111.                 //如果当前的check为true.计算总价  
  112.                 price += bean.getPrice()*bean.getNum();  
  113.                 count += bean.getNum();  
  114.             }else{  
  115.                 //只要有一个为没选中,就标记,改变下面的全选按钮  
  116.                 allCheck = false;  
  117.   
  118.             }  
  119.   
  120.             //设置下面的总价和数量  
  121.             totalNum.setText("共"+count+"件商品");  
  122.             totalPrice.setText("总价: "+price);  
  123.         }  
  124.   
  125.         //根据allcheck 改变下面的全选按钮的值  
  126.         if(allCheck){  
  127.             //如果遍历出来的所有check都是true,就全选也选中  
  128.             all_select.setTag(2);  
  129.             all_select.setBackgroundResource(R.drawable.shopcart_selected);  
  130.         }else{  
  131.             all_select.setTag(1);  
  132.             all_select.setBackgroundResource(R.drawable.shopcart_unselected);  
  133.         }  
  134.   
  135.     }  
  136.   
  137.   
  138.     //请求网络数据解析  
  139.     private void getData() {  
  140.         String path = "http://120.27.23.105/product/getCarts?uid=100";  
  141.         OkhttpUtils.getInstance().asy(null, path, new AbstractUiCallBack<SecondBean>() {  
  142.   
  143.              @Override  
  144.             public void success(SecondBean secondBean) {  
  145.                // System.out.println(secondBean.getData().get(0).getList().get(0).getTitle());  
  146.                //添加到集合里 data里面的list  
  147.                 for(int i=0;i<secondBean.getData().size();i++){  
  148.                     //将集合里面的list集合添加到大集合里去  
  149.                     listShop.addAll(secondBean.getData().get(i).getList());  
  150.                 }  
  151.                 // System.out.println(listShop.get(0).getTitle());  
  152.                 //设置适配器  
  153.                /*if(secondRecyAdapter==null){  
  154.                     secondRecyAdapter = new SecondRecyAdapter(SecondActivity.this,listShop);  
  155.                 second_recyview.setAdapter(secondRecyAdapter);  
  156.                 }else{  
  157.                     secondRecyAdapter.notifyDataSetChanged();  
  158.                 }*/  
  159.   
  160.             }  
  161.   
  162.             @Override  
  163.             public void failure(Exception e) {  
  164.   
  165.             }  
  166.         });  
  167.     }  
  168. }  
新建类继承application

[html] view plain copy
  1. public class App extends Application{  
  2.     @Override  
  3.     public void onCreate() {  
  4.         super.onCreate();  
  5.   
  6.         ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this).build();  
  7.         ImageLoader.getInstance().init(configuration);  
  8.     }  
  9. }  
清单文件中声明

[html] view plain copy
  1.  <application  
  2.         android:name=".aplica.App"  
  3.  </application>  

原创粉丝点击