Android 购物车UI及逻辑实现

时间:2023-01-16 04:40:35

转自:http://blog.csdn.net/u011011744/article/details/53538972先看看效果吧!

Android 购物车UI及逻辑实现


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

下面开始来看代码

Activity界面是这样的

Android 购物车UI及逻辑实现

首先是Activity 布局xml

  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>  
Android 购物车UI及逻辑实现
<?xml version=”1.0” encoding=”utf-8”?> 
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”vertical”>

&lt;include
    android:id="@+id/shopping_title"
    layout="@layout/layout_title" /&gt;

&lt;TextView
    android:id="@+id/tv_edit"
    android:layout_width="wrap_content"
    android:layout_height="25dp"
    android:layout_gravity="right"
    android:layout_margin="10dp"
    android:text="编辑"
    android:textSize="18dp" /&gt;


&lt;View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_below="@id/tv_edit"
    android:background="@color/gray3" /&gt;


&lt;ListView
    android:id="@+id/list_shopping_cart"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_below="@id/tv_edit"
    android:layout_weight="1"
    android:scrollbars="none" /&gt;

&lt;View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/gray3" /&gt;

&lt;RelativeLayout
    android:id="@+id/rl_bottom"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:background="@color/white"&gt;


    &lt;CheckBox
    android:id="@+id/ck_all"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_centerVertical="true"
    android:button="@drawable/check_box_style"
    android:checkMark="?android:attr/listChoiceIndicatorMultiple"
    android:gravity="center"
    android:paddingLeft="10dp"
    android:scaleX="0.6"
    android:scaleY="0.6"
    android:text="全选"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@color/desccolor" /&gt;


    &lt;TextView
        android:id="@+id/tv_settlement"
        android:layout_width="80dp"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:background="@color/desccolor"
        android:gravity="center"
        android:text="结算(0)"
        android:textColor="@color/white" /&gt;


    &lt;TextView
        android:id="@+id/tv_show_price"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toLeftOf="@id/tv_settlement"
        android:gravity="center"
        android:padding="5dp"
        android:text="合计:0.00"
        android:textColor="@color/desccolor" /&gt;
&lt;/RelativeLayout&gt;

</LinearLayout>再来看 ListView item的布局

Android 购物车UI及逻辑实现

item 布局xml

  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>  
Android 购物车UI及逻辑实现
<?xml version=”1.0” encoding=”utf-8”?> 
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”190dp”
android:orientation=”vertical”>

&lt;CheckBox
    android:id="@+id/ck_chose"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="8dp"
    android:button="@drawable/check_box_style"
    android:scaleX="0.6"
    android:scaleY="0.6" /&gt;

&lt;ImageView
    android:id="@+id/iv_show_pic"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="10dp"
    android:layout_toRightOf="@id/ck_chose"
    android:background="@mipmap/demo" /&gt;

&lt;LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="30dp"
    android:layout_toRightOf="@id/iv_show_pic"
    android:orientation="vertical"&gt;

    &lt;TextView
        android:id="@+id/tv_commodity_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="酒红色纯红色纯羊毛西服套装"
        android:textColor="@color/black"
        android:textStyle="bold" /&gt;

    &lt;RelativeLayout
        android:id="@+id/rl_edit"
        android:layout_width="110dp"
        android:layout_height="30dp"
        android:orientation="horizontal"
        android:visibility="gone"&gt;

        &lt;ImageView
            android:id="@+id/iv_sub"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@mipmap/iv_sub" /&gt;

        &lt;TextView
            android:id="@+id/tv_show_num"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="1"
            android:textColor="@color/desccolor" /&gt;

        &lt;ImageView
            android:id="@+id/iv_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:background="@mipmap/iv_add" /&gt;

        &lt;View
            android:layout_width="match_parent"
            android:layout_height="0.7dp"
            android:layout_alignParentBottom="true"
            android:background="@color/black" /&gt;
    &lt;/RelativeLayout&gt;


    &lt;TextView
        android:id="@+id/tv_fabric"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="面料:"
        android:textColor="@color/gray5" /&gt;

    &lt;LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"&gt;

        &lt;TextView
            android:id="@+id/tv_dress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="西服尺寸: 48"
            android:textColor="@color/gray5" /&gt;

    &lt;/LinearLayout&gt;


    &lt;TextView
        android:id="@+id/tv_pants"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="西裤尺寸: 68"
        android:textColor="@color/gray5" /&gt;

    &lt;LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal"&gt;

        &lt;TextView
            android:id="@+id/tv_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="¥390"
            android:textColor="@color/black"
            android:textStyle="bold" /&gt;


        &lt;TextView
            android:id="@+id/tv_num"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="40dp"
            android:text="x1"
            android:textColor="@color/gray5" /&gt;
    &lt;/LinearLayout&gt;
