Android 购物车页面和逻辑实现

距离上次写第一篇博客已经好久了,今天正好有时间就来写一篇关于最近在做的项目中的一个功能 购物车! 我这个购物车业务逻辑还算可以吧,不算太难,但由于我是第一次做,所以也碰到了很多细节上的问题...所以我想总结下来,方便以后学习和使用..好了先看看效果吧!



目前我做的功能除了结算就这些了...

下面开始来看代码

Activity界面是这样的


首先是Activity 布局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:layout_height="match_parent"  
  5.     android:orientation="vertical">  
  6.   
  7.     <include  
  8.         android:id="@+id/shopping_title"  
  9.         layout="@layout/layout_title" />  
  10.   
  11.     <TextView  
  12.         android:id="@+id/tv_edit"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="25dp"  
  15.         android:layout_gravity="right"  
  16.         android:layout_margin="10dp"  
  17.         android:text="编辑"  
  18.         android:textSize="18dp" />  
  19.   
  20.   
  21.     <View  
  22.         android:layout_width="match_parent"  
  23.         android:layout_height="1dp"  
  24.         android:layout_below="@id/tv_edit"  
  25.         android:background="@color/gray3" />  
  26.   
  27.   
  28.     <ListView  
  29.         android:id="@+id/list_shopping_cart"  
  30.         android:layout_width="match_parent"  
  31.         android:layout_height="0dp"  
  32.         android:layout_below="@id/tv_edit"  
  33.         android:layout_weight="1"  
  34.         android:scrollbars="none" />  
  35.   
  36.     <View  
  37.         android:layout_width="match_parent"  
  38.         android:layout_height="1dp"  
  39.         android:background="@color/gray3" />  
  40.   
  41.     <RelativeLayout  
  42.         android:id="@+id/rl_bottom"  
  43.         android:layout_width="match_parent"  
  44.         android:layout_height="50dp"  
  45.         android:layout_alignParentBottom="true"  
  46.         android:background="@color/white">  
  47.   
  48.   
  49.         <CheckBox  
  50.         android:id="@+id/ck_all"  
  51.         android:layout_width="wrap_content"  
  52.         android:layout_height="match_parent"  
  53.         android:layout_centerVertical="true"  
  54.         android:button="@drawable/check_box_style"  
  55.         android:checkMark="?android:attr/listChoiceIndicatorMultiple"  
  56.         android:gravity="center"  
  57.         android:paddingLeft="10dp"  
  58.         android:scaleX="0.6"  
  59.         android:scaleY="0.6"  
  60.         android:text="全选"  
  61.         android:textAppearance="?android:attr/textAppearanceLarge"  
  62.         android:textColor="@color/desccolor" />  
  63.   
  64.           
  65.         <TextView  
  66.             android:id="@+id/tv_settlement"  
  67.             android:layout_width="80dp"  
  68.             android:layout_height="match_parent"  
  69.             android:layout_alignParentRight="true"  
  70.             android:background="@color/desccolor"  
  71.             android:gravity="center"  
  72.             android:text="结算(0)"  
  73.             android:textColor="@color/white" />  
  74.   
  75.   
  76.         <TextView  
  77.             android:id="@+id/tv_show_price"  
  78.             android:layout_width="wrap_content"  
  79.             android:layout_height="match_parent"  
  80.             android:layout_toLeftOf="@id/tv_settlement"  
  81.             android:gravity="center"  
  82.             android:padding="5dp"  
  83.             android:text="合计:0.00"  
  84.             android:textColor="@color/desccolor" />  
  85.     </RelativeLayout>  
  86.   
  87. </LinearLayout>  
再来看 ListView item的布局



