Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android購物車項目快速開發

Android購物車項目快速開發

編輯:關於Android編程

購物車項目,業務需要實現了一個購物車的項目,簡單的了解下實現邏輯:數據計算等是在Adapter中計算出來的,通過在Adapter中計算出來的數據就可以回調到Activity中進行訂單操作等功能業務邏輯,每一個店鋪產生的數據是走一條流程的,(業務需求:不是作為一個類似淘寶,京東的平台數據又由平台分發,所以我們實現的是一對一的客戶交易的交易流程)接著往下看:

1.界面使用到的控件

goodsAdapter = new GoodsCarAdapter(ShopCarAvtivity.this, result);
lv_refresh.setAdapter(goodsAdapter);
goodsAdapter.setCheckInterface(ShopCarAvtivity.this);// 關鍵步驟1,設置復選框接口
goodsAdapter.setModifyCountInterface(ShopCarAvtivity.this);// 關鍵步驟2,設置數量增減接口
for (int i = 0; i < goodsAdapter.getGroupCount(); i++) {
 lv_refresh.expandGroup(i);// 關鍵步驟3,初始化時,將ExpandableListView以展開的方式呈現
}

2.項目中使用到的數據接口

界面當中的復選框的接口回調

public interface CheckGoodsListener {
/**
 * 組選框狀態改變觸發的事件
 *
 * @param groupPosition 組元素位置
 * @param isChecked  組元素選中與否
 */
void checkGroup(int groupPosition, boolean isChecked);

/**
 * 子選框狀態改變時觸發的事件
 *
 * @param groupPosition 組元素位置
 * @param childPosition 子元素位置
 * @param isChecked  子元素選中與否
 */
void checkChild(int groupPosition, int childPosition, boolean isChecked);


/**
 * 購買
 * @param groupPosition
 * @param childPosition
 * @param isChecked
 */
void checkGoodsBuy(ShopCarModel shopCarModel, double totalMonery, int totalCount);

} 

商品增加和修改的接口

/**
 * 改變數量的接口
 * Created by zhuangAH on 2016-11-7.
 */

public interface ModifyCountListener {

 /**
  * 增加操作
  *
  * @param groupPosition 組元素位置
  * @param childPosition 子元素位置
  * @param showCountView 用於展示變化後數量的View
  * @param isChecked  子元素選中與否
  */
 void doIncrease(int groupPosition, int childPosition, View showCountView, boolean isChecked);

 /**
  * 刪減操作
  *
  * @param groupPosition 組元素位置
  * @param childPosition 子元素位置
  * @param showCountView 用於展示變化後數量的View
  * @param isChecked  子元素選中與否
  */
 void doDecrease(int groupPosition, int childPosition, View showCountView, boolean isChecked);

 /**
  * 刪除子item
  *
  * @param groupPosition
  * @param childPosition
  */
 void childDelete(int groupPosition, int childPosition);

}

3.在Adapter中計算商品的金額數量

單個店鋪中有多個商品,所以這個店鋪的布局,包含選擇全組的按鈕,使用了接口的回調 checkInterface.checkGroup(groupPosition, ((CheckBox) v).isChecked());來判斷是否選中全組,在Activity中進行數據的便利是否選中商品之後再刷新數據。

 @Override
 public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

  GroupViewHolder gholder = null;
  if (convertView != null && !(convertView.getTag() instanceof GroupViewHolder)) {
   convertView = null;
  }
  if (convertView == null) {
   gholder = new GroupViewHolder();
   convertView = View.inflate(context, R.layout.item_shopcart_group, null);
   gholder.determineChekbox = (CheckBox) convertView.findViewById(R.id.determine_chekbox);
   gholder.tvSourceName = (TextView) convertView.findViewById(R.id.tv_source_name);
   convertView.setTag(gholder);
  } else {
   gholder = (GroupViewHolder) convertView.getTag();
  }

  final ShopCarModel group = (ShopCarModel) getGroup(groupPosition);
  gholder.tvSourceName.setText(group.getFactoryName());
  gholder.determineChekbox.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    group.setChoosed(((CheckBox) v).isChecked());
    checkInterface.checkGroup(groupPosition, ((CheckBox) v).isChecked());
   }
  });
  gholder.determineChekbox.setChecked(group.isChoosed());
  return convertView;
 }