&lt;/LinearLayout&gt;

&lt;TextView
    android:id="@+id/tv_delete"
    android:layout_width="60dp"
    android:layout_height="match_parent"
    android:layout_alignParentRight="true"
    android:background="@color/address_press"
    android:gravity="center"
    android:text="删除"
    android:visibility="gone" /&gt;

</RelativeLayout>好了 ,,布局就说完了 现在我们来看看逻辑部分

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

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

  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. }  
Android 购物车UI及逻辑实现
    /* 
* 复选框接口
*/
public interface CheckInterface {
/**
* 组选框状态改变触发的事件
*
* @param position 元素位置
* @param isChecked 元素选中与否
*/
void checkGroup(int position, boolean isChecked);
}

/**
 * 改变数量的接口
 */
public interface ModifyCountInterface {
    /**
     * 增加操作
     *
     * @param position      组元素位置
     * @param showCountView 用于展示变化后数量的View
     * @param isChecked     子元素选中与否
     */
    void doIncrease(int position, View showCountView, boolean isChecked);

    /**
     * 删减操作
     *
     * @param position      组元素位置
     * @param showCountView 用于展示变化后数量的View
     * @param isChecked     子元素选中与否
     */
    void doDecrease(int position, View showCountView, boolean isChecked);

    /**
     * 删除子item
     *
     * @param position
     */
    void childDelete(int position);
}</pre>


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

  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.     }  
Android 购物车UI及逻辑实现
public class ShoppingCartAdapter extends BaseAdapter {

private boolean isShow = true;//是否显示编辑/完成
private List&lt;ShoppingCartBean&gt; shoppingCartBeanList;
private CheckInterface checkInterface;
private ModifyCountInterface modifyCountInterface;
private Context context;

public ShoppingCartAdapter(Context context) {
    this.context = context;
}

public void setShoppingCartBeanList(List&lt;ShoppingCartBean&gt; shoppingCartBeanList) {
    this.shoppingCartBeanList = shoppingCartBeanList;
    notifyDataSetChanged();
}

/**
 * 单选接口
 *
 * @param checkInterface
 */
public void setCheckInterface(CheckInterface checkInterface) {
    this.checkInterface = checkInterface;
}

/**
 * 改变商品数量接口
 *
 * @param modifyCountInterface
 */
public void setModifyCountInterface(ModifyCountInterface modifyCountInterface) {
    this.modifyCountInterface = modifyCountInterface;
}

@Override
public int getCount() {
    return shoppingCartBeanList == null ? 0 : shoppingCartBeanList.size();
}

@Override
public Object getItem(int position) {
    return shoppingCartBeanList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}


/**
 * 是否显示可编辑
 *
 * @param flag
 */
public void isShow(boolean flag) {
    isShow = flag;
    notifyDataSetChanged();
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    final ViewHolder holder;
    if (convertView == null) {
        convertView = LayoutInflater.from(context).inflate(R.layout.item_shopping_cart_layout, parent, false);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    final ShoppingCartBean shoppingCartBean = shoppingCartBeanList.get(position);
    holder.tv_commodity_name.setText(shoppingCartBean.getShoppingName());
    holder.tv_fabric.setText("面料:" + shoppingCartBean.getFabric());
    holder.tv_dress.setText("西服尺寸:" + shoppingCartBean.getDressSize());
    holder.tv_pants.setText("西裤尺寸:" + shoppingCartBean.getPantsSize());
    holder.tv_price.setText("¥:" + shoppingCartBean.getPrice());
    holder.ck_chose.setChecked(shoppingCartBean.isChoosed());
    holder.tv_show_num.setText(shoppingCartBean.getCount() + "");
    holder.tv_num.setText("X" + shoppingCartBean.getCount());

    //单选框按钮
    holder.ck_chose.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    shoppingCartBean.setChoosed(((CheckBox) v).isChecked());
                    checkInterface.checkGroup(position, ((CheckBox) v).isChecked());//向外暴露接口
                }
            }
    );

    //增加按钮
    holder.iv_add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            modifyCountInterface.doIncrease(position, holder.tv_show_num, holder.ck_chose.isChecked());//暴露增加接口
        }
    });

    //删减按钮
    holder.iv_sub.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            modifyCountInterface.doDecrease(position, holder.tv_show_num, holder.ck_chose.isChecked());//暴露删减接口
        }
    });


    //删除弹窗
    holder.tv_delete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog alert = new AlertDialog.Builder(context).create();
            alert.setTitle("操作提示");
            alert.setMessage("您确定要将这些商品从购物车中移除吗?");
            alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            return;
                        }
                    });
            alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            modifyCountInterface.childDelete(position);//删除 目前只是从item中移除

                        }
                    });
            alert.show();
        }
    });

    //判断是否在编辑状态下
    if (isShow) {
        holder.tv_commodity_name.setVisibility(View.VISIBLE);
        holder.tv_fabric.setVisibility(View.VISIBLE);
        holder.rl_edit.setVisibility(View.GONE);
        holder.tv_delete.setVisibility(View.GONE);
    } else {
        holder.tv_commodity_name.setVisibility(View.GONE);
        holder.tv_fabric.setVisibility(View.GONE);
        holder.rl_edit.setVisibility(View.VISIBLE);
        holder.tv_delete.setVisibility(View.VISIBLE);
    }

    return convertView;
}


//初始化控件
class ViewHolder {
    ImageView iv_chose;
    ImageView iv_show_pic, iv_sub, iv_add;
    TextView tv_commodity_name, tv_fabric, tv_dress, tv_pants, tv_price, tv_num, tv_delete, tv_show_num;
    CheckBox ck_chose;
    RelativeLayout rl_edit;

    public ViewHolder(View itemView) {
        ck_chose = (CheckBox) itemView.findViewById(R.id.ck_chose);
        iv_show_pic = (ImageView) itemView.findViewById(R.id.iv_show_pic);
        iv_sub = (ImageView) itemView.findViewById(R.id.iv_sub);
        iv_add = (ImageView) itemView.findViewById(R.id.iv_add);

        tv_commodity_name = (TextView) itemView.findViewById(R.id.tv_commodity_name);
        tv_fabric = (TextView) itemView.findViewById(R.id.tv_fabric);
        tv_dress = (TextView) itemView.findViewById(R.id.tv_dress);
        tv_pants = (TextView) itemView.findViewById(R.id.tv_pants);
        tv_price = (TextView) itemView.findViewById(R.id.tv_price);
        tv_num = (TextView) itemView.findViewById(R.id.tv_num);
        tv_delete = (TextView) itemView.findViewById(R.id.tv_delete);
        tv_show_num = (TextView) itemView.findViewById(R.id.tv_show_num);
        rl_edit = (RelativeLayout) itemView.findViewById(R.id.rl_edit);

    }

}


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

  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. }  
Android 购物车UI及逻辑实现
public class ShoppingCartActivity extends BaseActivity implements View.OnClickListener 
, ShoppingCartAdapter.CheckInterface, ShoppingCartAdapter.ModifyCountInterface {
public TextView tv_title, tv_settlement, tv_show_price;
private TextView tv_all_check;
private CheckBox ck_all;
private ListView list_shopping_cart;
private ShoppingCartAdapter shoppingCartAdapter;
private TextView tv_edit;
private boolean flag = false;
private List<ShoppingCartBean> shoppingCartBeanList = new ArrayList<>();
private boolean mSelect;
private double totalPrice = 0.00;// 购买的商品总价
private int totalCount = 0;// 购买的商品总数量
/**
* 批量模式下,用来记录当前选中状态
*/
private SparseArray<Boolean> mSelectState = new SparseArray<Boolean>();

@Override
protected int getLayout() {
    return R.layout.layout_shopping_cart_activity;
}

@Override
protected void initView() {
    tv_title = bindView(R.id.tv_title);
    tv_title.setText("购物车");
    list_shopping_cart = bindView(R.id.list_shopping_cart);

// list_shopping_cart.setOnItemClickListener(this);
ck_all = bindView(R.id.ck_all);
ck_all.setOnClickListener(this);
// ck_all.setOnCheckedChangeListener(this);
tv_show_price = bindView(R.id.tv_show_price);
tv_settlement = bindView(R.id.tv_settlement);
tv_settlement.setOnClickListener(this);
tv_edit = bindView(R.id.tv_edit);
tv_edit.setOnClickListener(this);
shoppingCartAdapter = new ShoppingCartAdapter(this);
shoppingCartAdapter.setCheckInterface(this);
shoppingCartAdapter.setModifyCountInterface(this);
list_shopping_cart.setAdapter(shoppingCartAdapter);
shoppingCartAdapter.setShoppingCartBeanList(shoppingCartBeanList);

}

@Override
protected void initData() {

    for (int i = 0; i &lt; 6; i++) {
        ShoppingCartBean shoppingCartBean = new ShoppingCartBean();
        shoppingCartBean.setShoppingName("高端大气上档次的T桖");
        shoppingCartBean.setFabric("纯棉");
        shoppingCartBean.setDressSize(48);
        shoppingCartBean.setPantsSize(65);
        shoppingCartBean.setPrice(60);
        shoppingCartBean.setCount(2);
        shoppingCartBeanList.add(shoppingCartBean);
    }

}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        //全选按钮  
        case R.id.ck_all:
            if (shoppingCartBeanList.size() != 0) {
                if (ck_all.isChecked()) {
                    for (int i = 0; i &lt; shoppingCartBeanList.size(); i++) {
                        shoppingCartBeanList.get(i).setChoosed(true);
                    }
                    shoppingCartAdapter.notifyDataSetChanged();
                } else {
                    for (int i = 0; i &lt; shoppingCartBeanList.size(); i++) {
                        shoppingCartBeanList.get(i).setChoosed(false);
                    }
                    shoppingCartAdapter.notifyDataSetChanged();
                }
            }
            statistics();
            break;
        case R.id.tv_edit:
            flag = !flag;
            if (flag) {
                tv_edit.setText("完成");
                shoppingCartAdapter.isShow(false);
            } else {
                tv_edit.setText("编辑");
                shoppingCartAdapter.isShow(true);
            }
            break;
    }
}

/**
 * 单选
 *
 * @param position  组元素位置
 * @param isChecked 组元素选中与否
 */
@Override
public void checkGroup(int position, boolean isChecked) {

    shoppingCartBeanList.get(position).setChoosed(isChecked);

    if (isAllCheck())
        ck_all.setChecked(true);
    else
        ck_all.setChecked(false);

    shoppingCartAdapter.notifyDataSetChanged();
    statistics();
}


/**
 * 遍历list集合
 *
 * @return
 */
private boolean isAllCheck() {

    for (ShoppingCartBean group : shoppingCartBeanList) {
        if (!group.isChoosed())
            return false;
    }
    return true;
}

/**
 * 统计操作
 * 1.先清空全局计数器&lt;br&gt;
 * 2.遍历所有子元素,只要是被选中状态的,就进行相关的计算操作
 * 3.给底部的textView进行数据填充
 */
public void statistics() {
    totalCount = 0;
    totalPrice = 0.00;
    for (int i = 0; i &lt; shoppingCartBeanList.size(); i++) {
        ShoppingCartBean shoppingCartBean = shoppingCartBeanList.get(i);
        if (shoppingCartBean.isChoosed()) {
            totalCount++;
            totalPrice += shoppingCartBean.getPrice() * shoppingCartBean.getCount();
        }
    }
    tv_show_price.setText("合计:" + totalPrice);
    tv_settlement.setText("结算(" + totalCount + ")");
}

/**
 * 增加
 *
 * @param position      组元素位置
 * @param showCountView 用于展示变化后数量的View
 * @param isChecked     子元素选中与否
 */
@Override
public void doIncrease(int position, View showCountView, boolean isChecked) {
    ShoppingCartBean shoppingCartBean = shoppingCartBeanList.get(position);
    int currentCount = shoppingCartBean.getCount();
    currentCount++;
    shoppingCartBean.setCount(currentCount);
    ((TextView) showCountView).setText(currentCount + "");
    shoppingCartAdapter.notifyDataSetChanged();
    statistics();
}

/**
 * 删减
 *
 * @param position      组元素位置
 * @param showCountView 用于展示变化后数量的View
 * @param isChecked     子元素选中与否
 */
@Override
public void doDecrease(int position, View showCountView, boolean isChecked) {
    ShoppingCartBean shoppingCartBean = shoppingCartBeanList.get(position);
    int currentCount = shoppingCartBean.getCount();
    if (currentCount == 1) {
        return;
    }
    currentCount--;
    shoppingCartBean.setCount(currentCount);
    ((TextView) showCountView).setText(currentCount + "");
    shoppingCartAdapter.notifyDataSetChanged();
    statistics();

}

/**
 * 删除
 *
 * @param position
 */
@Override
public void childDelete(int position) {
    shoppingCartBeanList.remove(position);
    shoppingCartAdapter.notifyDataSetChanged();
    statistics();

}

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

1.

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

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

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

2.

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

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


总体上就是这些了…….感兴趣的同学可以下载demo看看



Demo在这里—–>http://download.csdn.net/detail/u011011744/9706870