您的位置:首页 > 其它

模拟购物车功能

2017-10-26 08:57 387 查看
今天给大家写一下购物车,都是死数据,但一些基本的功能都是有的先看一下布局文件:activity_main:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
android:id="@+id/top_bar"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@drawable/topbar_background"
android:orientation="vertical" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@android:color/transparent"
android:orientation="vertical" >

<ImageView
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_gravity="center_vertical"
android:padding="12dp"
android:src="@mipmap/topbar_up" />

<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:minHeight="48dp"
android:text="购物车"
android:textColor="#1a1a1a"
android:textSize="16sp" />

<TextView
android:id="@+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="40dp"
android:gravity="center"
android:minHeight="48dp"
android:text="编辑"
android:textColor="#1a1a1a"
android:textSize="14sp"
android:visibility="visible" />
</RelativeLayout>
</LinearLayout>

<ExpandableListView
android:id="@+id/exListView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:childIndicator="@null"
android:groupIndicator="@null" >
</ExpandableListView>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:orientation="horizontal" >

<CheckBox
android:id="@+id/all_chekbox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:layout_marginRight="4dp"
android:button="@drawable/check_box_bg"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center"
android:minHeight="64dp"
android:layout_marginLeft="10dp"
android:text="全选"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<LinearLayout
android:id="@+id/ll_info"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginRight="20dp"
android:layout_weight="1"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="合计:"
android:textSize="18sp"
android:textStyle="bold" />

<TextView
android:id="@+id/tv_total_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="¥0.00"
android:textColor="@color/orangered"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="不含运费"
android:gravity="right"
android:textColor="@color/gray"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="@+id/tv_go_to_pay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="@color/orange"
android:clickable="true"
android:gravity="center"
android:text="结算(0)"
android:textColor="#FAFAFA"

/>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_shar"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"
android:orientation="horizontal"
android:visibility="gone"
>
<TextView
android:id="@+id/tv_share"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:text="分享宝贝"
android:textColor="@color/white"
android:background="@color/orange"
android:textSize="16sp"
android:layout_marginRight="5dp"
android:textStyle="bold" />

<TextView
android:id="@+id/tv_save"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="移到收藏夹"
android:background="@color/orange"
android:textColor="@color/white"
android:layout_marginRight="5dp"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_delete"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/crimson"
android:clickable="true"
android:gravity="center"
android:text="删除"
android:textColor="#FAFAFA"
/>
</LinearLayout>

</LinearLayout>

</LinearLayout>
item_shopcart_group.xml:
<?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="wrap_content"
android:background="@color/white"
android:orientation="vertical" >

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white" >

<CheckBox
android:id="@+id/determine_chekbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="4dp"
android:button="@drawable/check_box_bg"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center"
android:minHeight="38dp"
android:minWidth="32dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:visibility="visible" />

<TextView
android:id="@+id/tv_source_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="@id/determine_chekbox"
android:background="@android:color/white"
android:drawableLeft="@mipmap/shop_ico"
android:drawablePadding="10dp"
android:drawableRight="@mipmap/s_jr_ico"
android:text="第八号当铺"
android:textColor="@color/grey_color2"
android:textSize="@dimen/txt_14" />
<Button
android:id="@+id/tv_store_edtor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="@null"
android:text="编辑"/>
</RelativeLayout>

</LinearLayout>
item_shopcart_product.xml:
<?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="wrap_content"
android:orientation="vertical" >

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#CCCCCC" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/page_backgroup"
android:orientation="horizontal" >

<CheckBox
android:id="@+id/check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="4dp"
android:button="@drawable/check_box_bg"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center"
android:minHeight="64dp"
android:minWidth="32dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:visibility="visible" />

<ImageView
android:id="@+id/iv_adapter_list_pic"
android:layout_width="85dp"
android:layout_height="85dp"
android:layout_marginBottom="15dp"
android:layout_marginTop="13dp"
android:scaleType="centerCrop"
android:src="@drawable/goods1" />