item 布局xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="190dp"  
  5.     android:orientation="vertical">  
  6.   
  7.     <CheckBox  
  8.         android:id="@+id/ck_chose"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_centerVertical="true"  
  12.         android:layout_marginLeft="8dp"  
  13.         android:button="@drawable/check_box_style"  
  14.         android:scaleX="0.6"  
  15.         android:scaleY="0.6" />  
  16.   
  17.     <ImageView  
  18.         android:id="@+id/iv_show_pic"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_centerVertical="true"  
  22.         android:layout_marginLeft="10dp"  
  23.         android:layout_toRightOf="@id/ck_chose"  
  24.         android:background="@mipmap/demo" />  
  25.   
  26.     <LinearLayout  
  27.         android:layout_width="wrap_content"  
  28.         android:layout_height="wrap_content"  
  29.         android:layout_marginLeft="15dp"  
  30.         android:layout_marginTop="30dp"  
  31.         android:layout_toRightOf="@id/iv_show_pic"  
  32.         android:orientation="vertical">  
  33.   
  34.         <TextView  
  35.             android:id="@+id/tv_commodity_name"  
  36.             android:layout_width="wrap_content"  
  37.             android:layout_height="wrap_content"  
  38.   
  39.             android:text="酒红色纯红色纯羊毛西服套装"  
  40.             android:textColor="@color/black"  
  41.             android:textStyle="bold" />  
  42.   
  43.         <RelativeLayout  
  44.             android:id="@+id/rl_edit"  
  45.             android:layout_width="110dp"  
  46.             android:layout_height="30dp"  
  47.             android:orientation="horizontal"  
  48.             android:visibility="gone">  
  49.   
  50.             <ImageView  
  51.                 android:id="@+id/iv_sub"  
  52.                 android:layout_width="wrap_content"  
  53.                 android:layout_height="wrap_content"  
  54.                 android:background="@mipmap/iv_sub" />  
  55.   
  56.             <TextView  
  57.                 android:id="@+id/tv_show_num"  
  58.                 android:layout_width="wrap_content"  
  59.                 android:layout_height="wrap_content"  
  60.                 android:layout_centerHorizontal="true"  
  61.                 android:layout_centerVertical="true"  
  62.                 android:text="1"  
  63.                 android:textColor="@color/desccolor" />  
  64.   
  65.             <ImageView  
  66.                 android:id="@+id/iv_add"  
  67.                 android:layout_width="wrap_content"  
  68.                 android:layout_height="wrap_content"  
  69.                 android:layout_alignParentRight="true"  
  70.                 android:background="@mipmap/iv_add" />  
  71.   
  72.             <View  
  73.                 android:layout_width="match_parent"  
  74.                 android:layout_height="0.7dp"  
  75.                 android:layout_alignParentBottom="true"  
  76.                 android:background="@color/black" />  
  77.         </RelativeLayout>  
  78.   
  79.   
  80.         <TextView  
  81.             android:id="@+id/tv_fabric"  
  82.             android:layout_width="wrap_content"  
  83.             android:layout_height="wrap_content"  
  84.             android:layout_marginTop="10dp"  
  85.             android:text="面料:"  
  86.             android:textColor="@color/gray5" />  
  87.   
  88.         <LinearLayout  
  89.             android:layout_width="wrap_content"  
  90.             android:layout_height="wrap_content"  
  91.             android:orientation="horizontal">  
  92.   
  93.             <TextView  
  94.                 android:id="@+id/tv_dress"  
  95.                 android:layout_width="wrap_content"  
  96.                 android:layout_height="wrap_content"  
  97.                 android:layout_marginTop="10dp"  
  98.                 android:text="西服尺寸: 48"  
  99.                 android:textColor="@color/gray5" />  
  100.   
  101.         </LinearLayout>  
  102.   
  103.   
  104.         <TextView  
  105.             android:id="@+id/tv_pants"  
  106.             android:layout_width="wrap_content"  
  107.             android:layout_height="wrap_content"  
  108.             android:layout_marginTop="10dp"  
  109.             android:text="西裤尺寸: 68"  
  110.             android:textColor="@color/gray5" />  
  111.   
  112.         <LinearLayout  
  113.             android:layout_width="wrap_content"  
  114.             android:layout_height="wrap_content"  
  115.             android:layout_marginTop="10dp"  
  116.             android:orientation="horizontal">  
  117.   
  118.             <TextView  
  119.                 android:id="@+id/tv_price"  
  120.                 android:layout_width="wrap_content"  
  121.                 android:layout_height="wrap_content"  
  122.                 android:text="¥390"  
  123.                 android:textColor="@color/black"  
  124.                 android:textStyle="bold" />  
  125.   
  126.   
  127.             <TextView  
  128.                 android:id="@+id/tv_num"  
  129.                 android:layout_width="wrap_content"  
  130.                 android:layout_height="wrap_content"  
  131.                 android:layout_marginLeft="40dp"  
  132.                 android:text="x1"  
  133.                 android:textColor="@color/gray5" />  
  134.         </LinearLayout>  
  135.     </LinearLayout>  
  136.   
  137.     <TextView  
  138.         android:id="@+id/tv_delete"  
  139.         android:layout_width="60dp"  
  140.         android:layout_height="match_parent"  
  141.         android:layout_alignParentRight="true"  
  142.         android:background="@color/address_press"  
  143.         android:gravity="center"  
  144.         android:text="删除"  
  145.         android:visibility="gone" />  
  146.   
  147. </RelativeLayout>  
