最近的項目,用到了商城系統,現在把購物車模塊抽取一下,搞個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;// 商品數量
}
}