在做购物车的时候:
快捷键的依赖:
ButterKnife(使用快捷键)
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
apply plugin: 'android-apt'
compile 'com.jakewharton:butterknife:8.2.1'
apt 'com.jakewharton:butterknife-compiler:8.2.1'
在主文件中:
<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>
效果图为:
注意在xml中图片的路径:
在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>
效果图为:
在主文件中:
public class Gouwuche extends Fragment 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; Unbinder unbinder; 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; @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v = View.inflate(getContext(), R.layout.gouwuche, null); unbinder = ButterKnife.bind(this, v); initDatas(); initEvents(); context=getActivity(); return v; } private void initEvents() { selva = new ShopcartAdapter(groups, children, getActivity()); 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以展开的方式呈现 } } @Override public 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的方式进行删除 */ public 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(); } @Override public 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(); } @Override public 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(); } @Override public 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); } @Override public 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); else allChekbox.setChecked(false); selva.notifyDataSetChanged(); calculate(); } @Override public 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() { @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; 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.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(getContext(), "分享成功", Toast.LENGTH_SHORT).show(); break; case R.id.tv_save: if (totalCount == 0) { Toast.makeText(context, "请选择要保存的商品", Toast.LENGTH_LONG).show(); return; } Toast.makeText(getContext(), "保存成功", Toast.LENGTH_SHORT).show(); break; } } @Override public void groupEdit(int groupPosition) { groups.get(groupPosition).setIsEdtor(true); selva.notifyDataSetChanged(); } Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); //删除购物车后动态改变数量 setCartNum(); } }; }