好了 ,,布局就说完了 现在我们来看看逻辑部分

由于很多操作是在Activity中操作 ListView的item ,所以我这里选择的是接口回调,我感觉方便些..也许你会有更好的方法.

首先我们在 Adapter中定义了几个

[java]  view plain  copy
  1. /** 
  2.  * 复选框接口 
  3.  */  
  4. public interface CheckInterface {  
  5.     /** 
  6.      * 组选框状态改变触发的事件 
  7.      * 
  8.      * @param position  元素位置 
  9.      * @param isChecked 元素选中与否 
  10.      */  
  11.     void checkGroup(int position, boolean isChecked);  
  12. }  
  13.   
  14.   
  15. /** 
  16.  * 改变数量的接口 
  17.  */  
  18. public interface ModifyCountInterface {  
  19.     /** 
  20.      * 增加操作 
  21.      * 
  22.      * @param position      组元素位置 
  23.      * @param showCountView 用于展示变化后数量的View 
  24.      * @param isChecked     子元素选中与否 
  25.      */  
  26.     void doIncrease(int position, View showCountView, boolean isChecked);  
  27.   
  28.     /** 
  29.      * 删减操作 
  30.      * 
  31.      * @param position      组元素位置 
  32.      * @param showCountView 用于展示变化后数量的View 
  33.      * @param isChecked     子元素选中与否 
  34.      */  
  35.     void doDecrease(int position, View showCountView, boolean isChecked);  
  36.   
  37.     /** 
  38.      * 删除子item 
  39.      * 
  40.      * @param position 
  41.      */  
  42.     void childDelete(int position);  
  43. }  


再来看看Adapter 没啥可说的.. 注释我在代码写的还算详细 ,相信能看懂

