最近的项目,用到了商城系统,现在把购物车模块抽取一下,搞个Demo
先看效果:
主界面:
/** * 购物车界面 * * @author shaoshuai * */ public class ShoppingCartPage extends BaseFragment implements OnClickListener { @ViewInject(R.id.lv_shopping_cart) private ListView lv_shopping_cart;// 购物车 // 底部条 @ViewInject(R.id.cb_select_all) private CheckBox cb_select_all;// 全选 @ViewInject(R.id.tv_total) private TextView tv_total;// 总计 @ViewInject(R.id.tv_checkout) private TextView tv_checkout;// 结账 public static final String FRAGMENT_NAME = "购物车"; private SC_ShopAdapter adapter; private List<JsShoppingCartItem> sCartData; private List<JsGoodsItem> selGoodsList = new ArrayList<JsGoodsItem>();// 已经选择的商品集合 private float actualPayment;// 实际付款 @Override public View initView(LayoutInflater inflater) { view = inflater.inflate(R.layout.fragment_shopping_cart, null); ViewUtils.inject(this, view); return view; } @Override public void initData(Bundle savedInstanceState) { MainActivity orderMange = (MainActivity) getActivity(); orderMange.tv_title.setText(FRAGMENT_NAME); getSCData(); adapter = new SC_ShopAdapter(this, sCartData); lv_shopping_cart.setAdapter(adapter); Util.setListViewHeight(lv_shopping_cart); } private void getSCData() { sCartData = new ArrayList<JsShoppingCartItem>(); for (int i = 0; i < 5; i++) { JsShoppingCartItem scItem = new JsShoppingCartItem(); scItem.id = "id"; scItem.shopLogo = "商铺图片";// 商铺Logo scItem.shopName = "商铺名称" + i;// 商铺名称 scItem.goodsData = new ArrayList<JsGoodsItem>();// 商品数据 for (int j = 0; j < i + 1; j++) { scItem.goodsData.add(new JsGoodsItem("商品id", "VIRTUE 摩托电动车双镜片揭面盔全盔半盔冬盔四季安全男女防雾头盔", "颜色分类:黑色;尺码:43", "商品图片", 165.00f, 1)); } sCartData.add(scItem); } } @Override public void initListener() { cb_select_all.setOnClickListener(this);// 全选 tv_checkout.setOnClickListener(this);// 结算 } @Override public void onClick(View v) { switch (v.getId()) { case R.id.cb_select_all:// 全选 boolean flag = cb_select_all.isChecked(); for (int i = 0; i < adapter.getSelect().size(); i++) { adapter.getSelect().set(i, flag); for (int j = 0; j < adapter.getPAdapter(i).getSelect().size(); j++) { adapter.getPAdapter(i).getSelect().set(j, flag); } } updateAmount(); adapter.notifyDataSetChanged(); break; case R.id.tv_checkout:// 结账 if (selGoodsList.size() == 0) { Toast.makeText(mContext, "请选择要结算的商品!", Toast.LENGTH_SHORT).show(); } else { // 创建订单 JsOrderItem order = new JsOrderItem("10101001", "待付款", actualPayment, "订单详情", selGoodsList); // toOrderMGPage(order); Toast.makeText(mContext, "前往结算界面", Toast.LENGTH_SHORT).show(); } break; default: break; } } public void checkAll(boolean checked) { cb_select_all.setChecked(checked); } public void updateAmount() { actualPayment = 0.0f;// 金额 selGoodsList.clear(); for (int i = 0; i < sCartData.size(); i++) { // 对于每个商铺 List<JsGoodsItem> goodsData = sCartData.get(i).goodsData;// 商品集合 for (int j = 0; j < goodsData.size(); j++) { if (adapter.getPAdapter(i).getSelect().get(j)) {// 第j个商品是否选中 selGoodsList.add(goodsData.get(j));// 添加到已选择集合 // 总价 += 单个商品价格 * 购买数量; float f = goodsData.get(j).goodsPrice * goodsData.get(j).goodsNum; actualPayment += f; } } } tv_total.setText(actualPayment + "");// 更新金额 tv_checkout.setText("结算(" + selGoodsList.size() + ")"); } }
/** * 购物车-商铺-填充器 * * @author shaoshuai * */ public class SC_ShopAdapter extends AbsAdapter<JsShoppingCartItem> { private LinkedList<Boolean> selected = new LinkedList<Boolean>(); List<SC_GoodsAdapter> pAdapterList = new ArrayList<SC_GoodsAdapter>(); ShoppingCartPage sCartPage; public SC_ShopAdapter(ShoppingCartPage scPage, List<JsShoppingCartItem> datas) { super(scPage.getActivity(), datas); this.sCartPage = scPage; selected.clear(); for (int i = 0; i < mDatas.size(); i++) { selected.add(false); SC_GoodsAdapter pAdapter = new SC_GoodsAdapter(sCartPage, mDatas.get(i).goodsData, this, i); pAdapterList.add(pAdapter); } } public List<Boolean> getSelect() { return selected; } public SC_GoodsAdapter getPAdapter(int position) { return pAdapterList.get(position); } public List<SC_GoodsAdapter> getPAdapterList() { return pAdapterList; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate(R.layout.item_cart_goods, parent, false); holder = new ViewHolder(); holder.cb_shop_all = (CheckBox) convertView.findViewById(R.id.cb_shop_all); holder.iv_shop_icon = (ImageView) convertView.findViewById(R.id.iv_shop_icon); holder.tv_shop_name = (TextView) convertView.findViewById(R.id.tv_shop_name); holder.lv_goods = (NoScrollListView) convertView.findViewById(R.id.lv_goods); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } final JsShoppingCartItem item = (JsShoppingCartItem) getItem(position); holder.iv_shop_icon.setImageResource(R.drawable.shangpu);// 商铺 holder.tv_shop_name.setText(item.shopName);// 名称 holder.lv_goods.setAdapter(pAdapterList.get(position));// 填充商品 holder.cb_shop_all.setChecked(selected.get(position));// 是否全选 holder.cb_shop_all.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { selected.set(position, !selected.get(position));// 选中取反 // 更新孩子 SC_GoodsAdapter goodsAdp = pAdapterList.get(position);// 商品适配器 for (int i = 0; i < goodsAdp.getSelect().size(); i++) { // 设置每一个孩子 跟 父亲相同 goodsAdp.getSelect().set(i, selected.get(position)); } // 更新 购物车 全选按钮 if (selected.contains(false)) { sCartPage.checkAll(false); } else { sCartPage.checkAll(true); } sCartPage.updateAmount(); notifyDataSetChanged(); } }); return convertView; } /** * 创建一个本,减少findViewById的次数 */ private class ViewHolder { CheckBox cb_shop_all;// 全选 ImageView iv_shop_icon;// 商铺图 TextView tv_shop_name;// 商铺名 NoScrollListView lv_goods;// 商品列表 } }
/** * 购物车-商品-填充器 * * @author shaoshuai * */ public class SC_GoodsAdapter extends AbsAdapter<JsGoodsItem> { private List<Boolean> goodsSelList = new ArrayList<Boolean>();// 商品选择集合 private int storePosition;// 商铺索引 SC_ShopAdapter adapter; ShoppingCartPage sCartPage; public SC_GoodsAdapter(ShoppingCartPage scPage, List<JsGoodsItem> datas, SC_ShopAdapter adapter, int storePosition) { super(scPage.getActivity(), datas); this.sCartPage = scPage; this.adapter = adapter; this.storePosition = storePosition; for (int j = 0; j < mDatas.size(); j++) { goodsSelList.add(false); } } public List<Boolean> getSelect() { return goodsSelList; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate(R.layout.item_order_goods2, parent, false); holder = new ViewHolder(); holder.cb_select = (CheckBox) convertView.findViewById(R.id.cb_select); holder.tv_comment = (ImageView) convertView.findViewById(R.id.tv_comment); holder.tv_goods_name = (TextView) convertView.findViewById(R.id.tv_goods_name); holder.tv_goods_des = (TextView) convertView.findViewById(R.id.tv_goods_des); holder.tv_goods_price = (TextView) convertView.findViewById(R.id.tv_goods_price); holder.eas_num = (EditAddSubView) convertView.findViewById(R.id.eas_num); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } final JsGoodsItem item = (JsGoodsItem) getItem(position); holder.tv_comment.setImageResource(R.drawable.shangpin);// 商品图 holder.tv_goods_name.setText(item.goodsName);// 名称 holder.tv_goods_des.setText(item.goodsDes);// 详细 holder.tv_goods_price.setText("¥" + item.goodsPrice);// 价格 holder.eas_num.setNumScope(1, 10);// 设置数值范围 holder.eas_num.setNum(item.goodsNum);// 数量 holder.eas_num.setOnNumChangeListener(new INumChangeListener() { @Override public void onNumChange(View view, int num) { item.goodsNum = num; sCartPage.updateAmount();// 更新金额 } }); holder.cb_select.setChecked(goodsSelList.get(position));// 是否选择 holder.cb_select.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { goodsSelList.set(position, !goodsSelList.get(position)); // 更新商品对应商铺对应的全选按钮 if (goodsSelList.contains(false)) { adapter.getSelect().set(storePosition, false); } else { adapter.getSelect().set(storePosition, true); } // 更新购物车,全选按钮 if (adapter.getSelect().contains(false)) { sCartPage.checkAll(false); } else { sCartPage.checkAll(true); } sCartPage.updateAmount();// 更新金额 adapter.notifyDataSetChanged(); } }); return convertView; } /** * 创建一个本,减少findViewById的次数 */ private class ViewHolder { CheckBox cb_select;// 选择 ImageView tv_comment;// 商品图 TextView tv_goods_name;// 商品名 TextView tv_goods_des;// 商品详情 TextView tv_goods_price;// 商品价格 EditAddSubView eas_num;// 商品数量 } }