在Activity中計算便利數據

 /***
  * 校對組元素
  *
  * @param groupPosition 組元素位置
  * @param isChecked  組元素選中與否
  */
 @Override
 public void checkGroup(int groupPosition, boolean isChecked) {

  List<GoodsModel> goodsModelList = shopCarModelList.get(groupPosition).getGoodsModel();
  for (int i = 0; i < goodsModelList.size(); i++) {
   goodsModelList.get(i).setChoosed(isChecked);
  }
  goodsAdapter.notifyDataSetChanged();
 }

在商品中計算數據結果:

@Override
public View getChildView(final int groupPosition, final int childPosition, final boolean isLastChild, View convertView, final ViewGroup parent) {
 GoodsViewHolder goodsViewHolder = null;
 int totalCount = 0;
 double totalPrice = 0.00;

 if (convertView != null && !(convertView.getTag() instanceof GoodsViewHolder)) {
  convertView = null;
 }
 if (convertView == null) {
  goodsViewHolder = new GoodsViewHolder();
  convertView = View.inflate(context, R.layout.item_shopcart_product, null);
  goodsViewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.check_box);
  goodsViewHolder.ivAdapterListPic = (ImageView) convertView.findViewById(R.id.iv_adapter_list_pic);
  goodsViewHolder.tvIntro = (TextView) convertView.findViewById(R.id.tv_intro);
  goodsViewHolder.tvPrice = (TextView) convertView.findViewById(R.id.tv_price);
  goodsViewHolder.tvBuyNum = (TextView) convertView.findViewById(R.id.tv_buy_num);
  goodsViewHolder.rlNoEdtor = (RelativeLayout) convertView.findViewById(R.id.rl_no_edtor);
  goodsViewHolder.tvReduce = (TextView) convertView.findViewById(R.id.tv_reduce);
  goodsViewHolder.tvNum = (TextView) convertView.findViewById(R.id.tv_num);
  goodsViewHolder.tvAdd = (TextView) convertView.findViewById(R.id.tv_add);
  goodsViewHolder.llChangeNum = (LinearLayout) convertView.findViewById(R.id.ll_change_num);
  goodsViewHolder.layout_item_foot = (LinearLayout) convertView.findViewById(R.id.layout_item_foot);
  goodsViewHolder.tv_goods_number = (TextView) convertView.findViewById(R.id.tv_goods_number);
  goodsViewHolder.tv_goods_menoy = (TextView) convertView.findViewById(R.id.tv_goods_menoy);
  goodsViewHolder.tv_buys = (TextView) convertView.findViewById(R.id.tv_buys);
  goodsViewHolder.laytou_car = (LinearLayout) convertView.findViewById(R.id.laytou_car);
  convertView.setTag(goodsViewHolder);
 } else {
  goodsViewHolder = (GoodsViewHolder) convertView.getTag();
 }
 //進行數據操作
 final GoodsModel goodsInfo = (GoodsModel) getChild(groupPosition, childPosition);
 if (goodsInfo != null) {
  //數量初始化為0,金額初始化為0
  goodsViewHolder.tv_goods_number.setText(TypeUtils.toString(0));
  goodsViewHolder.tv_goods_menoy.setText("¥ " + NumberUtils.formatMoneyScale(0.00));

  List<GoodsModel> goodsModel = CarUtrils.getGoodsList(goodShop, groupPosition);
  //判斷是否最後一個
  if ((goodsModel.size() - 1) == childPosition) {
   goodsViewHolder.layout_item_foot.setVisibility(View.VISIBLE);
   /**
    * 統計操作<br>
    * 1.先清空全局計數器<br>
    * 2.遍歷所有子元素,只要是被選中狀態的,就進行相關的計算操作<br>
    * 3.給底部的textView進行數據填充
    */
   //1判斷商品是否選中,再進行計算
   for (int j = 0; j < goodsModel.size(); j++) {
    GoodsModel model = goodsModel.get(j);
    if (model.isChoosed()) {
     totalCount += model.getSelectQty();
     totalPrice += TypeUtils.toDouble(NumberUtils.multiply(TypeUtils.toBigDecimal(NumberUtils.formatRounded(model.getPrice())), NumberUtils.toBigDecimal(model.getSelectQty())));
    }
   }

   if (totalPrice != 0.00) {
    goodsViewHolder.tv_goods_number.setText(TypeUtils.toString(totalCount));
    goodsViewHolder.tv_goods_menoy.setText(NumberUtils.formatRounded(TypeUtils.toBigDecimal(totalPrice)));
    goodsViewHolder.tv_buys.setBackgroundColor(context.getResources().getColor(R.color.main_color));
    goodsViewHolder.tv_buys.setEnabled(true);
   } else {
    goodsViewHolder.tv_goods_number.setText(TypeUtils.toString(0));
    goodsViewHolder.tv_goods_menoy.setText("¥ " + NumberUtils.formatMoneyScale(0.00));
    goodsViewHolder.tv_buys.setBackgroundColor(context.getResources().getColor(R.color.resport_line));
    goodsViewHolder.tv_buys.setEnabled(false);
   }

  } else {
   goodsViewHolder.layout_item_foot.setVisibility(View.GONE);
  }
  //設置基礎數據
  if (goodsInfo.getImageSrc() != null) {
   Glide.with(context)
     .load(goodsInfo.getImageSrc())
     .centerCrop()
     .placeholder(R.mipmap.test2)
     .crossFade()
     .into(goodsViewHolder.ivAdapterListPic);
  }

  goodsViewHolder.tvIntro.setText(goodsInfo.getName());
  goodsViewHolder.tvPrice.setText("¥ " + NumberUtils.formatRounded(goodsInfo.getPrice()));
  goodsViewHolder.tvBuyNum.setText("X " + NumberUtils.formatQty(goodsInfo.getQty()));
  //set Goods Check
  goodsViewHolder.checkBox.setChecked(goodsInfo.isChoosed());
  goodsViewHolder.tvNum.setText(TypeUtils.toString(goodsInfo.getSelectQty()));

  //選中的狀態下才能觸發點擊事件
  goodsViewHolder.tvAdd.setEnabled(true);
  goodsViewHolder.tvReduce.setEnabled(true);
  //加減
  final GoodsViewHolder finalGoodsViewHolder = goodsViewHolder;
  goodsViewHolder.tvAdd.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    modifyCountInterface.doIncrease(groupPosition, childPosition, finalGoodsViewHolder.tvNum, finalGoodsViewHolder.checkBox.isChecked());// 暴露增加接口

   }
  });
  final GoodsViewHolder finalGoodsViewHolder1 = goodsViewHolder;
  goodsViewHolder.tvReduce.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    modifyCountInterface.doDecrease(groupPosition, childPosition, finalGoodsViewHolder1.tvNum, finalGoodsViewHolder1.checkBox.isChecked());// 暴露刪減接口
   }
  });

  //goods check state OnClick
  goodsViewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    goodsInfo.setChoosed(((CheckBox) v).isChecked());
    ((CheckBox) v).setChecked(((CheckBox) v).isChecked());
    checkInterface.checkChild(groupPosition, childPosition, ((CheckBox) v).isChecked());
   }
  });
  //onClick to OrderDetailActivity
  final double finalTotalPrice = totalPrice;
  final int finalTotalCount = totalCount;
  goodsViewHolder.tv_buys.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    //check goods is not null
    //if not null can go other activity
    ShopCarModel shopCar = CarUtrils.getCheckShopCar(goodShop, groupPosition);
    if (shopCar != null && shopCar.getGoodsModel().size() > 0) {
     checkInterface.checkGoodsBuy(shopCar, finalTotalPrice, finalTotalCount);
    }
   }
  });
 }

 goodsViewHolder.laytou_car.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
   UIHelper.toShopCarGoodsDetailActivity(context, goodsInfo);
  }
 });
 return convertView;
}

以上代碼的是先判斷當前組是否為一個店鋪中的最後一個,在最後一個元素中局部計算當前的Group的數據,根據數據來選擇是否復位數據展示以及顯示,把計算的數據展示出來,最後通過接口回調的方式跳轉界面,把數據傳到Activity中去。就這樣子就處理完這個購物車的邏輯,以上可能不符合你的邏輯,但是你可以稍微修改就拿來使用。

Github地址: https://github.com/anhuifix/singleShopCar/tree/master

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved