制定写死数据实现购物车删除
需要使用的图片
activity_main
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.bwie.test.taobao_master.MainActivity"><in.srain.cube.views.ptr.PtrFrameLayout android:background="#f1f1f1" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mPtrframe" xmlns:cube_ptr="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" cube_ptr:ptr_resistance="1.7" cube_ptr:ptr_ratio_of_header_height_to_refresh="1.2" cube_ptr:ptr_duration_to_close="300" cube_ptr:ptr_duration_to_close_header="2000" cube_ptr:ptr_keep_header_when_refresh="true" cube_ptr:ptr_pull_to_fresh="false" ><FrameLayout android:background="@color/white" android:layout_width="match_parent" android:layout_height="match_parent"><LinearLayout android:id="@+id/ll_cart" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"><ExpandableListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_weight="1" android:layout_height="0dp"/><!--分割线--> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:background="@color/gray" /><LinearLayout android:gravity="center" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="50dp"><CheckBox android:textSize="16sp" android:background="@null" android:clickable="true" android:drawablePadding="10dp" android:drawableLeft="@drawable/checkbox_bg" android:text="@string/all" android:id="@+id/all_checkBox" android:button="@null" android:minHeight="64dp" android:layout_marginStart="10dp" android:layout_gravity="center_vertical" android:layout_weight="0.3" android:layout_width="0dp" android:layout_height="wrap_content" /><LinearLayout android:gravity="center" android:orientation="horizontal" android:id="@+id/order_info" android:layout_weight="0.7" android:layout_width="0dp" android:layout_height="wrap_content"><LinearLayout android:layout_marginEnd="20dp" android:orientation="vertical" android:layout_weight="0.5" android:layout_width="0dp" android:layout_height="wrap_content"><LinearLayout android:gravity="end" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"><TextView android:textColor="@android:color/black" android:textSize="18sp" android:text="@string/order_total" android:layout_marginStart="5dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /><TextView android:id="@+id/total_price" android:text="¥0.00" android:textSize="18sp" android:textColor="@color/ic_taobao" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout><TextView android:gravity="right" android:text="不含运费" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout><TextView android:id="@+id/go_pay" android:textSize="16sp" android:textColor="@android:color/white" android:background="@color/ic_taobao" android:gravity="center" android:text="结算(0)" android:layout_weight="0.5" android:layout_width="0dp" android:layout_height="60dp" /></LinearLayout><LinearLayout android:visibility="gone" android:gravity="center" android:id="@+id/share_info" android:layout_weight="0.7" android:orientation="horizontal" android:layout_width="0dp" android:layout_height="match_parent"><TextView android:id="@+id/share_goods" android:textSize="16sp" android:textColor="@android:color/white" android:background="@color/ic_yellow" android:gravity="center" android:text="分享宝贝" android:layout_weight="0.3" android:layout_width="0dp" android:layout_height="match_parent" /><TextView android:layout_marginStart="1dp" android:id="@+id/collect_goods" android:textSize="16sp" android:textColor="@android:color/white" android:background="@color/ic_taobao" android:gravity="center" android:text="移到收藏夹" android:layout_weight="0.3" android:layout_width="0dp" android:layout_height="match_parent" /><TextView android:layout_marginStart="1dp" android:id="@+id/del_goods" android:textSize="16sp" android:textColor="@android:color/white" android:background="@color/ic_red" android:gravity="center" android:text="删除" android:layout_weight="0.3" android:layout_width="0dp" android:layout_height="match_parent" /></LinearLayout></LinearLayout></LinearLayout><include android:id="@+id/layout_empty_shopcart" android:visibility="gone" android:layout_width="match_parent" android:layout_height="match_parent" layout="@layout/empty_shopcart" /></FrameLayout></in.srain.cube.views.ptr.PtrFrameLayout> </LinearLayout>
acitonbar
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"><TextView android:layout_centerInParent="true" android:id="@+id/shoppingcat_num" android:textColor="@android:color/white" android:textSize="20sp" android:text="购物车(0)" android:layout_width="wrap_content" android:layout_height="wrap_content" /><Button android:layout_alignParentRight="true" android:textSize="18sp" android:background="@android:color/transparent" android:textColor="@android:color/white" android:text="@string/actionBar_edit" android:id="@+id/actionBar_edit" android:layout_width="60dp" android:layout_height="wrap_content" /> </RelativeLayout>
dialog_change_num
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="280dp" android:layout_height="170dp"><TextView android:padding="5dp" android:layout_marginStart="10dp" android:layout_marginTop="10dp" android:textColor="@color/grey_color1" android:textSize="18sp" android:text="修改购买数量" android:layout_width="match_parent" android:layout_height="wrap_content" /><RelativeLayout android:focusable="true" android:focusableInTouchMode="true" android:id="@+id/ll_change_num" android:gravity="center" android:layout_marginTop="20dp" android:layout_width="match_parent" android:layout_height="40dp"><TextView android:id="@+id/dialog_reduceNum" android:background="@drawable/cart_min" android:layout_width="36dp" android:layout_height="40dp" /><EditText android:id="@+id/dialog_num" android:background="@drawable/icon_cart_count" android:textSize="20sp" android:textStyle="bold" android:textColor="@color/ic_taobao" android:text="1" android:gravity="center" android:inputType="number" android:layout_toRightOf="@+id/dialog_reduceNum" android:layout_width="70dp" android:layout_height="40dp" /><TextView android:layout_toRightOf="@+id/dialog_num" android:background="@drawable/cart_max" android:id="@+id/dialog_increaseNum" android:layout_width="36dp" android:layout_height="40dp" /></RelativeLayout><LinearLayout android:layout_marginBottom="10dp" android:gravity="center" android:layout_marginTop="20dp" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="40dp"><TextView android:textStyle="bold" android:textColor="@color/white" android:background="@color/grey_color3" android:textSize="18sp" android:gravity="center" android:text="取消" android:id="@+id/dialog_Nbutton" android:layout_width="120dp" android:layout_height="match_parent" /><TextView android:textStyle="bold" android:background="@color/ic_red" android:textColor="@color/white" android:gravity="center" android:textSize="18sp" android:layout_marginStart="10dp" android:text="确定" android:id="@+id/dialog_Pbutton" android:layout_width="120dp" android:layout_height="match_parent" /></LinearLayout></LinearLayout>
empty_shopcart
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:gravity="center" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:gravity="center" android:layout_width="match_parent" android:layout_height="match_parent"><ImageView android:src="@drawable/empty_cart" android:layout_width="wrap_content" android:layout_height="wrap_content" /><TextView android:layout_marginTop="10dp" android:text="购物车还没有商品" android:layout_width="wrap_content" android:layout_height="wrap_content" /><TextView android:layout_marginTop="10dp" android:text="快去淘宝逛逛吧" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout></LinearLayout>
item_shopcat_group
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="@color/white" android:layout_width="match_parent" android:layout_height="match_parent"><RelativeLayout android:padding="1dp" android:background="@color/white" android:layout_width="match_parent" android:layout_height="wrap_content"><CheckBox android:id="@+id/store_checkBox" android:visibility="visible" android:layout_marginEnd="5dp" android:layout_marginStart="10dp" android:gravity="center" android:minWidth="32dp" android:minHeight="48dp" android:background="@null" android:button="@null" android:drawableLeft="@drawable/checkbox_bg" android:layout_width="wrap_content" android:layout_height="wrap_content" /><TextView android:textColor="@color/grey_color2" android:drawableRight="@drawable/right_arrow" android:drawablePadding="5dp" android:drawableLeft="@drawable/shop" android:textSize="18sp" android:text="小马的店铺" android:layout_marginTop="5dp" android:layout_centerVertical="true" android:layout_toRightOf="@id/store_checkBox" android:id="@+id/store_name" android:layout_width="wrap_content" android:layout_height="wrap_content" /><Button android:background="@color/transparent" android:textColor="@color/grey_color2" android:id="@+id/store_edit" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:text="编辑" android:textSize="16sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout> </LinearLayout>
item_shopcat_product
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"><LinearLayout android:background="@color/light_white" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"><CheckBox android:id="@+id/single_checkBox" android:visibility="visible" android:layout_marginEnd="5dp" android:layout_marginStart="10dp" android:gravity="center" android:minWidth="32dp" android:minHeight="64dp" android:background="@null" android:button="@null" android:drawableLeft="@drawable/checkbox_bg" android:layout_width="wrap_content" android:layout_height="wrap_content" /><ImageView android:layout_marginBottom="5dp" android:id="@+id/goods_image" android:layout_marginTop="5dp" android:scaleType="centerCrop" android:src="@drawable/cmaz" android:layout_width="100dp" android:layout_height="120dp"/><RelativeLayout android:padding="2dp" android:id="@+id/goods_data" android:layout_marginStart="10dp" android:layout_marginTop="5dp" android:layout_width="wrap_content" android:layout_height="match_parent"><TextView android:text="五月天【沉梦昂志】2050年演唱会 武汉站" android:textSize="15dp" android:textColor="@android:color/black" android:maxLines="2" android:ellipsize="end" android:id="@+id/goods_name" android:layout_width="match_parent" android:layout_height="wrap_content" /><TextView android:id="@+id/goods_size" android:paddingTop="5dp" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_centerVertical="true" android:text="门票:内场 类型:憨人" android:textSize="14dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /><RelativeLayout android:layout_marginBottom="15dp" android:layout_alignParentBottom="true" android:layout_centerVertical="true" android:layout_alignParentStart="true" android:layout_width="match_parent" android:layout_height="wrap_content"><TextView android:textSize="16.5sp" android:textStyle="bold" android:id="@+id/goods_price" android:textColor="@color/ic_taobao" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:lines="1" android:text="¥100000.00" /><TextView android:textSize="16sp" android:text="" android:layout_toRightOf="@id/goods_price" android:layout_marginStart="10dp" android:layout_alignParentEnd="true" android:id="@+id/goods_prime_price" android:layout_centerVertical="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /><TextView android:textSize="17sp" android:textColor="@android:color/black" android:text="x1" android:layout_marginEnd="10dp" android:layout_alignParentEnd="true" android:id="@+id/goods_buyNum" android:layout_centerVertical="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout></RelativeLayout><LinearLayout android:visibility="gone" android:layout_marginStart="10dp" android:layout_marginTop="5dp" android:orientation="horizontal" android:id="@+id/edit_goods_data" android:layout_width="match_parent" android:layout_height="match_parent"><LinearLayout android:layout_weight="0.7" android:orientation="vertical" android:layout_width="0dp" android:layout_height="match_parent"><RelativeLayout android:layout_marginTop="10dp" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"><TextView android:layout_alignParentStart="true" android:layout_centerVertical="true" android:gravity="center" android:textSize="20sp" android:text="—" android:id="@+id/reduce_goodsNum" android:layout_width="35dp" android:layout_height="35dp" /><TextView android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:gravity="center" android:textSize="20sp" android:text="1" android:id="@+id/goods_Num" android:layout_width="35dp" android:layout_height="35dp" /><TextView android:layout_alignParentEnd="true" android:layout_centerVertical="true" android:gravity="center" android:textSize="20sp" android:text="+" android:id="@+id/increase_goods_Num" android:layout_width="35dp" android:layout_height="35dp" /></RelativeLayout><TextView android:layout_marginStart="10dp" android:layout_marginTop="20dp" android:id="@+id/goodsSize" android:text="门票:内场 类型:憨人" android:layout_width="match_parent" android:layout_height="wrap_content"/></LinearLayout><TextView android:id="@+id/del_goods" android:textSize="16.5sp" android:gravity="center" android:background="@color/ic_red" android:textColor="@android:color/white" android:text="删除" android:layout_weight="0.3" android:layout_width="0dp" android:layout_height="match_parent" /></LinearLayout><View android:layout_width="match_parent" android:layout_height="2.5dp" android:background="@color/white"/></LinearLayout> </LinearLayout>
drawable文件夹下cart_max
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/icon_plus_press" android:state_pressed="true"/><item android:drawable="@drawable/icon_plus_normal" android:state_pressed="false"/> </selector>
cart_min
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/icon_minus_press" android:state_pressed="true"/><item android:drawable="@drawable/icon_minus_normal" android:state_pressed="false"/> </selector>
checkbox_bg
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/checkbox_true" android:state_checked="true"/><item android:drawable="@drawable/checkbox_false" android:state_checked="false"/> </selector>
checkbox_false
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp" android:height="24dp" android:viewportWidth="24.0" android:viewportHeight="24.0"><path android:pathData="M12,22.5c-5.79,0 -10.5,-4.71 -10.5,-10.5S6.21,1.5 12, 1.5 22.5,6.21 22.5,12 17.79,22.5 12,22.5zM12,3c-4.963,0 -9,4.037 -9, 9s4.037,9 9,9 9,-4.037 9,-9S16.963,3 12,3z" android:fillColor="#a9b7b7"/> </vector>
checkbox_true
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="24dp" android:height="24dp" android:viewportWidth="24.0" android:viewportHeight="24.0"><path android:pathData="M12,1.546c-5.764,0 -10.454,4.69 -10.454,10.454 0,5.765 4.689, 10.454 10.454,10.454S22.454,17.765 22.454,12C22.454,6.236 17.765,1.546 12, 1.546zM17.044,10.276 L11.039,16.346c-0.001,0.001 -0.005,0.002 -0.006,0.005 -0.002, 0.001 -0.002,0.005 -0.005,0.006 -0.048,0.046 -0.107,0.075 -0.163,0.107 -0.028, 0.016 -0.05,0.04 -0.08,0.051 -0.09,0.036 -0.185,0.055 -0.28,0.055 -0.096,0 -0.193, -0.019 -0.284,-0.056 -0.03,-0.013 -0.054,-0.038 -0.082,-0.054 -0.056,-0.031 -0.113, -0.059 -0.161,-0.107 -0.001,-0.001 -0.002,-0.005 -0.004,-0.006 -0.001,-0.002 -0.005, -0.002 -0.006,-0.005l-2.954,-3.035c-0.289,-0.297 -0.282,-0.772 0.015,-1.061 0.297, -0.288 0.771,-0.283 1.061,0.015l2.42,2.487 5.467,-5.527c0.291,-0.295 0.767, -0.298 1.061,-0.006C17.333,9.506 17.335,9.981 17.044,10.276z" android:fillColor="#eb4f38"/> </vector>
divider
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android1="http://schemas.android.com/tools"><solid android1:color="@android:color/darker_gray"/><size android1:height="0.2dp"/> </shape>
right_arrow
<vector android:height="24dp" android:viewportHeight="16.0" android:viewportWidth="16.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"><path android:fillColor="#a9b7b7" android:pathData="M11.9,7.65l-6.75,-6.8c-0.15,-0.15 -0.4,-0.15 -0.55,0 -0.15, 0.15 -0.15,0.4 0,0.55l6.5,6.5 -6.5,6.65c-0.15,0.15 -0.15,0.4 0,0.55s0.4, 0.15 0.55,0l6.75,-6.9C12.05,8.05 12.05,7.8 11.9,7.65z"/> </vector>
shop
<vector android:height="26dp" android:viewportHeight="28.0" android:viewportWidth="28.0" android:width="26dp" xmlns:android="http://schemas.android.com/apk/res/android"><path android:fillColor="#eb4f38" android:pathData="M23.625,16.887c-0.525,0 -0.875, 0.35 -0.875,0.875l0,5.338c0,0.263 -0.263,0.525 -0.525,0.525L5.775,23.625c-0.263, 0 -0.525,-0.263 -0.525,-0.525l0,-5.338c0,-0.525 -0.35,-0.875 -0.875,-0.875s-0.875, 0.35 -0.875,0.875l0,5.338C3.5,24.325 4.55,25.375 5.775,25.375l16.45,0c1.225, 0 2.275,-1.05 2.275,-2.275l0,-5.338c0,-0.525 -0.35,-0.875 -0.875,-0.875zM26.163, 8.662l-2.888,-5.6c-0.175,-0.263 -0.438,-0.438 -0.787,-0.438l-17.5,0c-0.35,0 -0.7, 0.263 -0.787,0.613l-2.275,5.688c-0.175,0.175 -0.087,0.613 -0.087,0.7l0,2.188c0, 2.188 1.837,4.025 4.025,4.025L7,15.838c1.313,0 2.362,-0.438 3.063,-1.225 0.7, 0.787 1.925,1.225 3.413,1.225l1.137,0c1.488,0 2.712,-0.438 3.413,-1.225 0.7, 0.7 1.75,1.137 3.063,1.137l1.137,0c2.188,0 4.025,-1.837 4.025,-4.025l0,-2.275c0, -0.087 0,-0.613 -0.087,-0.787zM5.6,4.375l16.363,0l1.837,3.5L4.112,7.875l1.488, -3.5zM22.225,14l-1.137,0c-0.525,0 -2.275,-0.087 -2.275,-1.75 0,-0.525 -0.35, -0.875 -0.875,-0.875s-0.875,0.35 -0.875,0.875l0,0.35c0,1.4 -2.1,1.488 -2.538, 1.488l-1.137,0c-0.438,0 -2.538,-0.087 -2.538,-1.488l0,-0.175 0,-0.087c0, -0.525 -0.35,-0.875 -0.875,-0.875s-0.875,0.35 -0.875,0.875c0,1.313 -1.137, 1.75 -2.275,1.75L5.775,14.088c-1.225,0 -2.275,-1.05 -2.275,-2.275L3.5,9.625l21, 0l0,2.1c0,1.225 -0.963,2.275 -2.275,2.275z"/> </vector>
values文件夹下dimens
<?xml version="1.0" encoding="utf-8"?> <resources><dimen name="activity_horizontal_margin">16dp</dimen><dimen name="activity_vertical_margin">16dp</dimen> </resources>
colors
<?xml version="1.0" encoding="utf-8"?> <resources><color name="colorPrimary">#3F51B5</color><color name="colorPrimaryDark">#303F9F</color><color name="colorAccent">#FF4081</color><color name="ic_taobao">#ea8010</color><color name="ic_yellow">#f4c600</color><color name="ic_red">#eb4f38</color><!-- 列表分割线 色值#999999 --> <color name="list_divider">#999999</color><!-- 选中态色块 色值#D7D7D7 --> <color name="list_item_pressed">#D7D7D7</color><color name="white">#ffffff</color><!-- 白色 --> <color name="ivory">#fffff0</color><!-- 象牙色 --> <color name="lightyellow">#ffffe0</color><color name="capture_text_cover_bg">#3060a4e3</color><!-- 亮黄色 --> <color name="yellow">#ffff00</color><!-- 黄色 --> <color name="snow">#fffafa</color><!-- 雪白色 --> <color name="floralwhite">#fffaf0</color><!-- 花白色 --> <color name="lemonchiffon">#fffacd</color><!-- 柠檬绸色 --> <color name="cornsilk">#fff8dc</color><!-- 米绸色 --> <color name="seaShell">#fff5ee</color><!-- 海贝色 --> <color name="lavenderblush">#fff0f5</color><!-- 淡紫红 --> <color name="papayawhip">#ffefd5</color><!-- 番木色 --> <color name="blanchedalmond">#ffebcd</color><!-- 白杏色 --> <color name="mistyrose">#ffe4e1</color><!-- 浅玫瑰色 --> <color name="bisque">#ffe4c4</color><!-- 桔黄色 --> <color name="moccasin">#ffe4b5</color><!-- 鹿皮色 --> <color name="navajowhite">#ffdead</color><!-- 纳瓦白 --> <color name="peachpuff">#ffdab9</color><!-- 桃色 --> <color name="gold">#ffd700</color><!-- 金色 --> <color name="pink">#ffc0cb</color><!-- 粉红色 --> <color name="lightpink">#ffb6c1</color><!-- 亮粉红色 --> <color name="orange">#fd7903</color><!-- 橙色 --> <color name="lightsalmon">#ffa07a</color><!-- 亮肉色 --> <color name="darkorange">#ff8c00</color><!-- 暗桔黄色 --> <color name="coral">#ff7f50</color><!-- 珊瑚色 --> <color name="hotpink">#ff69b4</color><!-- 热粉红色 --> <color name="tomato">#ff6347</color><!-- 西红柿色 --> <color name="orangered">#ff4500</color><!-- 红橙色 --> <color name="deeppink">#ff1493</color><!-- 深粉红色 --> <color name="fuchsia">#ff00ff</color><!-- 紫红色 --> <color name="magenta">#ff00ff</color><!-- 红紫色 --> <color name="red">#ff0000</color><!-- 红色 --> <color name="oldlace">#fdf5e6</color><!-- 老花色 --> <color name="lightgoldenrodyellow">#fafad2</color><!-- 亮金黄色 --> <color name="linen">#faf0e6</color><!-- 亚麻色 --> <color name="antiquewhite">#faebd7</color><!-- 古董白 --> <color name="salmon">#fa8072</color><!-- 鲜肉色 --> <color name="ghostwhite">#f8f8ff</color><!-- 幽灵白 --> <color name="mintcream">#f5fffa</color><!-- 薄荷色 --> <color name="whitesmoke">#f5f5f5</color><!-- 烟白色 --> <color name="beige">#f5f5dc</color><!-- 米色 --> <color name="wheat">#f5deb3</color><!-- 浅黄色 --> <color name="sandybrown">#f4a460</color><!-- 沙褐色 --> <color name="azure">#f0ffff</color><!-- 天蓝色 --> <color name="honeydew">#f0fff0</color><!-- 蜜色 --> <color name="aliceblue">#f0f8ff</color><!-- 艾利斯兰 --> <color name="khaki">#f0e68c</color><!-- 黄褐色 --> <color name="lightcoral">#f08080</color><!-- 亮珊瑚色 --> <color name="palegoldenrod">#eee8aa</color><!-- 苍麒麟色 --> <color name="violet">#ee82ee</color><!-- 紫罗兰色 --> <color name="darksalmon">#e9967a</color><!-- 暗肉色 --> <color name="lavender">#e6e6fa</color><!-- 淡紫色 --> <color name="lightcyan">#e0ffff</color><!-- 亮青色 --> <color name="burlywood">#deb887</color><!-- 实木色 --> <color name="plum">#dda0dd</color><!-- 洋李色 --> <color name="gainsboro">#dcdcdc</color><!-- 淡灰色 --> <color name="crimson">#dc143c</color><!-- 暗深红色 --> <color name="palevioletred">#db7093</color><!-- 苍紫罗兰色 --> <color name="goldenrod">#daa520</color><!-- 金麒麟色 --> <color name="orchid">#da70d6</color><!-- 淡紫色 --> <color name="thistle">#d8bfd8</color><!-- 蓟色 --> <color name="lightgray">#d3d3d3</color><!-- 亮灰色 --> <color name="lightgrey">#d3d3d3</color><!-- 亮灰色 --> <color name="tan">#d2b48c</color><!-- 茶色 --> <color name="chocolate">#d2691e</color><!-- 巧可力色 --> <color name="peru">#cd853f</color><!-- 秘鲁色 --> <color name="indianred">#cd5c5c</color><!-- 印第安红 --> <color name="mediumvioletred">#c71585</color><!-- 中紫罗兰色 --> <color name="silver">#c0c0c0</color><!-- 银色 --> <color name="darkkhaki">#bdb76b</color><!-- 暗黄褐色 --> <color name="rosybrown">#bc8f8f</color><!-- 褐玫瑰红 --> <color name="mediumorchid">#ba55d3</color><!-- 中粉紫色 --> <color name="darkgoldenrod">#b8860b</color><!-- 暗金黄色 --> <color name="firebrick">#b22222</color><!-- 火砖色 --> <color name="powderblue">#b0e0e6</color><!-- 粉蓝色 --> <color name="lightsteelblue">#b0c4de</color><!-- 亮钢兰色 --> <color name="paleturquoise">#afeeee</color><!-- 苍宝石绿 --> <color name="greenyellow">#adff2f</color><!-- 黄绿色 --> <color name="lightblue">#add8e6</color><!-- 亮蓝色 --> <color name="darkgray">#a9a9a9</color><!-- 暗灰色 --> <color name="darkgrey">#a9a9a9</color><!-- 暗灰色 --> <color name="brown">#a52a2a</color><!-- 褐色 --> <color name="sienna">#a0522d</color><!-- 赭色 --> <color name="darkorchid">#9932cc</color><!-- 暗紫色 --> <color name="palegreen">#98fb98</color><!-- 苍绿色 --> <color name="darkviolet">#9400d3</color><!-- 暗紫罗兰色 --> <color name="mediumpurple">#9370db</color><!-- 中紫色 --> <color name="lightgreen">#90ee90</color><!-- 亮绿色 --> <color name="darkseagreen">#8fbc8f</color><!-- 暗海兰色 --> <color name="saddlebrown">#8b4513</color><!-- 重褐色 --> <color name="darkmagenta">#8b008b</color><!-- 暗洋红 --> <color name="darkred">#8b0000</color><!-- 暗红色 --> <color name="blueviolet">#8a2be2</color><!-- 紫罗兰蓝色 --> <color name="lightskyblue">#87cefa</color><!-- 亮天蓝色 --> <color name="skyblue">#87ceeb</color><!-- 天蓝色 --> <color name="gray">#808080</color><!-- 灰色 --> <color name="grey">#7c7b7b</color><!-- 灰色 --> <color name="olive">#808000</color><!-- 橄榄色 --> <color name="purple">#800080</color><!-- 紫色 --> <color name="maroon">#800000</color><!-- 粟色 --> <color name="aquamarine">#7fffd4</color><!-- 碧绿色 --> <color name="chartreuse">#7fff00</color><!-- 黄绿色 --> <color name="lawngreen">#7cfc00</color><!-- 草绿色 --> <color name="mediumslateblue">#7b68ee</color><!-- 中暗蓝色 --> <color name="lightslategray">#778899</color><!-- 亮蓝灰 --> <color name="lightslategrey">#778899</color><!-- 亮蓝灰 --> <color name="slategray">#708090</color><!-- 灰石色 --> <color name="slategrey">#708090</color><!-- 灰石色 --> <color name="olivedrab">#6b8e23</color><!-- 深绿褐色 --> <color name="slateblue">#6a5acd</color><!-- 石蓝色 --> <color name="dimgray">#696969</color><!-- 暗灰色 --> <color name="dimgrey">#696969</color><!-- 暗灰色 --> <color name="mediumaquamarine">#66cdaa</color><!-- 中绿色 --> <color name="cornflowerblue">#6495ed</color><!-- 菊兰色 --> <color name="cadetblue">#5f9ea0</color><!-- 军兰色 --> <color name="darkolivegreen">#556b2f</color><!-- 暗橄榄绿 --> <color name="indigo">#4b0082</color><!-- 靛青色 --> <color name="mediumturquoise">#48d1cc</color><!-- 中绿宝石 --> <color name="darkslateblue">#483d8b</color><!-- 暗灰蓝色 --> <color name="steelblue">#4682b4</color><!-- 钢兰色 --> <color name="royalblue">#4169e1</color><!-- 皇家蓝 --> <color name="turquoise">#40e0d0</color><!-- 青绿色 --> <color name="mediumseagreen">#3cb371</color><!-- 中海蓝 --> <color name="limegreen">#32cd32</color><!-- 橙绿色 --> <color name="darkslategray">#2f4f4f</color><!-- 暗瓦灰色 --> <color name="darkslategrey">#2f4f4f</color><!-- 暗瓦灰色 --> <color name="seagreen">#2e8b57</color><!-- 海绿色 --> <color name="forestgreen">#228b22</color><!-- 森林绿 --> <color name="lightseagreen">#20b2aa</color><!-- 亮海蓝色 --> <color name="dodgerblue">#1e90ff</color><!-- 闪兰色 --> <color name="midnightblue">#191970</color><!-- 中灰兰色 --> <color name="aqua">#00ffff</color><!-- 浅绿色 --> <color name="cyan">#00ffff</color><!-- 青色 --> <color name="springgreen">#00ff7f</color><!-- 春绿色 --> <color name="lime">#00ff00</color><!-- 酸橙色 --> <color name="mediumspringgreen">#00fa9a</color><!-- 中春绿色 --> <color name="darkturquoise">#00ced1</color><!-- 暗宝石绿 --> <color name="deepskyblue">#00bfff</color><!-- 深天蓝色 --> <color name="darkcyan">#008b8b</color><!-- 暗青色 --> <color name="teal">#008080</color><!-- 水鸭色 --> <color name="green">#008000</color><!-- 绿色 --> <color name="darkgreen">#006400</color><!-- 暗绿色 --> <color name="blue">#005dc1</color><!-- 蓝色 --> <color name="mediumblue">#0000cd</color><!-- 中兰色 --> <color name="darkblue">#00008b</color><!-- 暗蓝色 --> <color name="navy">#000080</color><!-- 海军色 --> <color name="black">#3B3B3B</color><!-- 黑色 --> <!-- #2b4f6d 退改签 、选择配送信息中地址 文字的颜色值 --> <!-- 通用颜色统一风格 --> <color name="text_click">#0174E1</color><color name="table_background">#cbcbcb</color><color name="background">#eaeaea</color><color name="light_white">#fcfcfc</color><color name="tab_main_color">#1e1d1d</color><color name="comm_bg">#eaeaea</color><!-- 加黑 --> <color name="comm_text_black">#464646</color><!-- 常用左侧文字颜色 --> <color name="comm_text_left">#3c3c3c</color><!-- 常用右侧文字颜色 --> <color name="comm_text_right">#3c3c3c</color><color name="comm_text_red">#da1609</color><color name="comm_text_blue">#0f90e3</color><color name="comm_text_green">#66c058</color><color name="comm_text_yellow">#DAE532</color><color name="comm_button_blue">#0074E1</color><!-- 常用提示文字颜色 --> <color name="comm_text_tips">#949494</color><!-- 分割线颜色 --> <color name="comm_cutline">#c4c4c4</color><!-- 内容条目按下时的样式 --> <color name="pressed_bg">#f2f2f2</color><color name="nopressed_bg">#fff</color><!-- 助手内容颜色 --> <color name="title_content_color">#484848</color><color name="content_color">#7d7d7d</color><!-- 理财 --> <color name="new_red">#D21A3E</color><color name="new_red_press">#B41131</color><color name="text_black">#515151</color><color name="text_left">#646464</color><color name="text_right">#a1a1a1</color><color name="text_gray">#b5b5b5</color><color name="cutline_gray">#c4c4c4</color><color name="text_yellow">#b18500</color><color name="btn_bg_gray">#f7f7f7</color><color name="new_green">#00FF99</color><!-- 首页快捷菜单 --> <color name="menu_item_bg">#f6f6f6</color><color name="text_tips">#929292</color><!-- 账户总览 --> <color name="text_left_account">#464646</color><color name="text_bom_account">#464646</color><!-- 品质生活 商品列表 --> <color name="text_price_red">#ed3b3b</color><color name="text_sold_greay">#b0b0b0</color><color name="text_title_black">#232323</color><!-- 帮助中心-常见问题 --> <color name="text_question">#333333</color><color name="text_answer">#777777</color><color name="result_view">#b0000000</color><color name="viewfinder_mask">#60000000</color><color name="possible_result_points">#c0ffff00</color><color name="transparent">#00000000</color><color name="comm_card_bg">#ffffff</color><color name="grey_50">#fafafa</color><color name="grey_200">#eeeeee</color><color name="btn_blue">#0067db</color><!-- 右边菜单点击颜色 --> <color name="right_menu_unpressed">#CDCEC9</color><color name="right_menu_pressed">#C3C3C3</color><color name="menu_item_press">#2e000000</color><color name="page_backgroup">#f2f2f2</color><!-- 灰色 --> <color name="grey_color1">#333333</color><color name="grey_color2">#666666</color><color name="grey_color3">#999999</color><!-- 橙色 --> <color name="orange_color">#de6838</color> </resources>
strings
<resources><string name="app_name">TaoBao-Master</string><string name="shoppingcat">购物车</string><string name="actionBar_edit">编辑</string><string name="all">全选</string><string name="order_total">合计 :</string> </resources>
styles
<style name="myActionBarStyle" parent="Theme.AppCompat.Light.DarkActionBar"></style>
MainActivity
import android.content.Context; import android.content.DialogInterface; import android.support.v7.app.ActionBar; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.Button; import android.widget.CheckBox; import android.widget.ExpandableListView; import android.widget.LinearLayout; import android.widget.TextView;import com.bwie.test.adapter.ShopcatAdapter; import com.bwie.test.entity.GoodsInfo; import com.bwie.test.entity.StoreInfo; import com.bwie.test.utils.UtilTool; import com.bwie.test.utils.UtilsLog;import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Random;import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; import in.srain.cube.views.ptr.PtrClassicDefaultHeader; import in.srain.cube.views.ptr.PtrDefaultHandler; import in.srain.cube.views.ptr.PtrFrameLayout; import static in.srain.cube.views.ptr.util.PtrLocalDisplay.dp2px;public class MainActivity extends AppCompatActivity implements View.OnClickListener,ShopcatAdapter.CheckInterface, ShopcatAdapter.ModifyCountInterface, ShopcatAdapter.GroupEditorListener{@BindView(R.id.listView)ExpandableListView listView;@BindView(R.id.all_checkBox)CheckBox allCheckBox;@BindView(R.id.total_price)TextView totalPrice;@BindView(R.id.go_pay)TextView goPay;@BindView(R.id.order_info)LinearLayout orderInfo;@BindView(R.id.share_goods)TextView shareGoods;@BindView(R.id.collect_goods)TextView collectGoods;@BindView(R.id.del_goods)TextView delGoods;@BindView(R.id.share_info)LinearLayout shareInfo;@BindView(R.id.ll_cart)LinearLayout llCart;@BindView(R.id.mPtrframe)PtrFrameLayout mPtrFrame;TextView shoppingcatNum;Button actionBarEdit;LinearLayout empty_shopcart;private Context mcontext;private double mtotalPrice = 0.00;private int mtotalCount = 0;//false就是编辑,ture就是完成 private boolean flag = false;private ShopcatAdapter adapter;private List<StoreInfo> groups; //组元素的列表 private Map<String, List<GoodsInfo>> childs; //子元素的列表 @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ButterKnife.bind(this);initPtrFrame();initActionBar();initData();initEvents();}private void initPtrFrame() {final PtrClassicDefaultHeader header = new PtrClassicDefaultHeader(this);header.setPadding(dp2px(20), dp2px(20), 0, 0);mPtrFrame.setHeaderView(header);mPtrFrame.addPtrUIHandler(header);mPtrFrame.setPtrHandler(new PtrDefaultHandler() {@Override public void onRefreshBegin(PtrFrameLayout frame) {mPtrFrame.postDelayed(new Runnable() {@Override public void run() {mPtrFrame.refreshComplete();}},2000);}@Override public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);}});}private void initEvents() {actionBarEdit.setOnClickListener(this);adapter = new ShopcatAdapter(groups, childs, mcontext);adapter.setCheckInterface(this);//关键步骤1:设置复选框的接口 adapter.setModifyCountInterface(this); //关键步骤2:设置增删减的接口 adapter.setGroupEditorListener(this);//关键步骤3:监听组列表的编辑状态 listView.setGroupIndicator(null); //设置属性 GroupIndicator 去掉向下箭头 listView.setAdapter(adapter);for (int i = 0; i < adapter.getGroupCount(); i++) {listView.expandGroup(i); //关键步骤4:初始化,将ExpandableListView以展开的方式显示 }listView.setOnScrollListener(new AbsListView.OnScrollListener() {@Override public void onScrollStateChanged(AbsListView view, int scrollState) {}@Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {int firstVisiablePostion=view.getFirstVisiblePosition();int top=-1;View firstView=view.getChildAt(firstVisibleItem);UtilsLog.i("childCount="+view.getChildCount());//返回的是显示层面所包含的子view的个数 if(firstView!=null){top=firstView.getTop();}UtilsLog.i("firstVisiableItem="+firstVisibleItem+",fistVisiablePosition="+firstVisiablePostion+",firstView="+firstView+",top="+top);if(firstVisibleItem==0&&top==0){mPtrFrame.setEnabled(true);}else{mPtrFrame.setEnabled(false);}}});}@Override protected void onResume() {super.onResume();setCartNum();}//设置购物车的数量 private void setCartNum() {int count = 0;for (int i = 0; i < groups.size(); i++) {StoreInfo group = groups.get(i);group.setChoosed(allCheckBox.isChecked());List<GoodsInfo> Childs = childs.get(group.getId());for (GoodsInfo childs : Childs) {count++;}}//购物车已经清空 if (count == 0) {clearCart();} else {shoppingcatNum.setText("购物车(" + count + ")");}}private void clearCart() {shoppingcatNum.setText("购物车(0)");actionBarEdit.setVisibility(View.GONE);llCart.setVisibility(View.GONE);empty_shopcart.setVisibility(View.VISIBLE);//这里发生过错误 }//模拟数据 private void initData() {mcontext = this;groups = new ArrayList<StoreInfo>();childs = new HashMap<String, List<GoodsInfo>>();for (int i = 0; i < 5; i++) {groups.add(new StoreInfo(i + "", "小马的第" + (i + 1) + "号当铺"));List<GoodsInfo> goods = new ArrayList<>();for (int j = 0; j <= i; j++) {int[] img = {R.drawable.cmaz, R.drawable.cmaz, R.drawable.cmaz, R.drawable.cmaz,R.drawable.cmaz, R.drawable.cmaz};//i-j 就是商品的id, 对应着第几个店铺的第几个商品,1-1 就是第一个店铺的第一个商品 goods.add(new GoodsInfo(i + "-" + j, "商品", groups.get(i).getName() + "的第" + (j + 1) + "个商品", 255.00 + new Random().nextInt(1500), 1555 + new Random().nextInt(3000),"第一排", "出头天者", img[j], new Random().nextInt(100)));}childs.put(groups.get(i).getId(), goods);}}//删除操作 private void doDelete() {List<StoreInfo> toBeDeleteGroups = new ArrayList<StoreInfo>(); //待删除的组元素 for (int i = 0; i < groups.size(); i++) {StoreInfo group = groups.get(i);if (group.isChoosed()) {toBeDeleteGroups.add(group);}List<GoodsInfo> toBeDeleteChilds = new ArrayList<GoodsInfo>();//待删除的子元素 List<GoodsInfo> child = childs.get(group.getId());for (int j = 0; j < child.size(); j++) {if (child.get(j).isChoosed()) {toBeDeleteChilds.add(child.get(j));}}child.removeAll(toBeDeleteChilds);}groups.removeAll(toBeDeleteGroups);//重新设置购物车 setCartNum();adapter.notifyDataSetChanged();}private void findView(View view) {shoppingcatNum = (TextView) view.findViewById(R.id.shoppingcat_num);actionBarEdit = (Button) view.findViewById(R.id.actionBar_edit);//不知道为什么,ButterKnife不知道BindView empty_shopcart = (LinearLayout) findViewById(R.id.layout_empty_shopcart);}private void initActionBar() {//隐藏标题栏 if (getSupportActionBar() != null) {//去掉阴影 getSupportActionBar().setElevation(0);getSupportActionBar().setDisplayShowTitleEnabled(false);getSupportActionBar().setDisplayShowCustomEnabled(true);View view = getLayoutInflater().inflate(R.layout.acitonbar, null);findView(view);getSupportActionBar().setCustomView(view, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));ActionBar.LayoutParams lp = (ActionBar.LayoutParams) view.getLayoutParams();lp.gravity = Gravity.HORIZONTAL_GRAVITY_MASK | Gravity.CENTER_HORIZONTAL;getSupportActionBar().setCustomView(view, lp);}}@OnClick({R.id.all_checkBox, R.id.go_pay, R.id.share_goods, R.id.collect_goods, R.id.del_goods})public void onClick(View view) {AlertDialog dialog;switch (view.getId()) {case R.id.all_checkBox:doCheckAll();break;case R.id.go_pay:if (mtotalCount == 0) {UtilTool.toast(mcontext, "请选择要支付的商品");return;}dialog = new AlertDialog.Builder(mcontext).create();dialog.setMessage("总计:" + mtotalCount + "种商品," + mtotalPrice + "元");dialog.setButton(DialogInterface.BUTTON_POSITIVE, "支付", new DialogInterface.OnClickListener() {@Override public void onClick(DialogInterface dialog, int which) {return;}});dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {@Override public void onClick(DialogInterface dialog, int which) {return;}});dialog.show();break;case R.id.share_goods:if (mtotalCount == 0) {UtilTool.toast(mcontext, "请选择要分享的商品");return;}UtilTool.toast(mcontext, "分享成功");break;case R.id.collect_goods:if (mtotalCount == 0) {UtilTool.toast(mcontext, "请选择要收藏的商品");return;}UtilTool.toast(mcontext, "收藏成功");break;case R.id.del_goods:if (mtotalCount == 0) {UtilTool.toast(mcontext, "请选择要删除的商品");return;}dialog = new AlertDialog.Builder(mcontext).create();dialog.setMessage("确认要删除该商品吗?");dialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {@Override public void onClick(DialogInterface dialog, int which) {doDelete();}});dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {@Override public void onClick(DialogInterface dialog, int which) {return;}});dialog.show();break;case R.id.actionBar_edit:flag = !flag;setActionBarEditor();setVisiable();break;}}@Override public void checkGroup(int groupPosition, boolean isChecked) {StoreInfo group = groups.get(groupPosition);List<GoodsInfo> child = childs.get(group.getId());for (int i = 0; i < child.size(); i++) {child.get(i).setChoosed(isChecked);}if (isCheckAll()) {allCheckBox.setChecked(true);//全选 } else {allCheckBox.setChecked(false);//反选 }adapter.notifyDataSetChanged();calulate();}//判断组元素是否全选 private boolean isCheckAll() {for (StoreInfo group : groups) {if (!group.isChoosed()) {return false;}}return true;}@Override public void checkChild(int groupPosition, int childPosition, boolean isChecked) {boolean allChildSameState = true; //判断该组下面的所有子元素是否处于同一状态 StoreInfo group = groups.get(groupPosition);List<GoodsInfo> child = childs.get(group.getId());for (int i = 0; i < child.size(); i++) {//不选全中 if (child.get(i).isChoosed() != isChecked) {allChildSameState = false;break;}}if (allChildSameState) {group.setChoosed(isChecked);//如果子元素状态相同,那么对应的组元素也设置成这一种的同一状态 } else {group.setChoosed(false);//否则一律视为未选中 }if (isCheckAll()) {allCheckBox.setChecked(true);//全选 } else {allCheckBox.setChecked(false);//反选 }adapter.notifyDataSetChanged();calulate();}@Override public void doIncrease(int groupPosition, int childPosition, View showCountView, boolean isChecked) {GoodsInfo good = (GoodsInfo) adapter.getChild(groupPosition, childPosition);int count = good.getCount();count++;good.setCount(count);((TextView) showCountView).setText(String.valueOf(count));adapter.notifyDataSetChanged();calulate();}@Override public void doDecrease(int groupPosition, int childPosition, View showCountView, boolean isChecked) {GoodsInfo good = (GoodsInfo) adapter.getChild(groupPosition, childPosition);int count = good.getCount();if (count == 1) {return;}count--;good.setCount(count);((TextView) showCountView).setText("" + count);adapter.notifyDataSetChanged();calulate();}@Override public void doUpdate(int groupPosition, int childPosition, View showCountView, boolean isChecked) {GoodsInfo good = (GoodsInfo) adapter.getChild(groupPosition, childPosition);int count = good.getCount();UtilsLog.i("进行更新数据,数量" + count + "");((TextView) showCountView).setText(String.valueOf(count));adapter.notifyDataSetChanged();calulate();}@Override public void childDelete(int groupPosition, int childPosition) {StoreInfo group = groups.get(groupPosition);List<GoodsInfo> child = childs.get(group.getId());child.remove(childPosition);if (child.size() == 0) {groups.remove(groupPosition);}adapter.notifyDataSetChanged();calulate();}@Override public void groupEditor(int groupPosition) {}/** * ActionBar标题上点编辑的时候,只显示每一个店铺的商品修改界面 * ActionBar标题上点完成的时候,只显示每一个店铺的商品信息界面 */ private void setActionBarEditor() {for (int i = 0; i < groups.size(); i++) {StoreInfo group = groups.get(i);if (group.isActionBarEditor()) {group.setActionBarEditor(false);} else {group.setActionBarEditor(true);}}adapter.notifyDataSetChanged();}//全选与反选 private void doCheckAll() {for (int i = 0; i < groups.size(); i++) {StoreInfo group = groups.get(i);group.setChoosed(allCheckBox.isChecked());List<GoodsInfo> child = childs.get(group.getId());for (int j = 0; j < child.size(); j++) {child.get(j).setChoosed(allCheckBox.isChecked());//这里出现过错误 }}adapter.notifyDataSetChanged();calulate();}//计算商品总价 private void calulate() {mtotalPrice = 0.00;mtotalCount = 0;for (int i = 0; i < groups.size(); i++) {StoreInfo group = groups.get(i);List<GoodsInfo> child = childs.get(group.getId());for (int j = 0; j < child.size(); j++) {GoodsInfo good = child.get(j);if (good.isChoosed()) {mtotalCount++;mtotalPrice += good.getPrice() * good.getCount();}}}totalPrice.setText("¥" + mtotalPrice + "");goPay.setText("去支付(" + mtotalCount + ")");if (mtotalCount == 0) {setCartNum();} else {shoppingcatNum.setText("购物车(" + mtotalCount + ")");}}private void setVisiable() {if (flag) {orderInfo.setVisibility(View.GONE);shareInfo.setVisibility(View.VISIBLE);actionBarEdit.setText("完成");} else {orderInfo.setVisibility(View.VISIBLE);shareInfo.setVisibility(View.GONE);actionBarEdit.setText("编辑");}}@Override protected void onDestroy() {super.onDestroy();adapter = null;childs.clear();groups.clear();mtotalPrice = 0.00;mtotalCount = 0;} }
adapter文件夹下ShopcatAdapter
import android.content.Context; import android.content.DialogInterface; import android.support.v7.app.AlertDialog; import android.text.Spannable; import android.text.SpannableString; import android.text.Spanned; import android.text.style.StrikethroughSpan; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.CheckBox; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView;import com.bwie.test.taobao_master.R; import com.bwie.test.utils.UtilTool; import com.bwie.test.utils.UtilsLog; import com.bwie.test.entity.GoodsInfo; import com.bwie.test.entity.StoreInfo;import java.util.List; import java.util.Map;import butterknife.BindView; import butterknife.ButterKnife; public class ShopcatAdapter extends BaseExpandableListAdapter {private List<StoreInfo> groups;//这个String对应着StoreInfo的Id,也就是店铺的Id private Map<String, List<GoodsInfo>> childrens;private Context mcontext;private CheckInterface checkInterface;private ModifyCountInterface modifyCountInterface;private GroupEditorListener groupEditorListener;private int count = 0;private boolean flag=true; //组的编辑按钮是否可见,true可见,false不可见 public ShopcatAdapter(List<StoreInfo> groups, Map<String, List<GoodsInfo>> childrens, Context mcontext) {this.groups = groups;this.childrens = childrens;this.mcontext = mcontext;}@Override public int getGroupCount() {return groups.size();}@Override public int getChildrenCount(int groupPosition) {String groupId = groups.get(groupPosition).getId();return childrens.get(groupId).size();}@Override public Object getGroup(int groupPosition) {return groups.get(groupPosition);}@Override public Object getChild(int groupPosition, int childPosition) {List<GoodsInfo> childs = childrens.get(groups.get(groupPosition).getId());return childs.get(childPosition);}@Override public long getGroupId(int groupPosition) {return groupPosition;}@Override public long getChildId(int groupPosition, int childPosition) {return childPosition;}@Override public boolean hasStableIds() {return false;}@Override public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {final GroupViewHolder groupViewHolder;if (convertView == null) {convertView = View.inflate(mcontext, R.layout.item_shopcat_group, null);groupViewHolder = new GroupViewHolder(convertView);convertView.setTag(groupViewHolder);} else {groupViewHolder = (GroupViewHolder) convertView.getTag();}final StoreInfo group = (StoreInfo) getGroup(groupPosition);groupViewHolder.storeName.setText(group.getName());groupViewHolder.storeCheckBox.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {group.setChoosed(((CheckBox) v).isChecked());checkInterface.checkGroup(groupPosition, ((CheckBox) v).isChecked());}});groupViewHolder.storeCheckBox.setChecked(group.isChoosed());/**【文字指的是组的按钮的文字】 * 当我们按下ActionBar的 "编辑"按钮, 应该把所有组的文字显示"编辑",并且设置按钮为不可见 * 当我们完成编辑后,再把组的编辑按钮设置为可见 * 不懂,请自己操作淘宝,观察一遍 */ if(group.isActionBarEditor()){group.setEditor(false);groupViewHolder.storeEdit.setVisibility(View.GONE);flag=false;}else{flag=true;groupViewHolder.storeEdit.setVisibility(View.VISIBLE);}/** * 思路:当我们按下组的"编辑"按钮后,组处于编辑状态,文字显示"完成" * 当我们点击“完成”按钮后,文字显示"编辑“,组处于未编辑状态 */ if (group.isEditor()) {groupViewHolder.storeEdit.setText("完成");} else {groupViewHolder.storeEdit.setText("编辑");}groupViewHolder.storeEdit.setOnClickListener(new GroupViewClick(group, groupPosition,groupViewHolder.storeEdit));return convertView;}@Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild,View convertView, ViewGroup parent) {final ChildViewHolder childViewHolder;if (convertView == null) {convertView = View.inflate(mcontext, R.layout.item_shopcat_product, null);childViewHolder = new ChildViewHolder(convertView);convertView.setTag(childViewHolder);} else {childViewHolder = (ChildViewHolder) convertView.getTag();}/** * 根据组的编辑按钮的可见与不可见,去判断是组对下辖的子元素编辑 * 如果组的编辑按钮可见,那么肯定是组对自己下辖元素的编辑 * 如果组的编辑按钮不可见,那么肯定是ActionBar对组下辖元素的编辑 */ if(flag){if (groups.get(groupPosition).isEditor()) {childViewHolder.editGoodsData.setVisibility(View.VISIBLE);childViewHolder.delGoods.setVisibility(View.VISIBLE);childViewHolder.goodsData.setVisibility(View.GONE);} else {childViewHolder.delGoods.setVisibility(View.VISIBLE);childViewHolder.goodsData.setVisibility(View.VISIBLE);childViewHolder.editGoodsData.setVisibility(View.GONE);}}else{if(groups.get(groupPosition).isActionBarEditor()){childViewHolder.delGoods.setVisibility(View.GONE);childViewHolder.editGoodsData.setVisibility(View.VISIBLE);childViewHolder.goodsData.setVisibility(View.GONE);}else{childViewHolder.delGoods.setVisibility(View.VISIBLE);childViewHolder.goodsData.setVisibility(View.VISIBLE);childViewHolder.editGoodsData.setVisibility(View.GONE);}}final GoodsInfo child = (GoodsInfo) getChild(groupPosition, childPosition);if (child != null) {childViewHolder.goodsName.setText(child.getDesc());childViewHolder.goodsPrice.setText("¥" + child.getPrice() + "");childViewHolder.goodsNum.setText(String.valueOf(child.getCount()));childViewHolder.goodsImage.setImageResource(R.drawable.cmaz);childViewHolder.goods_size.setText("门票:" + child.getColor() + ",类型:" + child.getSize());//设置打折前的原价 SpannableString spannableString = new SpannableString("¥" + child.getPrime_price() + "");StrikethroughSpan span = new StrikethroughSpan();spannableString.setSpan(span,0,spannableString.length()-1+1, Spanned.SPAN_INCLUSIVE_INCLUSIVE); //避免无限次的append if (childViewHolder.goodsPrimePrice.length() > 0) {childViewHolder.goodsPrimePrice.setText("");}childViewHolder.goodsPrimePrice.setText(spannableString);childViewHolder.goodsBuyNum.setText("x" + child.getCount() + "");childViewHolder.singleCheckBox.setChecked(child.isChoosed());childViewHolder.singleCheckBox.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {child.setChoosed(((CheckBox) v).isChecked());childViewHolder.singleCheckBox.setChecked(((CheckBox) v).isChecked());checkInterface.checkChild(groupPosition, childPosition, ((CheckBox) v).isChecked());}});childViewHolder.increaseGoodsNum.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {modifyCountInterface.doIncrease(groupPosition, childPosition,childViewHolder.goodsNum, childViewHolder.singleCheckBox.isChecked());}});childViewHolder.reduceGoodsNum.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {modifyCountInterface.doDecrease(groupPosition, childPosition, childViewHolder.goodsNum, childViewHolder.singleCheckBox.isChecked());}});childViewHolder.goodsNum.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {showDialog(groupPosition,childPosition,childViewHolder.goodsNum,childViewHolder.singleCheckBox.isChecked(),child);}});childViewHolder.delGoods.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {new AlertDialog.Builder(mcontext).setMessage("确定要删除该商品吗").setNegativeButton("取消",null).setPositiveButton("确定", new DialogInterface.OnClickListener() {@Override public void onClick(DialogInterface dialog, int which) {modifyCountInterface.childDelete(groupPosition,childPosition);}}).create().show();;}});}return convertView;}/** * @param groupPosition * @param childPosition * @param showCountView * @param isChecked * @param child */ private void showDialog(final int groupPosition, final int childPosition, final View showCountView,final boolean isChecked, final GoodsInfo child) {final AlertDialog.Builder alertDialog_Builder=new AlertDialog.Builder(mcontext);View view= LayoutInflater.from(mcontext).inflate(R.layout.dialog_change_num,null);final AlertDialog dialog=alertDialog_Builder.create();dialog.setView(view);//errored,这里是dialog,不是alertDialog_Buidler count=child.getCount();final EditText num= (EditText) view.findViewById(R.id.dialog_num);num.setText(count+"");//自动弹出键盘 dialog.setOnShowListener(new DialogInterface.OnShowListener() {@Override public void onShow(DialogInterface dialog) {UtilTool.showKeyboard(mcontext,showCountView);}});final TextView increase= (TextView) view.findViewById(R.id.dialog_increaseNum);final TextView DeIncrease=(TextView)view.findViewById(R.id.dialog_reduceNum);final TextView pButton= (TextView) view.findViewById(R.id.dialog_Pbutton);final TextView nButton= (TextView) view.findViewById(R.id.dialog_Nbutton);nButton.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {dialog.dismiss();}});pButton.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {int number=Integer.parseInt(num.getText().toString().trim());if(number==0){dialog.dismiss();}else{UtilsLog.i("数量="+number+"");num.setText(String.valueOf(number));child.setCount(number);modifyCountInterface.doUpdate(groupPosition,childPosition,showCountView,isChecked);dialog.dismiss();}}});increase.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {count++;num.setText(String.valueOf(count));}});DeIncrease.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {if(count>1){count--;num.setText(String.valueOf(count));}}});dialog.show();}@Override public boolean isChildSelectable(int groupPosition, int childPosition) {return false;}public GroupEditorListener getGroupEditorListener() {return groupEditorListener;}public void setGroupEditorListener(GroupEditorListener groupEditorListener) {this.groupEditorListener = groupEditorListener;}public CheckInterface getCheckInterface() {return checkInterface;}public void setCheckInterface(CheckInterface checkInterface) {this.checkInterface = checkInterface;}public ModifyCountInterface getModifyCountInterface() {return modifyCountInterface;}public void setModifyCountInterface(ModifyCountInterface modifyCountInterface) {this.modifyCountInterface = modifyCountInterface;}static class GroupViewHolder {@BindView(R.id.store_checkBox)CheckBox storeCheckBox;@BindView(R.id.store_name)TextView storeName;@BindView(R.id.store_edit)TextView storeEdit;public GroupViewHolder(View view) {ButterKnife.bind(this, view);}}/** * 店铺的复选框 */ public interface CheckInterface {/** * 组选框状态改变触发的事件 * @param groupPosition 组元素的位置 * @param isChecked 组元素的选中与否 */ void checkGroup(int groupPosition, boolean isChecked);/** * 子选框状态改变触发的事件 * @param groupPosition 组元素的位置 * @param childPosition 子元素的位置 * @param isChecked 子元素的选中与否 */ void checkChild(int groupPosition, int childPosition, boolean isChecked);}/** * 改变数量的接口 */ public interface ModifyCountInterface {/** * 增加操作 * @param groupPosition 组元素的位置 * @param childPosition 子元素的位置 * @param showCountView 用于展示变化后数量的View * @param isChecked 子元素选中与否 */ void doIncrease(int groupPosition, int childPosition, View showCountView, boolean isChecked);void doDecrease(int groupPosition, int childPosition, View showCountView, boolean isChecked);void doUpdate(int groupPosition, int childPosition, View showCountView, boolean isChecked);/** * 删除子Item * @param groupPosition * @param childPosition */ void childDelete(int groupPosition, int childPosition);}/** * 监听编辑状态 */ public interface GroupEditorListener {void groupEditor(int groupPosition);}/** * 使某个小组处于编辑状态 */ private class GroupViewClick implements View.OnClickListener {private StoreInfo group;private int groupPosition;private TextView editor;public GroupViewClick(StoreInfo group, int groupPosition, TextView editor) {this.group = group;this.groupPosition = groupPosition;this.editor = editor;}@Override public void onClick(View v) {if (editor.getId() == v.getId()) {groupEditorListener.groupEditor(groupPosition);if (group.isEditor()) {group.setEditor(false);} else {group.setEditor(true);}notifyDataSetChanged();}}}static class ChildViewHolder {@BindView(R.id.single_checkBox)CheckBox singleCheckBox;@BindView(R.id.goods_image)ImageView goodsImage;@BindView(R.id.goods_name)TextView goodsName;@BindView(R.id.goods_size)TextView goods_size;@BindView(R.id.goods_price)TextView goodsPrice;@BindView(R.id.goods_prime_price)TextView goodsPrimePrice;@BindView(R.id.goods_buyNum)TextView goodsBuyNum;@BindView(R.id.goods_data)RelativeLayout goodsData;@BindView(R.id.reduce_goodsNum)TextView reduceGoodsNum;@BindView(R.id.goods_Num)TextView goodsNum;@BindView(R.id.increase_goods_Num)TextView increaseGoodsNum;@BindView(R.id.goodsSize)TextView goodsSize;@BindView(R.id.del_goods)TextView delGoods;@BindView(R.id.edit_goods_data)LinearLayout editGoodsData;public ChildViewHolder(View view) {ButterKnife.bind(this, view);}} }
utils文件夹下UtilsLog
import android.util.Log; public class UtilsLog {//开关 private static final Boolean DEBUG=true;//TAG,统一TAG,便于信息过滤 private static final String TAG="CAONIMA";public static void d(String text){if(DEBUG){Log.d(TAG,text);}}public static void i(String text){if(DEBUG){Log.i(TAG,text);}}public static void e(String text){if(DEBUG){Log.e(TAG,text);}}public static void w(String text){if(DEBUG){Log.w(TAG,text);}}public static void f(String text){if(DEBUG){Log.wtf(TAG,text);}} }
UtilTool
import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.InputMethodManager; import android.widget.Toast; public class UtilTool {public static void hideKeyboard(Context mcontext, ViewGroup view){view.requestFocus();InputMethodManager im= (InputMethodManager) mcontext.getSystemService(Context.INPUT_METHOD_SERVICE);try{im.hideSoftInputFromWindow(view.getWindowToken(),0);}catch(Exception e){e.printStackTrace();}}public static void showKeyboard(Context mcontext,View view){InputMethodManager im= (InputMethodManager) mcontext.getSystemService(Context.INPUT_METHOD_SERVICE);im.showSoftInput(view,0);}public static void toast(Context mcontext,String text){Toast.makeText(mcontext,text, Toast.LENGTH_SHORT).show();} }
需要导入的依赖
<activity android:name=".MainActivity" android:label="@string/shoppingcat"/>
compile 'com.jakewharton:butterknife:8.5.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1' //强大的下拉刷新控件 compile 'in.srain.cube:ultra-ptr:1.0.11'
bean文件夹下GoodsInfo
public class GoodsInfo {private String id;private String name;private boolean isChoosed;private String imageUrl;private String desc;private double price;private double prime_price;private int postion;private int count;private String color;private String size;private int goodsImg;public GoodsInfo(String id, String name, String desc,double price, double prime_price,String color, String size, int goodsImg,int count) {this.id = id;this.name = name;this.desc = desc;this.price = price;this.prime_price = prime_price;this.count = count;this.color = color;this.size = size;this.goodsImg = goodsImg;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public boolean isChoosed() {return isChoosed;}public void setChoosed(boolean choosed) {isChoosed = choosed;}public String getImageUrl() {return imageUrl;}public void setImageUrl(String imageUrl) {this.imageUrl = imageUrl;}public String getDesc() {return desc;}public void setDesc(String desc) {this.desc = desc;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public double getPrime_price() {return prime_price;}public void setPrime_price(double prime_price) {this.prime_price = prime_price;}public int getPostion() {return postion;}public void setPostion(int postion) {this.postion = postion;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public String getSize() {return size;}public void setSize(String size) {this.size = size;}public int getGoodsImg() {return goodsImg;}public void setGoodsImg(int goodsImg) {this.goodsImg = goodsImg;} }
StoreInfo
public class StoreInfo {private String id;private String name;private boolean isChoosed;private boolean isEditor; //自己对该组的编辑状态 private boolean ActionBarEditor;// 全局对该组的编辑状态 private int flag;public StoreInfo(String id, String name) {this.id = id;this.name = name;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public boolean isChoosed() {return isChoosed;}public void setChoosed(boolean choosed) {isChoosed = choosed;}public boolean isEditor() {return isEditor;}public void setEditor(boolean editor) {isEditor = editor;}public boolean isActionBarEditor() {return ActionBarEditor;}public void setActionBarEditor(boolean actionBarEditor) {ActionBarEditor = actionBarEditor;} }
制定写死数据实现购物车删除相关推荐
- html如何看数据是不是写死的,程序员常讲的「写死」是什么意思?
我们在开发软件的过程中对于一些固定不变的参数都会写死,这个时候很多人也许不明白程序员嘴里说的「写死」是什么意思?可以不写死吗?不写死就会增加难度吗?这里吾爱编程为大家介绍一下关于程序员常讲的写死是什么 ...
- leveldb源码分析:数据插入与删除(Put与Delete)
leveldb数据的插入与获取 leveldb提供的数据的交互接口如下: // Set the database entry for "key" to "value&qu ...
- 给select设置默认值,在option在页面上已经写死的情况下
2019独角兽企业重金招聘Python工程师标准>>> 从后台传来数据 request.setAttribute("time", time), time有year ...
- android 从文件制定位置读取数据
今天,简单讲讲android 如何从制定的位置读取文件. 之前,我写了使用FileInputStream使用read(byte[] b)读取文件的博客,每次调用read()时,FileInputStr ...
- oracle中修改多个字段默认值_利用VBA代码在已有的数据表中删除、添加、修改字段...
大家好,今日继续给大家讲解VBA数据库解决方案的第21讲,如何利用VBA代码在已有的数据表中删除,添加,修改字段.这个内容是操作数据库的一项必修的内容,还望大家在实际工作中多利用,所以这节的知识,对于 ...
- mysql将查到的数据删除_MySQL基本SQL语句之数据插入、删除数据和更新数据 | 旺旺知识库...
一.INSERT插入数据: 方法一:批量插入 基本语法: INSERT INTO tb_name (col1, col2, ...) VALUES (val1, val2, ...)[,(val1, ...
- 删除数据oracle,oracle删除数据
oracle 动态删除,oracle监听之动态和静态注册,oracle删除数据,oracle删除用户 Oracle 删除数据的几种方法_计算机软件及应用_IT/计算机_专业资料.删除... oracl ...
- python查询千万级数据_Python批量删除mysql中千万级大量数据
场景描述 线上mysql数据库里面有张表保存有每天的统计结果,每天有1千多万条,这是我们意想不到的,统计结果咋有这么多.运维找过来,磁盘占了200g,最后问了运营,可以只保留最近3天的,前面的数据,只 ...
- ECharts Y轴固定分割段数,Y轴动态数值非写死
网上一些方法是写死最大值最小值和步长, 我这里最大值不确定需要动态设置,使用以下方法. //计算ehcarts y轴最大值 和 步长 let maxVal = 0; //最大值 let intVal ...
最新文章
- 深入浅出,ARCore开发原理
- python程序写蛇_python蟒蛇绘制程序
- happens-before
- linux msleep 头文件,Linux延迟函数
- 演示:Linux工程环境应用实训(防火墙、NAT、静态路由)详细配步骤
- Python的逻辑运算符and小析
- 2021年五月中旬推荐文章
- 法国电信:5G加速数字化落地 物联网是首座金矿
- 使用了Spring boot devtools, dozer转换嵌套对象失败解决记录
- 让form登录界面居中_徒手撸-登录页面(1)
- PS把一张白纸里的黑色图形抠出来
- python 最小二乘回归 高斯核_[数值计算] 数据拟合——非线性最小二乘法
- ie6下的iframe,问题多多。
- H3C 交换机web页
- Apollo入门使用手册
- 求勾股数元祖(java)
- Unity性能优化要点分析(二) 渲染优化技术
- 华为防火墙故障处理工具之查看路由表
- 企业微信可以统计加班时长吗?如何查看?
- windows下载安装opencv
热门文章
- OFDM系统同步技术的matlab仿真,包括符号定时同步,采样钟同步,频偏估计
- JAVA知识点梳理第四部分——Swing控件
- LocalDateTime时间戳转换
- [使用经验] 搜集整理 i9100 含港行,欧水,国行可精简列表完整 12月8日
- git远程操作报错git branch --set-upstream-to=origin/branch master
- 使用浏览器免费下载MP3,MP4等可在线播放的媒体资源(图文详解)。
- 什么样的投资者适合做股票配资?
- buuctf wustctf2020_getshell_2 ret2shellcode
- Struts2框架简单流程介绍
- Springboot - 用SpringBoot 2.3.0.M1创建Docker映像