[java]  view plain  copy
  1. public class ShoppingCartAdapter extends BaseAdapter {  
  2.   
  3.     private boolean isShow = true;//是否显示编辑/完成  
  4.     private List<ShoppingCartBean> shoppingCartBeanList;  
  5.     private CheckInterface checkInterface;  
  6.     private ModifyCountInterface modifyCountInterface;  
  7.     private Context context;  
  8.   
  9.     public ShoppingCartAdapter(Context context) {  
  10.         this.context = context;  
  11.     }  
  12.   
  13.     public void setShoppingCartBeanList(List<ShoppingCartBean> shoppingCartBeanList) {  
  14.         this.shoppingCartBeanList = shoppingCartBeanList;  
  15.         notifyDataSetChanged();  
  16.     }  
  17.   
  18.     /** 
  19.      * 单选接口 
  20.      * 
  21.      * @param checkInterface 
  22.      */  
  23.     public void setCheckInterface(CheckInterface checkInterface) {  
  24.         this.checkInterface = checkInterface;  
  25.     }  
  26.   
  27.     /** 
  28.      * 改变商品数量接口 
  29.      * 
  30.      * @param modifyCountInterface 
  31.      */  
  32.     public void setModifyCountInterface(ModifyCountInterface modifyCountInterface) {  
  33.         this.modifyCountInterface = modifyCountInterface;  
  34.     }  
  35.   
  36.     @Override  
  37.     public int getCount() {  
  38.         return shoppingCartBeanList == null ? 0 : shoppingCartBeanList.size();  
  39.     }  
  40.   
  41.     @Override  
  42.     public Object getItem(int position) {  
  43.         return shoppingCartBeanList.get(position);  
  44.     }  
  45.   
  46.     @Override  
  47.     public long getItemId(int position) {  
  48.         return position;  
  49.     }  
  50.   
  51.   
  52.     /** 
  53.      * 是否显示可编辑 
  54.      * 
  55.      * @param flag 
  56.      */  
  57.     public void isShow(boolean flag) {  
  58.         isShow = flag;  
  59.         notifyDataSetChanged();  
  60.     }  
  61.   
  62.     @Override  
  63.     public View getView(final int position, View convertView, ViewGroup parent) {  
  64.   
  65.         final ViewHolder holder;  
  66.         if (convertView == null) {  
  67.             convertView = LayoutInflater.from(context).inflate(R.layout.item_shopping_cart_layout, parent, false);  
  68.             holder = new ViewHolder(convertView);  
  69.             convertView.setTag(holder);  
  70.         } else {  
  71.             holder = (ViewHolder) convertView.getTag();  
  72.         }  
  73.         final ShoppingCartBean shoppingCartBean = shoppingCartBeanList.get(position);  
  74.         holder.tv_commodity_name.setText(shoppingCartBean.getShoppingName());  
  75.         holder.tv_fabric.setText("面料:" + shoppingCartBean.getFabric());  
  76.         holder.tv_dress.setText("西服尺寸:" + shoppingCartBean.getDressSize());  
  77.         holder.tv_pants.setText("西裤尺寸:" + shoppingCartBean.getPantsSize());  
  78.         holder.tv_price.setText("¥:" + shoppingCartBean.getPrice());  
  79.         holder.ck_chose.setChecked(shoppingCartBean.isChoosed());  
  80.         holder.tv_show_num.setText(shoppingCartBean.getCount() + "");  
  81.         holder.tv_num.setText("X" + shoppingCartBean.getCount());  
  82.   
  83.         //单选框按钮  
  84.         holder.ck_chose.setOnClickListener(  
  85.                 new View.OnClickListener() {  
  86.                     @Override  
  87.                     public void onClick(View v) {  
  88.                         shoppingCartBean.setChoosed(((CheckBox) v).isChecked());  
  89.                         checkInterface.checkGroup(position, ((CheckBox) v).isChecked());//向外暴露接口  
  90.                     }  
  91.                 }  
  92.         );  
  93.   
  94.         //增加按钮  
  95.         holder.iv_add.setOnClickListener(new View.OnClickListener() {  
  96.             @Override  
  97.             public void onClick(View v) {  
  98.                 modifyCountInterface.doIncrease(position, holder.tv_show_num, holder.ck_chose.isChecked());//暴露增加接口  
  99.             }  
  100.         });  
  101.   
  102.         //删减按钮  
  103.         holder.iv_sub.setOnClickListener(new View.OnClickListener() {  
  104.             @Override  
  105.             public void onClick(View v) {  
  106.                 modifyCountInterface.doDecrease(position, holder.tv_show_num, holder.ck_chose.isChecked());//暴露删减接口  
  107.             }  
  108.         });  
  109.   
  110.   
  111.         //删除弹窗  
  112.         holder.tv_delete.setOnClickListener(new View.OnClickListener() {  
  113.             @Override  
  114.             public void onClick(View v) {  
  115.                 AlertDialog alert = new AlertDialog.Builder(context).create();  
  116.                 alert.setTitle("操作提示");  
  117.                 alert.setMessage("您确定要将这些商品从购物车中移除吗?");  
  118.                 alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",  
  119.                         new DialogInterface.OnClickListener() {  
  120.                             @Override  
  121.                             public void onClick(DialogInterface dialog, int which) {  
  122.                                 return;  
  123.                             }  
  124.                         });  
  125.                 alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定",  
  126.                         new DialogInterface.OnClickListener() {  
  127.                             @Override  
  128.                             public void onClick(DialogInterface dialog, int which) {  
  129.                                 modifyCountInterface.childDelete(position);//删除 目前只是从item中移除  
  130.   
  131.                             }  
  132.                         });  
  133.                 alert.show();  
  134.             }  
  135.         });  
  136.   
  137.         //判断是否在编辑状态下  
  138.         if (isShow) {  
  139.             holder.tv_commodity_name.setVisibility(View.VISIBLE);  
  140.             holder.tv_fabric.setVisibility(View.VISIBLE);  
  141.             holder.rl_edit.setVisibility(View.GONE);  
  142.             holder.tv_delete.setVisibility(View.GONE);  
  143.         } else {  
  144.             holder.tv_commodity_name.setVisibility(View.GONE);  
  145.             holder.tv_fabric.setVisibility(View.GONE);  
  146.             holder.rl_edit.setVisibility(View.VISIBLE);  
  147.             holder.tv_delete.setVisibility(View.VISIBLE);  
  148.         }  
  149.   
  150.         return convertView;  
  151.     }  
  152.   
  153.   
  154.     //初始化控件  
  155.     class ViewHolder {  
  156.         ImageView iv_chose;  
  157.         ImageView iv_show_pic, iv_sub, iv_add;  
  158.         TextView tv_commodity_name, tv_fabric, tv_dress, tv_pants, tv_price, tv_num, tv_delete, tv_show_num;  
  159.         CheckBox ck_chose;  
  160.         RelativeLayout rl_edit;  
  161.   
  162.         public ViewHolder(View itemView) {  
  163.             ck_chose = (CheckBox) itemView.findViewById(R.id.ck_chose);  
  164.             iv_show_pic = (ImageView) itemView.findViewById(R.id.iv_show_pic);  
  165.             iv_sub = (ImageView) itemView.findViewById(R.id.iv_sub);  
  166.             iv_add = (ImageView) itemView.findViewById(R.id.iv_add);  
  167.   
  168.             tv_commodity_name = (TextView) itemView.findViewById(R.id.tv_commodity_name);  
  169.             tv_fabric = (TextView) itemView.findViewById(R.id.tv_fabric);  
  170.             tv_dress = (TextView) itemView.findViewById(R.id.tv_dress);  
  171.             tv_pants = (TextView) itemView.findViewById(R.id.tv_pants);  
  172.             tv_price = (TextView) itemView.findViewById(R.id.tv_price);  
  173.             tv_num = (TextView) itemView.findViewById(R.id.tv_num);  
  174.             tv_delete = (TextView) itemView.findViewById(R.id.tv_delete);  
  175.             tv_show_num = (TextView) itemView.findViewById(R.id.tv_show_num);  
  176.             rl_edit = (RelativeLayout) itemView.findViewById(R.id.rl_edit);  
  177.   
  178.         }  
  179.   
  180.     }  