<RelativeLayout
android:id="@+id/rl_no_edtor"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="13dp"
>

<TextView
android:id="@+id/tv_intro"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:ellipsize="end"
android:maxLines="2"
android:text="第八号当铺美女一枚"
android:textColor="@color/grey_color1"
android:textSize="@dimen/txt_14" />
<TextView
android:id="@+id/tv_color_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="颜色:黑色;尺码:29"
android:textColor="@color/gray"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="20dp"
android:layout_alignParentStart="true">

<TextView
android:id="@+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:singleLine="true"
android:text="¥ 308.00"
android:textColor="@color/orange_color"
android:textSize="@dimen/txt_14"
android:textStyle="bold" />

<TextView
android:id="@+id/tv_discount_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/tv_price"
android:text=""
android:textColor="@color/gray"
android:textSize="@dimen/txt_10"
/>
<TextView
android:id="@+id/tv_buy_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:layout_alignParentRight="true"
android:text="X 1"
android:textColor="@color/gray"
android:textSize="@dimen/txt_10"
/>
</RelativeLayout>
</RelativeLayout>
<LinearLayout
android:id="@+id/ll_edtor"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="13dp"
android:visibility="gone"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:id="@+id/ll_change_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_marginTop="10dp"
android:orientation="horizontal" >

<TextView
android:id="@+id/tv_reduce"
android:layout_width="35dp"
android:layout_height="35dp"
android:background="@drawable/text_angle_gray"
android:gravity="center"
android:text="一"
android:textColor="@color/grey_color1"
android:textSize="@dimen/txt_12" />

<TextView
android:id="@+id/tv_num"
android:layout_width="35dp"
android:layout_height="35dp"
android:background="@drawable/text_angle"
android:gravity="center"
android:singleLine="true"
android:text="1"
android:textColor="@color/grey_color1"
android:textSize="@dimen/txt_12" />

<TextView
android:id="@+id/tv_add"
android:layout_width="35dp"
android:layout_height="35dp"
android:background="@drawable/text_angle_right"
android:gravity="center"
android:text="+"
android:textColor="@color/grey_color1"
android:textSize="@dimen/txt_12" />
</LinearLayout>
<TextView
android:id="@+id/tv_colorsize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="颜色:黑色;尺码:29"
android:layout_gravity="left"
android:textColor="@color/gray"/>
</LinearLayout>

<TextView
android:id="@+id/tv_goods_delete"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:text="删除"
android:background="@color/orange"
android:gravity="center"
android:layout_gravity="center"
android:textColor="@color/white"/>
</LinearLayout>
</LinearLayout>

</LinearLayout>
这里边用到很多的颜色,在这里吧color文件也展示一下: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>
在values文件下的dimens.xml中加入字体大小:
<!-- 字体大小 -->
<dimen name="txt_8">8sp</dimen>
<dimen name="txt_10">14sp</dimen>
<dimen name="txt_12">16sp</dimen>
<dimen name="txt_14">18sp</dimen>
<dimen name="txt_16">16sp</dimen>
<dimen name="txt_18">18sp</dimen>
<dimen name="txt_20">20sp</dimen>
<dimen name="txt_22">22sp</dimen>
<dimen name="txt_24">24sp</dimen>
<dimen name="txt_26">26sp</dimen>
<dimen name="txt_28">28sp</dimen>
在drawable文件下,建5个文件check_box_bg.xml、list_selector.xml、text_angle.xml、text_angle_gray.xml、text_angle_right.xmlcheck_box_bg.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector  xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/ic_checked" android:state_checked="true"></item>
<item android:drawable="@mipmap/ic_uncheck"></item>
</selector>
list_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@color/menu_item_press" android:state_focused="true"/>
<item android:drawable="@color/menu_item_press" android:state_selected="true"/>
<item android:drawable="@color/menu_item_press" android:state_pressed="true"/>
<item android:drawable="@android:color/transparent"/>

