比下边的高级的购物车下载地址
http://download.csdn.net/download/wyj1369/10038262
先上图
接下来我就上代码啦
main布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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="@drawable/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="12dp"
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:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2.5"
android:gravity="center_vertical"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/all_chekbox"
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:paddingLeft="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:visibility="visible" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="合计:"
android:textSize="16sp"
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/purple"
android:textSize="16sp"
android:textStyle="bold" />
LinearLayout>
<TextView
android:id="@+id/tv_delete"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/orange"
android:clickable="true"
android:gravity="center"
android:text="删除"
android:textColor="#FAFAFA" />
<TextView
android:id="@+id/tv_go_to_pay"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/crimson"
android:clickable="true"
android:gravity="center"
android:text="结算(0)"
android:textColor="#FAFAFA" />
LinearLayout>
LinearLayout>`
`布局文件 item_shopcart_group.xml
<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_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="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="@drawable/shop_ico"
android:drawablePadding="10dp"
android:drawableRight="@drawable/s_jr_ico"
android:text="第八号当铺"
android:textColor="@color/grey_color2"
android:textSize="@dimen/txt_14" />
RelativeLayout>
LinearLayout>
第二个布局 item_shopcart_product.xml
<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/psb" />
<RelativeLayout
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" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
android:orientation="horizontal" >
<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_type_size"
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:singleLine="true"
android:text="( 4L )"
android:textColor="@color/grey_color3"
android:textSize="@dimen/txt_10"
android:visibility="gone" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_reduce"
android:layout_width="25dp"
android:layout_height="25dp"
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="25dp"
android:layout_height="25dp"
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="25dp"
android:layout_height="25dp"
android:background="@drawable/text_angle_right"
android:gravity="center"
android:text="+"
android:textColor="@color/grey_color1"
android:textSize="@dimen/txt_12" />
LinearLayout>
RelativeLayout>
RelativeLayout>
LinearLayout>
LinearLayout>
配置文件字体
<dimen name="txt_8">8spdimen>
<dimen name="txt_10">10spdimen>
<dimen name="txt_12">12spdimen>
<dimen name="txt_14">14spdimen>
<dimen name="txt_16">16spdimen>
<dimen name="txt_18">18spdimen>
<dimen name="txt_20">20spdimen>
<dimen name="txt_22">22spdimen>
<dimen name="txt_24">24spdimen>
<dimen name="txt_26">26spdimen>
<dimen name="txt_28">28spdimen>
颜色`
<resources>
<color name="list_divider">#999999color>
<color name="list_item_pressed">#D7D7D7color>
<color name="white">#ffffffcolor>
<color name="ivory">#fffff0color>
<color name="lightyellow">#ffffe0color>
<color name="capture_text_cover_bg">#3060a4e3color>
<color name="yellow">#ffff00color>
<color name="snow">#fffafacolor>
<color name="floralwhite">#fffaf0color>
<color name="lemonchiffon">#fffacdcolor>
<color name="cornsilk">#fff8dccolor>
<color name="seaShell">#fff5eecolor>
<color name="lavenderblush">#fff0f5color>
<color name="papayawhip">#ffefd5color>
<color name="blanchedalmond">#ffebcdcolor>
<color name="mistyrose">#ffe4e1color>
<color name="bisque">#ffe4c4color>
<color name="moccasin">#ffe4b5color>
<color name="navajowhite">#ffdeadcolor>
<color name="peachpuff">#ffdab9color>
<color name="gold">#ffd700color>
<color name="pink">#ffc0cbcolor>
<color name="lightpink">#ffb6c1color>
<color name="orange">#fd7903color>
<color name="lightsalmon">#ffa07acolor>
<color name="darkorange">#ff8c00color>
<color name="coral">#ff7f50color>
<color name="hotpink">#ff69b4color>
<color name="tomato">#ff6347color>
<color name="orangered">#ff4500color>
<color name="deeppink">#ff1493color>
<color name="fuchsia">#ff00ffcolor>
<color name="magenta">#ff00ffcolor>
<color name="red">#ff0000color>
<color name="oldlace">#fdf5e6color>
<color name="lightgoldenrodyellow">#fafad2color>
<color name="linen">#faf0e6color>
<color name="antiquewhite">#faebd7color>
<color name="salmon">#fa8072color>
<color name="ghostwhite">#f8f8ffcolor>
<color name="mintcream">#f5fffacolor>
<color name="whitesmoke">#f5f5f5color>
<color name="beige">#f5f5dccolor>
<color name="wheat">#f5deb3color>
<color name="sandybrown">#f4a460color>
<color name="azure">#f0ffffcolor>
<color name="honeydew">#f0fff0color>
<color name="aliceblue">#f0f8ffcolor>
<color name="khaki">#f0e68ccolor>
<color name="lightcoral">#f08080color>
<color name="palegoldenrod">#eee8aacolor>
<color name="violet">#ee82eecolor>
<color name="darksalmon">#e9967acolor>
<color name="lavender">#e6e6facolor>
<color name="lightcyan">#e0ffffcolor>
<color name="burlywood">#deb887color>
<color name="plum">#dda0ddcolor>
<color name="gainsboro">#dcdcdccolor>
<color name="crimson">#dc143ccolor>
<color name="palevioletred">#db7093color>
<color name="goldenrod">#daa520color>
<color name="orchid">#da70d6color>
<color name="thistle">#d8bfd8color>
<color name="lightgray">#d3d3d3color>
<color name="lightgrey">#d3d3d3color>
<color name="tan">#d2b48ccolor>
<color name="chocolate">#d2691ecolor>
<color name="peru">#cd853fcolor>
<color name="indianred">#cd5c5ccolor>
<color name="mediumvioletred">#c71585color>
<color name="silver">#c0c0c0color>
<color name="darkkhaki">#bdb76bcolor>
<color name="rosybrown">#bc8f8fcolor>
<color name="mediumorchid">#ba55d3color>
<color name="darkgoldenrod">#b8860bcolor>
<color name="firebrick">#b22222color>
<color name="powderblue">#b0e0e6color>
<color name="lightsteelblue">#b0c4decolor>
<color name="paleturquoise">#afeeeecolor>
<color name="greenyellow">#adff2fcolor>
<color name="lightblue">#add8e6color>
<color name="darkgray">#a9a9a9color>
<color name="darkgrey">#a9a9a9color>
<color name="brown">#a52a2acolor>
<color name="sienna">#a0522dcolor>
<color name="darkorchid">#9932cccolor>
<color name="palegreen">#98fb98color>
<color name="darkviolet">#9400d3color>
<color name="mediumpurple">#9370dbcolor>
<color name="lightgreen">#90ee90color>
<color name="darkseagreen">#8fbc8fcolor>
<color name="saddlebrown">#8b4513color>
<color name="darkmagenta">#8b008bcolor>
<color name="darkred">#8b0000color>
<color name="blueviolet">#8a2be2color>
<color name="lightskyblue">#87cefacolor>
<color name="skyblue">#87ceebcolor>
<color name="gray">#808080color>
<color name="grey">#7c7b7bcolor>
<color name="olive">#808000color>
<color name="purple">#800080color>
<color name="maroon">#800000color>
<color name="aquamarine">#7fffd4color>
<color name="chartreuse">#7fff00color>
<color name="lawngreen">#7cfc00color>
<color name="mediumslateblue">#7b68eecolor>
<color name="lightslategray">#778899color>
<color name="lightslategrey">#778899color>
<color name="slategray">#708090color>
<color name="slategrey">#708090color>
<color name="olivedrab">#6b8e23color>
<color name="slateblue">#6a5acdcolor>
<color name="dimgray">#696969color>
<color name="dimgrey">#696969color>
<color name="mediumaquamarine">#66cdaacolor>
<color name="cornflowerblue">#6495edcolor>
<color name="cadetblue">#5f9ea0color>
<color name="darkolivegreen">#556b2fcolor>
<color name="indigo">#4b0082color>
<color name="mediumturquoise">#48d1cccolor>
<color name="darkslateblue">#483d8bcolor>
<color name="steelblue">#4682b4color>
<color name="royalblue">#4169e1color>
<color name="turquoise">#40e0d0color>
<color name="mediumseagreen">#3cb371color>
<color name="limegreen">#32cd32color>
<color name="darkslategray">#2f4f4fcolor>
<color name="darkslategrey">#2f4f4fcolor>
<color name="seagreen">#2e8b57color>
<color name="forestgreen">#228b22color>
<color name="lightseagreen">#20b2aacolor>
<color name="dodgerblue">#1e90ffcolor>
<color name="midnightblue">#191970color>
<color name="aqua">#00ffffcolor>
<color name="cyan">#00ffffcolor>
<color name="springgreen">#00ff7fcolor>
<color name="lime">#00ff00color>
<color name="mediumspringgreen">#00fa9acolor>
<color name="darkturquoise">#00ced1color>
<color name="deepskyblue">#00bfffcolor>
<color name="darkcyan">#008b8bcolor>
<color name="teal">#008080color>
<color name="green">#008000color>
<color name="darkgreen">#006400color>
<color name="blue">#005dc1color>
<color name="mediumblue">#0000cdcolor>
<color name="darkblue">#00008bcolor>
<color name="navy">#000080color>
<color name="black">#3B3B3Bcolor>
<color name="text_click">#0174E1color>
<color name="table_background">#cbcbcbcolor>
<color name="background">#eaeaeacolor>
<color name="light_white">#fcfcfccolor>
<color name="tab_main_color">#1e1d1dcolor>
<color name="comm_bg">#eaeaeacolor>
<color name="comm_text_black">#464646color>
<color name="comm_text_left">#3c3c3ccolor>
<color name="comm_text_right">#3c3c3ccolor>
<color name="comm_text_red">#da1609color>
<color name="comm_text_blue">#0f90e3color>
<color name="comm_text_green">#66c058color>
<color name="comm_text_yellow">#DAE532color>
<color name="comm_button_blue">#0074E1color>
<color name="comm_text_tips">#949494color>
<color name="comm_cutline">#c4c4c4color>
<color name="pressed_bg">#f2f2f2color>
<color name="nopressed_bg">#fffcolor>
<color name="title_content_color">#484848color>
<color name="content_color">#7d7d7dcolor>
<color name="new_red">#D21A3Ecolor>
<color name="new_red_press">#B41131color>
<color name="text_black">#515151color>
<color name="text_left">#646464color>
<color name="text_right">#a1a1a1color>
<color name="text_gray">#b5b5b5color>
<color name="cutline_gray">#c4c4c4color>
<color name="text_yellow">#b18500color>
<color name="btn_bg_gray">#f7f7f7color>
<color name="new_green">#00FF99color>
<color name="menu_item_bg">#f6f6f6color>
<color name="text_tips">#929292color>
<color name="text_left_account">#464646color>
<color name="text_bom_account">#464646color>
<color name="text_price_red">#ed3b3bcolor>
<color name="text_sold_greay">#b0b0b0color>
<color name="text_title_black">#232323color>
<color name="text_question">#333333color>
<color name="text_answer">#777777color>
<color name="result_view">#b0000000color>
<color name="viewfinder_mask">#60000000color>
<color name="possible_result_points">#c0ffff00color>
<color name="transparent">#00000000color>
<color name="comm_card_bg">#ffffffcolor>
<color name="grey_50">#fafafacolor>
<color name="grey_200">#eeeeeecolor>
<color name="btn_blue">#0067dbcolor>
<color name="right_menu_unpressed">#CDCEC9color>
<color name="right_menu_pressed">#C3C3C3color>
<color name="menu_item_press">#2e000000color>
<color name="page_backgroup">#f2f2f2color>
<color name="grey_color1">#333333color>
<color name="grey_color2">#666666color>
<color name="grey_color3">#999999color>
<color name="orange_color">#de6838color>
resources>
适配器 ShopcartExpandableListViewAdapter
package com.jock.adapter;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import com.jock.entity.GroupInfo;
import com.jock.entity.ProductInfo;
import com.jock.shopcar.R;
public class ShopcartExpandableListViewAdapter extends BaseExpandableListAdapter
{
private List groups;
private Map> children;
private Context context;
//HashMap groupMap = new HashMap();
//HashMap childrenMap = new HashMap();
private CheckInterface checkInterface;
private ModifyCountInterface modifyCountInterface;
/**
* 构造函数
*
* @param groups
* 组元素列表
* @param children
* 子元素列表
* @param context
*/
public ShopcartExpandableListViewAdapter(List groups, Map> children, Context context)
{
super();
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;
}
@Override
public int getGroupCount()
{
return groups.size();
}
@Override
public int getChildrenCount(int groupPosition)
{
String groupId = groups.get(groupPosition).getId();
return children.get(groupId).size();
}
@Override
public Object getGroup(int groupPosition)
{
return groups.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition)
{
List childs = children.get(groups.get(groupPosition).getId());
return childs.get(childPosition);
}
@Override
public long getGroupId(int groupPosition)
{
return 0;
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
return 0;
}
@Override
public boolean hasStableIds()
{
return false;
}
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
GroupHolder gholder;
if (convertView == null)
{
gholder = new GroupHolder();
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);
//groupMap.put(groupPosition, convertView);
convertView.setTag(gholder);
} else
{
// convertView = groupMap.get(groupPosition);
gholder = (GroupHolder) convertView.getTag();
}
final GroupInfo group = (GroupInfo) getGroup(groupPosition);
if (group != null)
{
gholder.tv_group_name.setText(group.getName());
gholder.cb_check.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
group.setChoosed(((CheckBox) v).isChecked());
checkInterface.checkGroup(groupPosition, ((CheckBox) v).isChecked());// 暴露组选接口
}
});
gholder.cb_check.setChecked(group.isChoosed());
}
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
final ChildHolder cholder;
if (convertView == null)
{
cholder = new ChildHolder();
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);
// childrenMap.put(groupPosition, convertView);
convertView.setTag(cholder);
} else
{
// convertView = childrenMap.get(groupPosition);
cholder = (ChildHolder) convertView.getTag();
}
final ProductInfo product = (ProductInfo) getChild(groupPosition, childPosition);
if (product != null)
{
cholder.tv_product_desc.setText(product.getDesc());
cholder.tv_price.setText("¥" + product.getPrice() + "");
cholder.tv_count.setText(product.getCount() + "");
cholder.cb_check.setChecked(product.isChoosed());
cholder.cb_check.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
product.setChoosed(((CheckBox) v).isChecked());
cholder.cb_check.setChecked(((CheckBox) v).isChecked());
checkInterface.checkChild(groupPosition, childPosition, ((CheckBox) v).isChecked());// 暴露子选接口
}
});
cholder.iv_increase.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
modifyCountInterface.doIncrease(groupPosition, childPosition, cholder.tv_count, cholder.cb_check.isChecked());// 暴露增加接口
}
});
cholder.iv_decrease.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
modifyCountInterface.doDecrease(groupPosition, childPosition, cholder.tv_count, cholder.cb_check.isChecked());// 暴露删减接口
}
});
}
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return false;
}
/**
* 组元素绑定器
*
*
*/
private class GroupHolder
{
CheckBox cb_check;
TextView tv_group_name;
}
/**
* 子元素绑定器
*
*
*/
private class ChildHolder
{
CheckBox cb_check;
TextView tv_product_name;
TextView tv_product_desc;
TextView tv_price;
TextView iv_increase;
TextView tv_count;
TextView iv_decrease;
}
/**
* 复选框接口
*
*
*/
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);
}
}
bean类1
package com.jock.entity;
public class BaseInfo
{
protected String Id;
protected String name;
protected boolean isChoosed;
public BaseInfo()
{
super();
}
public BaseInfo(String id, String name)
{
super();
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;
}
}
bean2
package com.jock.entity;
public class GroupInfo extends BaseInfo
{
public GroupInfo()
{
super();
}
public GroupInfo(String id, String name)
{
super(id, name);
// TODO Auto-generated constructor stub
}
}
bean类3
package com.jock.entity;
public class ProductInfo extends BaseInfo
{
private String imageUrl;
private String desc;
private double price;
private int count;
private int position;// 绝对位置,只在ListView构造的购物车中,在删除时有效
public ProductInfo()
{
super();
}
public ProductInfo(String id, String name, String imageUrl, String desc, double price, int count)
{
super.Id = id;
super.name = name;
this.imageUrl = imageUrl;
this.desc = desc;
this.price = price;
this.count = count;
}
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;
}
}
main中的代码
package com.jock.shopcart;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;
import com.jock.adapter.ShopcartExpandableListViewAdapter;
import com.jock.adapter.ShopcartExpandableListViewAdapter.CheckInterface;
import com.jock.adapter.ShopcartExpandableListViewAdapter.ModifyCountInterface;
import com.jock.entity.GroupInfo;
import com.jock.entity.ProductInfo;
import com.jock.shopcar.R;
public class MainActivity extends Activity implements CheckInterface, ModifyCountInterface, OnClickListener
{
private ExpandableListView exListView;
private CheckBox cb_check_all;
private TextView tv_total_price;
private TextView tv_delete;
private TextView tv_go_to_pay;
private Context context;
private double totalPrice = 0.00;// 购买的商品总价
private int totalCount = 0;// 购买的商品总数量
private ShopcartExpandableListViewAdapter selva;
private List groups = new ArrayList();// 组元素数据列表
private Map> children = new HashMap>();// 子元素数据列表
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initView();
initEvents();
}
private void initView()
{
context = this;
virtualData();
exListView = (ExpandableListView) findViewById(R.id.exListView);
cb_check_all = (CheckBox) findViewById(R.id.all_chekbox);
tv_total_price = (TextView) findViewById(R.id.tv_total_price);
tv_delete = (TextView) findViewById(R.id.tv_delete);
tv_go_to_pay = (TextView) findViewById(R.id.tv_go_to_pay);
}
private void initEvents()
{
selva = new ShopcartExpandableListViewAdapter(groups, children, this);
selva.setCheckInterface(this);// 关键步骤1,设置复选框接口
selva.setModifyCountInterface(this);// 关键步骤2,设置数量增减接口
exListView.setAdapter(selva);
for (int i = 0; i < selva.getGroupCount(); i++)
{
exListView.expandGroup(i);// 关键步骤3,初始化时,将ExpandableListView以展开的方式呈现
}
cb_check_all.setOnClickListener(this);
tv_delete.setOnClickListener(this);
tv_go_to_pay.setOnClickListener(this);
}
/**
* 模拟数据
* 遵循适配器的数据列表填充原则,组元素被放在一个List中,对应的组元素下辖的子元素被放在Map中,
* 其键是组元素的Id(通常是一个唯一指定组元素身份的值)
*/
private void virtualData()
{
for (int i = 0; i < 6; i++)
{
groups.add(new GroupInfo(i + "", "第八号当铺" + (i + 1) + "号店"));
List products = new ArrayList();
for (int j = 0; j <= i; j++)
{
products.add(new ProductInfo(j + "", "商品", "", groups.get(i).getName() + "的第" + (j + 1) + "个商品", 120.00 + i * j, 1));
}
children.put(groups.get(i).getId(), products);// 将组元素的一个唯一值,这里取Id,作为子元素List的Key
}
}
@Override
public void onClick(View v)
{
AlertDialog alert;
switch (v.getId())
{
case R.id.all_chekbox:
doCheckAll();
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()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
return;
}
});
alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
return;
}
});
alert.show();
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()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
return;
}
});
alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
doDelete();
}
});
alert.show();
break;
}
}
/**
* 删除操作
* 1.不要边遍历边删除,容易出现数组越界的情况
* 2.现将要删除的对象放进相应的列表容器中,待遍历完后,以removeAll的方式进行删除
*/
protected void doDelete()
{
List toBeDeleteGroups = new ArrayList();// 待删除的组元素列表
for (int i = 0; i < groups.size(); i++)
{
GroupInfo group = groups.get(i);
if (group.isChoosed())
{
toBeDeleteGroups.add(group);
}
List toBeDeleteProducts = new ArrayList();// 待删除的子元素列表
List 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();
}
@Override
public void doIncrease(int groupPosition, int childPosition, View showCountView, boolean isChecked)
{
ProductInfo product = (ProductInfo) selva.getChild(groupPosition, childPosition);
int currentCount = product.getCount();
currentCount++;
product.setCount(currentCount);
((TextView) showCountView).setText(currentCount + "");
selva.notifyDataSetChanged();
calculate();
}
@Override
public void doDecrease(int groupPosition, int childPosition, View showCountView, boolean isChecked)
{
ProductInfo product = (ProductInfo) selva.getChild(groupPosition, childPosition);
int currentCount = product.getCount();
if (currentCount == 1)
return;
currentCount--;
product.setCount(currentCount);
((TextView) showCountView).setText(currentCount + "");
selva.notifyDataSetChanged();
calculate();
}
@Override
public void checkGroup(int groupPosition, boolean isChecked)
{
GroupInfo group = groups.get(groupPosition);
List childs = children.get(group.getId());
for (int i = 0; i < childs.size(); i++)
{
childs.get(i).setChoosed(isChecked);
}
if (isAllCheck())
cb_check_all.setChecked(true);
else
cb_check_all.setChecked(false);
selva.notifyDataSetChanged();
calculate();
}
@Override
public void checkChild(int groupPosition, int childPosiTion, boolean isChecked)
{
boolean allChildSameState = true;// 判断改组下面的所有子元素是否是同一种状态
GroupInfo group = groups.get(groupPosition);
List 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())
cb_check_all.setChecked(true);
else
cb_check_all.setChecked(false);
selva.notifyDataSetChanged();
calculate();
}
private boolean isAllCheck()
{
for (GroupInfo group : groups)
{
if (!group.isChoosed())
return false;
}
return true;
}
/** 全选与反选 */
private void doCheckAll()
{
for (int i = 0; i < groups.size(); i++)
{
groups.get(i).setChoosed(cb_check_all.isChecked());
GroupInfo group = groups.get(i);
List childs = children.get(group.getId());
for (int j = 0; j < childs.size(); j++)
{
childs.get(j).setChoosed(cb_check_all.isChecked());
}
}
selva.notifyDataSetChanged();
}
/**
* 统计操作
* 1.先清空全局计数器
* 2.遍历所有子元素,只要是被选中状态的,就进行相关的计算操作
* 3.给底部的textView进行数据填充
*/
private void calculate()
{
totalCount = 0;
totalPrice = 0.00;
for (int i = 0; i < groups.size(); i++)
{
GroupInfo group = groups.get(i);
List childs = children.get(group.getId());
for (int j = 0; j < childs.size(); j++)
{
ProductInfo product = childs.get(j);
if (product.isChoosed())
{
totalCount++;
totalPrice += product.getPrice() * product.getCount();
}
}
}
tv_total_price.setText("¥" + totalPrice);
tv_go_to_pay.setText("去支付(" + totalCount + ")");
}
}
就这样啦一些没有的drawable文件没有也不影响运行
把他注释或删除到