现在我们在来看看Activity 代码, 同样注释写的很详细

[java]  view plain  copy
  1. public class ShoppingCartActivity extends BaseActivity implements View.OnClickListener  
  2.         , ShoppingCartAdapter.CheckInterface, ShoppingCartAdapter.ModifyCountInterface {  
  3.     public TextView tv_title, tv_settlement, tv_show_price;  
  4.     private TextView tv_all_check;  
  5.     private CheckBox ck_all;  
  6.     private ListView list_shopping_cart;  
  7.     private ShoppingCartAdapter shoppingCartAdapter;  
  8.     private TextView tv_edit;  
  9.     private boolean flag = false;  
  10.     private List<ShoppingCartBean> shoppingCartBeanList = new ArrayList<>();  
  11.     private boolean mSelect;  
  12.     private double totalPrice = 0.00;// 购买的商品总价  
  13.     private int totalCount = 0;// 购买的商品总数量  
  14.     /** 
  15.      * 批量模式下,用来记录当前选中状态 
  16.      */  
  17.     private SparseArray<Boolean> mSelectState = new SparseArray<Boolean>();  
  18.   
  19.   
  20.     @Override  
  21.     protected int getLayout() {  
  22.         return R.layout.layout_shopping_cart_activity;  
  23.     }  
  24.   
  25.     @Override  
  26.     protected void initView() {  
  27.         tv_title = bindView(R.id.tv_title);  
  28.         tv_title.setText("购物车");  
  29.         list_shopping_cart = bindView(R.id.list_shopping_cart);  
  30. //        list_shopping_cart.setOnItemClickListener(this);  
  31.         ck_all = bindView(R.id.ck_all);  
  32.         ck_all.setOnClickListener(this);  
  33. //        ck_all.setOnCheckedChangeListener(this);  
  34.         tv_show_price = bindView(R.id.tv_show_price);  
  35.         tv_settlement = bindView(R.id.tv_settlement);  
  36.         tv_settlement.setOnClickListener(this);  
  37.         tv_edit = bindView(R.id.tv_edit);  
  38.         tv_edit.setOnClickListener(this);  
  39.         shoppingCartAdapter = new ShoppingCartAdapter(this);  
  40.         shoppingCartAdapter.setCheckInterface(this);  
  41.         shoppingCartAdapter.setModifyCountInterface(this);  
  42.         list_shopping_cart.setAdapter(shoppingCartAdapter);  
  43.         shoppingCartAdapter.setShoppingCartBeanList(shoppingCartBeanList);  
  44.   
  45.     }  
  46.   
  47.     @Override  
  48.     protected void initData() {  
  49.   
  50.         for (int i = 0; i < 6; i++) {  
  51.             ShoppingCartBean shoppingCartBean = new ShoppingCartBean();  
  52.             shoppingCartBean.setShoppingName("高端大气上档次的T桖");  
  53.             shoppingCartBean.setFabric("纯棉");  
  54.             shoppingCartBean.setDressSize(48);  
  55.             shoppingCartBean.setPantsSize(65);  
  56.             shoppingCartBean.setPrice(60);  
  57.             shoppingCartBean.setCount(2);  
  58.             shoppingCartBeanList.add(shoppingCartBean);  
  59.         }  
  60.   
  61.     }  
  62.   
  63.     @Override  
  64.     public void onClick(View v) {  
  65.         switch (v.getId()) {  
  66.             //全选按钮    
  67.             case R.id.ck_all:  
  68.                 if (shoppingCartBeanList.size() != 0) {  
  69.                     if (ck_all.isChecked()) {  
  70.                         for (int i = 0; i < shoppingCartBeanList.size(); i++) {  
  71.                             shoppingCartBeanList.get(i).setChoosed(true);  
  72.                         }  
  73.                         shoppingCartAdapter.notifyDataSetChanged();  
  74.                     } else {  
  75.                         for (int i = 0; i < shoppingCartBeanList.size(); i++) {  
  76.                             shoppingCartBeanList.get(i).setChoosed(false);  
  77.                         }  
  78.                         shoppingCartAdapter.notifyDataSetChanged();  
  79.                     }  
  80.                 }  
  81.                 statistics();  
  82.                 break;  
  83.             case R.id.tv_edit:  
  84.                 flag = !flag;  
  85.                 if (flag) {  
  86.                     tv_edit.setText("完成");  
  87.                     shoppingCartAdapter.isShow(false);  
  88.                 } else {  
  89.                     tv_edit.setText("编辑");  
  90.                     shoppingCartAdapter.isShow(true);  
  91.                 }  
  92.                 break;  
  93.         }  
  94.     }  
  95.   
  96.     /** 
  97.      * 单选 
  98.      * 
  99.      * @param position  组元素位置 
  100.      * @param isChecked 组元素选中与否 
  101.      */  
  102.     @Override  
  103.     public void checkGroup(int position, boolean isChecked) {  
  104.   
  105.         shoppingCartBeanList.get(position).setChoosed(isChecked);  
  106.   
  107.         if (isAllCheck())  
  108.             ck_all.setChecked(true);  
  109.         else  
  110.             ck_all.setChecked(false);  
  111.   
  112.         shoppingCartAdapter.notifyDataSetChanged();  
  113.         statistics();  
  114.     }  
  115.   
  116.   
  117.     /** 
  118.      * 遍历list集合 
  119.      * 
  120.      * @return 
  121.      */  
  122.     private boolean isAllCheck() {  
  123.   
  124.         for (ShoppingCartBean group : shoppingCartBeanList) {  
  125.             if (!group.isChoosed())  
  126.                 return false;  
  127.         }  
  128.         return true;  
  129.     }  
  130.   
  131.     /** 
  132.      * 统计操作 
  133.      * 1.先清空全局计数器<br> 
  134.      * 2.遍历所有子元素,只要是被选中状态的,就进行相关的计算操作 
  135.      * 3.给底部的textView进行数据填充 
  136.      */  
  137.     public void statistics() {  
  138.         totalCount = 0;  
  139.         totalPrice = 0.00;  
  140.         for (int i = 0; i < shoppingCartBeanList.size(); i++) {  
  141.             ShoppingCartBean shoppingCartBean = shoppingCartBeanList.get(i);  
  142.             if (shoppingCartBean.isChoosed()) {  
  143.                 totalCount++;  
  144.                 totalPrice += shoppingCartBean.getPrice() * shoppingCartBean.getCount();  
  145.             }  
  146.         }  
  147.         tv_show_price.setText("合计:" + totalPrice);  
  148.         tv_settlement.setText("结算(" + totalCount + ")");  
  149.     }  
  150.   
  151.     /** 
  152.      * 增加 
  153.      * 
  154.      * @param position      组元素位置 
  155.      * @param showCountView 用于展示变化后数量的View 
  156.      * @param isChecked     子元素选中与否 
  157.      */  
  158.     @Override  
  159.     public void doIncrease(int position, View showCountView, boolean isChecked) {  
  160.         ShoppingCartBean shoppingCartBean = shoppingCartBeanList.get(position);  
  161.         int currentCount = shoppingCartBean.getCount();  
  162.         currentCount++;  
  163.         shoppingCartBean.setCount(currentCount);  
  164.         ((TextView) showCountView).setText(currentCount + "");  
  165.         shoppingCartAdapter.notifyDataSetChanged();  
  166.         statistics();  
  167.     }  
  168.   
  169.     /** 
  170.      * 删减 
  171.      * 
  172.      * @param position      组元素位置 
  173.      * @param showCountView 用于展示变化后数量的View 
  174.      * @param isChecked     子元素选中与否 
  175.      */  
  176.     @Override  
  177.     public void doDecrease(int position, View showCountView, boolean isChecked) {  
  178.         ShoppingCartBean shoppingCartBean = shoppingCartBeanList.get(position);  
  179.         int currentCount = shoppingCartBean.getCount();  
  180.         if (currentCount == 1) {  
  181.             return;  
  182.         }  
  183.         currentCount--;  
  184.         shoppingCartBean.setCount(currentCount);  
  185.         ((TextView) showCountView).setText(currentCount + "");  
  186.         shoppingCartAdapter.notifyDataSetChanged();  
  187.         statistics();  
  188.   
  189.     }  
  190.   
  191.     /** 
  192.      * 删除 
  193.      * 
  194.      * @param position 
  195.      */  
  196.     @Override  
  197.     public void childDelete(int position) {  
  198.         shoppingCartBeanList.remove(position);  
  199.         shoppingCartAdapter.notifyDataSetChanged();  
  200.         statistics();  
  201.   
  202.     }  
  203.   
  204.   
  205. }  