</selector>
text_angle.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFFFF"/>
<corners android:radius="0dp"/>
<stroke
android:color="#CCCCCC"
android:width="0.01dp" />
</shape>
text_angle_gray.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<!-- 连框颜色值 -->
<item>
<shape>
<solid android:color="#CCCCCC" />
</shape>
</item>
<!-- 主体背景颜色值 -->
<item
android:bottom="1dp"
android:top="1dp"
android:left="1dp"
>

<!-- 边框里面背景颜色 白色 -->
<shape>
<solid android:color="@color/page_backgroup" />
</shape>
</item>

</layer-list>
text_angle_right.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<!-- 连框颜色值 -->
<item>
<shape>
<solid android:color="#CCCCCC" />
</shape>
</item>
<!-- 主体背景颜色值 -->
<item
android:bottom="1dp"
android:top="1dp"
android:right="1dp"
>

<!-- 边框里面背景颜色 白色 -->
<shape>
<solid android:color="@color/page_backgroup" />
</shape>
</item>

</layer-list>
写完后,在res目录下建一个menu文件:里边建一个menu_main.xml:
<menu 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" tools:context=".MainActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
</menu>
GoodsInfo.class:商品信息:
package entity;

/**
* 商品信息
*/
public class GoodsInfo {
protected String Id;
protected String name;
protected boolean isChoosed;
private String imageUrl;
private String desc;
private double price;
private int count;
private int position;// 绝对位置,只在ListView构造的购物车中,在删除时有效
private String color;
private String size;
private int goodsImg;
private double discountPrice;

public double getDiscountPrice() {
return discountPrice;
}

public void setDiscountPrice(double discountPrice) {
this.discountPrice = discountPrice;
}

public int getGoodsImg() {
return goodsImg;
}

public void setGoodsImg(int goodsImg) {
this.goodsImg = goodsImg;
}

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 GoodsInfo(String id, String name, String desc, double price, int count, String color,
String size, int goodsImg,double discountPrice) {
Id = id;
this.name = name;
this.desc = desc;
this.price = price;
this.count = count;
this.color=color;
this.size=size;
this.goodsImg=goodsImg;
this.discountPrice=discountPrice;
}

public String getId() {
return Id;
}

public void setId(String id) {
Id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public boolean isChoosed() {
return isChoosed;
}

public void setChoosed(boolean isChoosed) {
this.isChoosed = isChoosed;
}

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 int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

public int getPosition() {
return position;
}

public void setPosition(int position) {
this.position = position;
}

}
StoreInfo.class:店铺信息:
package entity;

/**
* 店铺信息
*/
public class StoreInfo
{
protected String Id;
protected String name;
protected boolean isChoosed;
private boolean isEdtor;

public boolean isEdtor() {
return isEdtor;
}

public void setIsEdtor(boolean isEdtor) {
this.isEdtor = isEdtor;
}

public StoreInfo(String id, String name) {
Id = id;
this.name = name;
}

public String getId() {
return Id;
}

public void setId(String id) {
Id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public boolean isChoosed() {
return isChoosed;
}

public void setChoosed(boolean isChoosed) {
this.isChoosed = isChoosed;
}
}
购物车数据适配器:ShopcartAdapter.class:
package com.bwie.test.mygwc;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.style.StrikethroughSpan;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.BaseExpandableListAdapter;import android.widget.Button;import android.widget.CheckBox;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.RelativeLayout;import android.widget.TextView;import java.util.List;import java.util.Map;import entity.GoodsInfo;import entity.StoreInfo;/*** 购物车数据适配器*/public class ShopcartAdapter extends BaseExpandableListAdapter {private List<StoreInfo> groups;private Map<String, List<GoodsInfo>> children;private Context context;private CheckInterface checkInterface;private ModifyCountInterface modifyCountInterface;private int flag = 0;private GroupEdtorListener mListener;public GroupEdtorListener getmListener() {return mListener;}public void setmListener(GroupEdtorListener mListener) {this.mListener = mListener;}/*** 构造函数** @param groups   组元素列表* @param children 子元素列表* @param context*/public ShopcartAdapter(List<StoreInfo> groups, Map<String, List<GoodsInfo>> children, Context context) {this.groups = groups;this.children = children;this.context = context;}public void setCheckInterface(CheckInterface checkInterface) {this.checkInterface = checkInterface;}public void setModifyCountInterface(ModifyCountInterface modifyCountInterface) {this.modifyCountInterface = modifyCountInterface;}@Overridepublic int getGroupCount() {return groups.size();}@Overridepublic int getChildrenCount(int groupPosition) {String groupId = groups.get(groupPosition).getId();return children.get(groupId).size();}@Overridepublic Object getGroup(int groupPosition) {return groups.get(groupPosition);}@Overridepublic Object getChild(int groupPosition, int childPosition) {List<GoodsInfo> childs = children.get(groups.get(groupPosition).getId());return childs.get(childPosition);}@Overridepublic long getGroupId(int groupPosition) {return groupPosition;}@Overridepublic long getChildId(int groupPosition, int childPosition) {return childPosition;}@Overridepublic boolean hasStableIds() {return false;}@Overridepublic View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {final GroupViewHolder gholder;if (convertView == null) {gholder = new GroupViewHolder();convertView = View.inflate(context, R.layout.item_shopcart_group, null);gholder.cb_check = (CheckBox) convertView.findViewById(R.id.determine_chekbox);gholder.tv_group_name = (TextView) convertView.findViewById(R.id.tv_source_name);gholder.store_edtor = (Button) convertView.findViewById(R.id.tv_store_edtor);convertView.setTag(gholder);} else {gholder = (GroupViewHolder) convertView.getTag();}final StoreInfo group = (StoreInfo) getGroup(groupPosition);if (group != null) {gholder.tv_group_name.setText(group.getName());gholder.cb_check.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v){group.setChoosed(((CheckBox) v).isChecked());checkInterface.checkGroup(groupPosition, ((CheckBox) v).isChecked());// 暴露组选接口}});gholder.cb_check.setChecked(group.isChoosed());gholder.store_edtor.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {mListener.groupEdit(groupPosition);if (flag == 0) {group.setIsEdtor(true);gholder.store_edtor.setText("完成");} else if (flag == 1) {group.setIsEdtor(false);gholder.store_edtor.setText("编辑");}flag = (flag + 1) % 2;//其余得到循环执行上面2个不同的功能}});} else {groups.remove(groupPosition);}return convertView;}@Overridepublic View getChildView(final int groupPosition, final int childPosition, final boolean isLastChild, View convertView, final ViewGroup parent) {final ChildViewHolder cholder;if (convertView == null) {cholder = new ChildViewHolder();convertView = View.inflate(context, R.layout.item_shopcart_product, null);cholder.cb_check = (CheckBox) convertView.findViewById(R.id.check_box);cholder.tv_product_desc = (TextView) convertView.findViewById(R.id.tv_intro);cholder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);cholder.iv_increase = (TextView) convertView.findViewById(R.id.tv_add);cholder.iv_decrease = (TextView) convertView.findViewById(R.id.tv_reduce);cholder.tv_count = (TextView) convertView.findViewById(R.id.tv_num);cholder.rl_no_edtor = (RelativeLayout) convertView.findViewById(R.id.rl_no_edtor);cholder.tv_color_size = (TextView) convertView.findViewById(R.id.tv_color_size);cholder.tv_discount_price = (TextView) convertView.findViewById(R.id.tv_discount_price);cholder.tv_buy_num = (TextView) convertView.findViewById(R.id.tv_buy_num);cholder.ll_edtor = (LinearLayout) convertView.findViewById(R.id.ll_edtor);cholder.tv_colorsize = (TextView) convertView.findViewById(R.id.tv_colorsize);cholder.tv_goods_delete = (TextView) convertView.findViewById(R.id.tv_goods_delete);cholder.iv_adapter_list_pic= (ImageView) convertView.findViewById(R.id.iv_adapter_list_pic);convertView.setTag(cholder);} else {cholder = (ChildViewHolder) convertView.getTag();}if (groups.get(groupPosition).isEdtor() == true) {cholder.ll_edtor.setVisibility(View.VISIBLE);cholder.rl_no_edtor.setVisibility(View.GONE);} else {cholder.ll_edtor.setVisibility(View.GONE);cholder.rl_no_edtor.setVisibility(View.VISIBLE);}final GoodsInfo goodsInfo = (GoodsInfo) getChild(groupPosition, childPosition);if (goodsInfo != null) {cholder.tv_product_desc.setText(goodsInfo.getDesc());cholder.tv_price.setText("¥" + goodsInfo.getPrice() + "");cholder.tv_count.setText(goodsInfo.getCount() + "");cholder.iv_adapter_list_pic.setImageResource(goodsInfo.getGoodsImg());cholder.tv_color_size.setText("颜色:" + goodsInfo.getColor() + "," + "尺码:" + goodsInfo.getSize() + "瓶/斤");SpannableString spanString = new SpannableString("¥"+String.valueOf(goodsInfo.getDiscountPrice()));StrikethroughSpan span = new StrikethroughSpan();spanString.setSpan(span, 0, String.valueOf(goodsInfo.getDiscountPrice()).length()+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);if(cholder.tv_discount_price.getText().toString().length()>0){cholder.tv_discount_price.setText("");}cholder.tv_discount_price.append(spanString);cholder.tv_buy_num.setText("x" + goodsInfo.getCount());cholder.cb_check.setChecked(goodsInfo.isChoosed());cholder.cb_check.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {goodsInfo.setChoosed(((CheckBox) v).isChecked());cholder.cb_check.setChecked(((CheckBox) v).isChecked());checkInterface.checkChild(groupPosition, childPosition, ((CheckBox) v).isChecked());// 暴露子选接口}});cholder.iv_increase.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {modifyCountInterface.doIncrease(groupPosition, childPosition, cholder.tv_count, cholder.cb_check.isChecked());// 暴露增加接口}});cholder.iv_decrease.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {modifyCountInterface.doDecrease(groupPosition, childPosition, cholder.tv_count, cholder.cb_check.isChecked());// 暴露删减接口}});//删除 购物车cholder.tv_goods_delete.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {AlertDialog alert = new AlertDialog.Builder(context).create();alert.setTitle("操作提示");alert.setMessage("您确定要将这些商品从购物车中移除吗?");alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {return;}});alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {modifyCountInterface.childDelete(groupPosition, childPosition);}});alert.show();}});}return convertView;}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {return false;}/*** 组元素绑定器*/private class GroupViewHolder {CheckBox cb_check;TextView tv_group_name;Button store_edtor;}/*** 子元素绑定器*/private class ChildViewHolder {CheckBox cb_check;ImageView iv_adapter_list_pic;TextView tv_product_name;TextView tv_product_desc;TextView tv_price;TextView iv_increase;TextView tv_count;TextView iv_decrease;RelativeLayout rl_no_edtor;TextView tv_color_size;TextView tv_discount_price;TextView tv_buy_num;LinearLayout ll_edtor;TextView tv_colorsize;TextView tv_goods_delete;}/*** 复选框接口*/public interface CheckInterface {/*** 组选框状态改变触发的事件** @param groupPosition 组元素位置* @param isChecked     组元素选中与否*/public void checkGroup(int groupPosition, boolean isChecked);/*** 子选框状态改变时触发的事件** @param groupPosition 组元素位置* @param childPosition 子元素位置* @param isChecked     子元素选中与否*/public void checkChild(int groupPosition, int childPosition, boolean isChecked);}/*** 改变数量的接口*/public interface ModifyCountInterface {/*** 增加操作** @param groupPosition 组元素位置* @param childPosition 子元素位置* @param showCountView 用于展示变化后数量的View* @param isChecked     子元素选中与否*/public void doIncrease(int groupPosition, int childPosition, View showCountView, boolean isChecked);/*** 删减操作** @param groupPosition 组元素位置* @param childPosition 子元素位置* @param showCountView 用于展示变化后数量的View* @param isChecked     子元素选中与否*/public void doDecrease(int groupPosition, int childPosition, View showCountView, boolean isChecked);/*** 删除子item* @param groupPosition* @param childPosition*/public void childDelete(int groupPosition, int childPosition);}/*** 监听编辑状态*/public interface GroupEdtorListener{public void groupEdit(int groupPosition);}}
MainActivity:
package com.bwie.test.mygwc;import android.content.Context;import android.content.DialogInterface;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.support.v7.app.AlertDialog;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.CheckBox;import android.widget.ExpandableListView;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;import android.widget.Toast;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 entity.GoodsInfo;import entity.StoreInfo;public class MainActivity extends AppCompatActivity implements ShopcartAdapter.CheckInterface,ShopcartAdapter.ModifyCountInterface, ShopcartAdapter.GroupEdtorListener {@BindView(R.id.back)ImageView back;@BindView(R.id.title)TextView title;@BindView(R.id.subtitle)TextView subtitle;@BindView(R.id.top_bar)LinearLayout topBar;@BindView(R.id.exListView)ExpandableListView exListView;@BindView(R.id.all_chekbox)CheckBox allChekbox;@BindView(R.id.tv_total_price)TextView tvTotalPrice;@BindView(R.id.tv_go_to_pay)TextView tvGoToPay;@BindView(R.id.ll_info)LinearLayout llInfo;@BindView(R.id.tv_share)TextView tvShare;@BindView(R.id.tv_save)TextView tvSave;@BindView(R.id.tv_delete)TextView tvDelete;@BindView(R.id.ll_shar)LinearLayout llShar;private Context context;private double totalPrice = 0.00;// 购买的商品总价private int totalCount = 0;// 购买的商品总数量private ShopcartAdapter selva;private List<StoreInfo> groups = new ArrayList<StoreInfo>();// 组元素数据列表private Map<String, List<GoodsInfo>> children = new HashMap<String, List<GoodsInfo>>();// 子元素数据列表private int flag = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ButterKnife.bind(this);context = this;initDatas();initEvents();}private void initEvents() {selva = new ShopcartAdapter(groups, children, this);selva.setCheckInterface(this);// 关键步骤1,设置复选框接口selva.setModifyCountInterface(this);// 关键步骤2,设置数量增减接口selva.setmListener(this);exListView.setAdapter(selva);for (int i = 0; i < selva.getGroupCount(); i++) {exListView.expandGroup(i);// 关键步骤3,初始化时,将ExpandableListView以展开的方式呈现}}@Overrideprotected void onResume() {super.onResume();setCartNum();}/*** 设置购物车产品数量*/private void setCartNum() {int count = 0;for (int i = 0; i < groups.size(); i++) {groups.get(i).setChoosed(allChekbox.isChecked());StoreInfo group = groups.get(i);List<GoodsInfo> childs = children.get(group.getId());for (GoodsInfo goodsInfo : childs) {count += 1;}}title.setText("购物车" + "(" + count + ")");}/*** 模拟数据<br>* 遵循适配器的数据列表填充原则,组元素被放在一个List中,对应的组元素下辖的子元素被放在Map中,<br>* 其键是组元素的Id(通常是一个唯一指定组元素身份的值)*/private void initDatas() {for (int i = 0; i < 3; i++) {groups.add(new StoreInfo(i + "", "天猫店铺" + (i + 1) + "号店"));List<GoodsInfo> products = new ArrayList<GoodsInfo>();for (int j = 0; j <= i; j++) {int[] img = {R.drawable.goods1, R.drawable.goods2, R.drawable.goods3, R.drawable.goods4, R.drawable.goods5, R.drawable.goods6};products.add(new GoodsInfo(j + "", "商品", groups.get(i).getName() + "的第" + (j + 1) + "个商品", 12.00 + new Random().nextInt(23), new Random().nextInt(5) + 1, "豪华", "1", img[i * j], 6.00 + new Random().nextInt(13)));}children.put(groups.get(i).getId(), products);// 将组元素的一个唯一值,这里取Id,作为子元素List的Key}}/*** 删除操作<br>* 1.不要边遍历边删除,容易出现数组越界的情况<br>* 2.现将要删除的对象放进相应的列表容器中,待遍历完后,以removeAll的方式进行删除*/protected 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> toBeDeleteProducts = new ArrayList<GoodsInfo>();// 待删除的子元素列表List<GoodsInfo> childs = children.get(group.getId());for (int j = 0; j < childs.size(); j++) {if (childs.get(j).isChoosed()) {toBeDeleteProducts.add(childs.get(j));}}childs.removeAll(toBeDeleteProducts);}groups.removeAll(toBeDeleteGroups);selva.notifyDataSetChanged();calculate();}@Overridepublic void doIncrease(int groupPosition, int childPosition,View showCountView, boolean isChecked) {GoodsInfo product = (GoodsInfo) selva.getChild(groupPosition,childPosition);int currentCount = product.getCount();currentCount++;product.setCount(currentCount);((TextView) showCountView).setText(currentCount + "");selva.notifyDataSetChanged();calculate();}@Overridepublic void doDecrease(int groupPosition, int childPosition,View showCountView, boolean isChecked) {GoodsInfo product = (GoodsInfo) selva.getChild(groupPosition,childPosition);int currentCount = product.getCount();if (currentCount == 1)return;currentCount--;product.setCount(currentCount);((TextView) showCountView).setText(currentCount + "");selva.notifyDataSetChanged();calculate();}@Overridepublic void childDelete(int groupPosition, int childPosition) {children.get(groups.get(groupPosition).getId()).remove(childPosition);StoreInfo group = groups.get(groupPosition);List<GoodsInfo> childs = children.get(group.getId());if (childs.size() == 0) {groups.remove(groupPosition);}selva.notifyDataSetChanged();handler.sendEmptyMessage(0);}@Overridepublic void checkGroup(int groupPosition, boolean isChecked) {StoreInfo group = groups.get(groupPosition);List<GoodsInfo> childs = children.get(group.getId());for (int i = 0; i < childs.size(); i++) {childs.get(i).setChoosed(isChecked);}if (isAllCheck())allChekbox.setChecked(true);elseallChekbox.setChecked(false);selva.notifyDataSetChanged();calculate();}@Overridepublic void checkChild(int groupPosition, int childPosiTion,boolean isChecked) {boolean allChildSameState = true;// 判断改组下面的所有子元素是否是同一种状态StoreInfo group = groups.get(groupPosition);List<GoodsInfo> childs = children.get(group.getId());for (int i = 0; i < childs.size(); i++) {// 不全选中if (childs.get(i).isChoosed() != isChecked) {allChildSameState = false;break;}}//获取店铺选中商品的总金额if (allChildSameState) {group.setChoosed(isChecked);// 如果所有子元素状态相同,那么对应的组元素被设为这种统一状态} else {group.setChoosed(false);// 否则,组元素一律设置为未选中状态}if (isAllCheck()) {allChekbox.setChecked(true);// 全选} else {allChekbox.setChecked(false);// 反选}selva.notifyDataSetChanged();calculate();}private boolean isAllCheck() {for (StoreInfo group : groups) {if (!group.isChoosed())return false;}return true;}/*** 全选与反选*/private void doCheckAll() {for (int i = 0; i < groups.size(); i++) {groups.get(i).setChoosed(allChekbox.isChecked());StoreInfo group = groups.get(i);List<GoodsInfo> childs = children.get(group.getId());for (int j = 0; j < childs.size(); j++) {childs.get(j).setChoosed(allChekbox.isChecked());}}selva.notifyDataSetChanged();calculate();}/*** 统计操作<br>* 1.先清空全局计数器<br>* 2.遍历所有子元素,只要是被选中状态的,就进行相关的计算操作<br>* 3.给底部的textView进行数据填充*/private void calculate() {totalCount = 0;totalPrice = 0.00;for (int i = 0; i < groups.size(); i++) {StoreInfo group = groups.get(i);List<GoodsInfo> childs = children.get(group.getId());for (int j = 0; j < childs.size(); j++) {GoodsInfo product = childs.get(j);if (product.isChoosed()) {totalCount++;totalPrice += product.getPrice() * product.getCount();}}}tvTotalPrice.setText("¥" + totalPrice);tvGoToPay.setText("去支付(" + totalCount + ")");}@OnClick({R.id.all_chekbox, R.id.tv_delete, R.id.tv_go_to_pay, R.id.subtitle, R.id.tv_save, R.id.tv_share})public void onClick(View view) {AlertDialog alert;switch (view.getId()) {case R.id.all_chekbox:doCheckAll();break;case R.id.tv_delete:if (totalCount == 0) {Toast.makeText(context, "请选择要移除的商品", Toast.LENGTH_LONG).show();return;}alert = new AlertDialog.Builder(context).create();alert.setTitle("操作提示");alert.setMessage("您确定要将这些商品从购物车中移除吗?");alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {return;}});alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {doDelete();}});alert.show();break;case R.id.tv_go_to_pay:if (totalCount == 0) {Toast.makeText(context, "请选择要支付的商品", Toast.LENGTH_LONG).show();return;}alert = new AlertDialog.Builder(context).create();alert.setTitle("操作提示");alert.setMessage("总计:\n" + totalCount + "种商品\n" + totalPrice + "元");alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {return;}});alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定",new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {return;}});alert.show();break;case R.id.subtitle:if (flag == 0) {llInfo.setVisibility(View.GONE);tvGoToPay.setVisibility(View.GONE);llShar.setVisibility(View.VISIBLE);subtitle.setText("完成");} else if (flag == 1) {llInfo.setVisibility(View.VISIBLE);tvGoToPay.setVisibility(View.VISIBLE);llShar.setVisibility(View.GONE);subtitle.setText("编辑");}flag = (flag + 1) % 2;//其余得到循环执行上面2个不同的功能break;case R.id.tv_share:if (totalCount == 0) {Toast.makeText(context, "请选择要分享的商品", Toast.LENGTH_LONG).show();return;}Toast.makeText(MainActivity.this, "分享成功", Toast.LENGTH_SHORT).show();break;case R.id.tv_save:if (totalCount == 0) {Toast.makeText(context, "请选择要保存的商品", Toast.LENGTH_LONG).show();return;}Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();break;}}@Overridepublic void groupEdit(int groupPosition) {groups.get(groupPosition).setIsEdtor(true);selva.notifyDataSetChanged();}Handler handler = new Handler() {@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);//删除购物车后动态改变数量setCartNum();}};}
这其中有很多的图片素材,自己可以找自己的图片替换一下。这就是最终的效果图、
这个工程中,用到了ButterKnife工具,不会配置的人,请点击:http://blog.csdn.net/pentablet/article/details/78351364
这其中有图片的下载,github下载地址:https://github.com/BiPian/MyGwc 

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  模拟购物车