整体的就是这样,,我说一下 我做的时候遇到的问题和解决办法

1.

问题:当我单选一个一个选中后,全选按钮也会自动选中 ,但是当我取消一个item后 全选按钮没有自动取消 (正常是只有有一个item没有选中 全选按钮是不会选中的)

解决:首先解决问题要找到原因所在 ,造成这个Bug的原因是我CheckBox的点击事件用的是setOnCheckedChangeListener 后来换成了setOnClickListener 就好了 .

两者都能实现对CheckBox的状态改变的监听,但一般情况下,用的更多的是setOnCheckedChangeListener。因为,当CheckBox的状态不是通过点击事件改变,而是通过其他的方式改变时,比如setCheck(),setOnClickListener无法完成此种情况下的监听。OnCheckChangedListener监听CheckBox的状态,无论来自你的onClick事件还是其他。

2.

问题: 就是在改变物品个数的是时候会出现复用!

解决: 原因就是我没有保存当前 改变后是值, 保存一下就OK了..

  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: 购物车页面是一个常见的电商应用中的重要页面,通过展示用户选择的商品清单,方便用户查看、编辑和结算购物车中的商品。 Android Studio 是一款专业的 Android 开发工具,提供了丰富的组件和功能,可以轻松实现购物车页面的设计与开发。 具体实现购物车页面可以参考以下步骤: 1. 创建布局文件:在 Android Studio 中创建一个新的布局文件,用于展示购物车页面的UI。可以使用各种组件如RecyclerView、TextView、Button等来显示商品信息、数量、价格等。 2. 绑定数据:在购物车页面的 Activity 或 Fragment 中,使用适配器(Adapter)将购物车数据绑定到布局文件中的组件上。适配器可以继承RecyclerView.Adapter类,实现数据的绑定与渲染。 3. 实现交互功能:为购物车页面添加相应的交互功能,比如点击商品跳转到商品详情页面、增加或减少商品数量、删除商品等。可以使用事件监听器来实现这些功能,并更新数据适配器以刷新页面。 4. 数据管理:购物车页面需要实时反映用户在其它页面(比如商品列表页面)上的操作,例如添加商品到购物车或从购物车中删除商品。可以通过数据库或全局变量来管理购物车数据的增删改查。 5. 结算功能:购物车页面通常提供结算功能。可以添加一个“结算”按钮,当用户点击按钮时,将购物车中的商品信息传递给后台服务器进行结算处理,并跳转到支付页面或者显示支付二维码。 通过以上步骤,您可以在 Android Studio 中实现一个基础的购物车页面。当然,具体的实现还取决于您的需求和设计风格,可以根据需要进行个性化的定制和优化。希望对您有所帮助! ### 回答2: 购物车页面是电商应用中非常常见的一个功能,Android Studio 是一款非常强大的集成开发环境,可以用来开发各种类型的 Android 应用程序。在 Android Studio 中实现购物车页面也是非常简单的。 首先,我们需要在 Android Studio 中创建一个新的项目,并添加一个购物车页面的布局文件。这可以通过在项目视图中右键点击“res”文件夹,选择“New”->“Layout resource file”来完成。在布局文件中,我们可以使用各种 Android 布局和控件来构建购物车页面界面,比如列表视图、文本视图、按钮等。 接下来,我们需要在购物车页面的 Java 文件中,编写逻辑代码来实现购物车的功能。首先,我们可以使用 RecyclerView 来展示购物车中的商品列表,这可以通过在布局文件中添加一个 RecyclerView 控件,并在 Java 文件中创建一个适配器来实现。适配器可以帮助我们展示购物车中的商品信息,并响应用户的操作,比如删除商品、修改商品数量等。 在购物车页面中,我们还可以添加其他的功能,比如结算购物车、清空购物车等。这些功能可以通过添加按钮和事件监听器来实现。当用户点击结算按钮时,我们可以计算购物车中商品的总价,并跳转到支付页面;当用户点击清空购物车按钮时,我们可以清空购物车中的商品列表。 除了基本的购物车功能,我们还可以通过与后端服务器进行交互,实现更复杂的功能,比如根据用户的收货地址计算运费、根据库存信息判断商品是否可购买等。这可以通过使用网络请求库来实现,比如 Retrofit 或 Volley,通过向服务器发送请求,接收服务器返回的数据,并进行相应的处理。 综上所述,通过使用 Android Studio ,我们可以方便地实现购物车页面的开发。通过布局文件和逻辑代码的编写,我们可以展示购物车中的商品列表,并实现各种购物车的功能。同时,我们还可以通过与后端服务器的交互,实现更复杂的功能。使用 Android Studio,开发购物车页面变得更加简单、快捷。 ### 回答3: Android Studio 是一款非常流行的用于开发Android应用程序的集成开发环境,而CSDN是国内知名的技术博客社区。在Android Studio中实现购物车页面,可以按照以下步骤进行操作: 1. 首先,创建一个新的Android项目,并在布局文件中设计购物车页面的UI。可以使用LinearLayout、RelativeLayout等布局容器,添加TextView、ImageView、Button等控件实现页面的显示和交互。 2. 在Java代码中,定义购物车页面逻辑,包括商品的添加、删除、数量的增减等功能。可以使用ArrayList或HashMap等数据结构存储购物车中的商品信息,并通过Adapter将数据与列表控件关联起来,实现商品列表的展示。 3. 添加监听器来处理用户的操作。例如,通过监听商品列表中删除按钮的点击事件,实现删除商品的功能;通过监听商品数量增减按钮的点击事件,实现购物数量的调整等。 4. 在购物车页面中,可以添加一些统计功能,如购物车中商品的总数量和总价格等。在Java代码中,对购物车中的商品进行遍历,并计算商品数量和价格的总和,在界面上展示出来。 5. 如果需要与后端服务器进行交互,可以使用网络请求库(如OkHttp、Retrofit等)发送请求。例如,将购物车中的商品信息发送给服务器进行后续处理,或者从服务器获取商品信息进行展示。 以上是使用Android Studio实现购物车页面的基本步骤。通过合理设计UI布局,处理用户的操作和展示数据,就可以实现一个功能完善的购物车页面。在CSDN中可以找到更多关于Android开发的教程和案例,帮助开发者更好地实现购物车功能。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值