【参考借鉴】Android仿淘宝购物车,玩转电商购物车
首先看一下效果
购物车主要由ExpandableListView来实现,其中逻辑比较复杂的是价钱与各个操作之间的交互,掌握的交互就掌握了购物车的原理。
11111.png 222.png
实现过程
1.配置group与child的item布局文件。记住要设置group不可展开。
item_group.xml的配置如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/backgroundWhite">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/toolbar_padLeft">
<CheckBox
android:id="@+id/item_shopcart_group_checkBox"
style="@style/Shopcart_Checkbox_Style" />
<TextView
android:layout_marginLeft="@dimen/toolbar_padLeft"
style="@style/Shopcart_shopName_Style"
android:text="赵同学店铺"
android:id="@+id/item_shopcart_group_nameTv"/>
<Button
android:id="@+id/item_shopcart_group_editBtn"
style="@style/Shopcart_EditBtn_Style"
android:text="编辑" />
</LinearLayout>
</RelativeLayout>
item_child.xml的配置如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/backgroundWhite">
<LinearLayout
android:gravity="center_vertical"
android:paddingLeft="@dimen/toolbar_padLeft"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/item_shopcart_child_checkBox"
style="@style/Shopcart_Checkbox_Style"/>
<!--图片的paddingTom与paddingBotton与内容布局的一样:如需修改,请注意-->
<ImageView
android:layout_marginLeft="@dimen/toolbar_padLeft"
style="@style/Shopcart_Good_img_Style"
android:id="@+id/item_shopcart_child_goods_image"
android:src="@mipmap/goods_img"/>
<!--未编辑时的布局-->
<LinearLayout
android:id="@+id/item_shopcart_child_goods_data_noedit"
android:paddingBottom="@dimen/toolbar_padLeft"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingRight="@dimen/padding_top">
<TextView
style="@style/Shopcart_Good_title_Style"
android:text="雅诗兰黛气垫保湿遮瑕裸妆防水清爽防晒不脱妆"
android:id="@+id/item_shopcart_child_goods_name"/>
<TextView
style="@style/Shopcart_Good_Size_Style"
android:id="@+id/item_shopcart_child_goods_size"
android:text="规格:小猪 黄色"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
style="@style/Shopcart_Good_Price_Style"
android:id="@+id/item_shopcart_child_goods_price"
android:text="¥100.00" />
<TextView
style="@style/Shopcart_Good_Count_Style"
android:text="x1"
android:textSize="24px"
android:id="@+id/item_shopcart_child_goods_buyNum"/>
</LinearLayout>
</LinearLayout>
<!--编辑时的布局-->
<LinearLayout
android:visibility="gone"
android:id="@+id/item_shopcart_child_edit_goods_data"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:paddingBottom="@dimen/toolbar_padLeft"
android:paddingTop="@dimen/toolbar_padLeft">
<LinearLayout
android:layout_marginTop="12px"
android:layout_width="match_parent"
android:layout_height="66px"
android:layout_marginRight="@dimen/padding_top"
android:orientation="horizontal">
<RelativeLayout
android:id="@+id/item_shopcart_child_reduce"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@mipmap/shopping_cart_minus_bg">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@mipmap/shopping_cart_minus" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@mipmap/shopping_cart_num_bg">
<TextView
android:id="@+id/item_shopcart_child_goods_Num_edit"
style="@style/Shopcart_Good_CountEdit_Style"
android:layout_centerInParent="true"
android:text="3" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/item_shopcart_child_add"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@mipmap/shopping_cart_add_bg">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@mipmap/shopping_cart_add" />
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical">
<TextView
android:id="@+id/item_shopcart_child_goods_sizeEdit"
style="@style/Shopcart_Good_Size_Style"
android:layout_width="wrap_content"
android:text="规格:小猪 黄色" />
<ImageView
android:layout_toRightOf="@id/item_shopcart_child_goods_sizeEdit"
android:layout_alignBottom="@id/item_shopcart_child_goods_sizeEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/toolbar_padLeft"
android:src="@mipmap/shopping_cart_unfold" />
</RelativeLayout>
</LinearLayout>
<TextView
android:id="@+id/item_shopcart_child_del_goods"
style="@style/Shopcart_Good_delete_Style" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/item_shopcart_child_divideId"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/divide"
android:layout_marginLeft="@dimen/toolbar_padLeft"
android:layout_marginRight="@dimen/toolbar_padLeft"/>
</LinearLayout>
2.自定义适配器继承BaseExpandableListAdapter
一些点击操作由实现接口来处理。
package com.zxh.project.taobaoshoppingcart.adapter;
import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StrikethroughSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.zxh.project.taobaoshoppingcart.R;
import com.zxh.project.taobaoshoppingcart.customviews.CustomBaseDialog;
import com.zxh.project.taobaoshoppingcart.entity.ShoppingcartGoodsInfo;
import com.zxh.project.taobaoshoppingcart.entity.ShoppingcartStoreInfo;
import java.util.List;
import java.util.Map;
import butterknife.BindView;
import butterknife.ButterKnife;
/**
* 购物车expandableListview适配器
* @author ZhaoXH on 2017/8/11.
*/
public class ShoppingcartAdapter extends BaseExpandableListAdapter {
private List<ShoppingcartStoreInfo> groups;
//这个String对应着StoreInfo的Id,也就是店铺的Id
private Map<String, List<ShoppingcartGoodsInfo>> childrens;
private Context mcontext;
private CheckInterface checkInterface;
private ModifyCountInterface modifyCountInterface;
private GroupEditorListener groupEditorListener;
private int count = 0;
private boolean flag=true; //组的编辑按钮是否可见,true可见,false不可见
public ShoppingcartAdapter(List<ShoppingcartStoreInfo> groups, Map<String, List<ShoppingcartGoodsInfo>> childrens, Context mcontext) {
this.groups = groups;
this.childrens = childrens;
this.mcontext = mcontext;
}
@Override
public int getGroupCount() {
return groups==null?0:groups.size();
}
@Override
public int getChildrenCount(int groupPosition) {
String groupId = groups.get(groupPosition).getId();
return childrens.get(groupId).size();
}
@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
List<ShoppingcartGoodsInfo> childs = childrens.get(groups.get(groupPosition).getId());
return childs.get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
//=========================================================================
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
final GroupViewHolder groupViewHolder;
if (convertView == null) {
convertView = View.inflate(mcontext, R.layout.item_shoppingcart_group, null);
groupViewHolder = new GroupViewHolder(convertView);
convertView.setTag(groupViewHolder);
} else {
groupViewHolder = (GroupViewHolder) convertView.getTag();
}
final ShoppingcartStoreInfo group = (ShoppingcartStoreInfo) getGroup(groupPosition);
groupViewHolder.storeName.setText(group.getName());
groupViewHolder.storeCheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
group.setChoosed(((CheckBox) v).isChecked());
checkInterface.checkGroup(groupPosition, ((CheckBox) v).isChecked());
}
});
groupViewHolder.storeCheckBox.setChecked(group.isChoosed());
/**【文字指的是组的按钮的文字】
* 当我们按下ActionBar的 "编辑"按钮, 应该把所有组的文字显示"编辑",并且设置按钮为不可见
* 当我们完成编辑后,再把组的编辑按钮设置为可见
* 不懂,请自己操作淘宝,观察一遍
*/
if(group.isActionBarEditor()){
group.setEditor(false);
groupViewHolder.storeEdit.setVisibility(View.INVISIBLE);
flag=false;
}else{
flag=true;
groupViewHolder.storeEdit.setVisibility(View.VISIBLE);
}
/**
* 思路:当我们按下组的"编辑"按钮后,组处于编辑状态,文字显示"完成"
* 当我们点击“完成”按钮后,文字显示"编辑“,组处于未编辑状态
*/
if (group.isEditor()) {
groupViewHolder.storeEdit.setText("完成");
} else {
groupViewHolder.storeEdit.setText("编辑");
}
groupViewHolder.storeEdit.setOnClickListener(new GroupViewClick(group, groupPosition, groupViewHolder.storeEdit));
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final ChildViewHolder childViewHolder;
if (convertView == null) {
convertView = View.inflate(mcontext, R.layout.item_shoppingcart_child, null);
childViewHolder = new ChildViewHolder(convertView);
convertView.setTag(childViewHolder);
} else {
childViewHolder = (ChildViewHolder) convertView.getTag();
}
/**
* 根据组的编辑按钮的可见与不可见,去判断是组对下辖的子元素编辑 还是ActionBar对组的下瞎元素的编辑
* 如果组的编辑按钮可见,那么肯定是组对自己下辖元素的编辑
* 如果组的编辑按钮不可见,那么肯定是ActionBar对组下辖元素的编辑
*/
if(flag){
if (groups.get(groupPosition).isEditor()) {
childViewHolder.editGoodsData.setVisibility(View.VISIBLE);
childViewHolder.delGoods.setVisibility(View.VISIBLE);
childViewHolder.goodsDataNoedit.setVisibility(View.GONE);
} else {
childViewHolder.delGoods.setVisibility(View.VISIBLE);
childViewHolder.goodsDataNoedit.setVisibility(View.VISIBLE);
childViewHolder.editGoodsData.setVisibility(View.GONE);
}
}else{
if(groups.get(groupPosition).isActionBarEditor()){
childViewHolder.delGoods.setVisibility(View.GONE);
childViewHolder.editGoodsData.setVisibility(View.VISIBLE);
childViewHolder.goodsDataNoedit.setVisibility(View.GONE);
}else{
childViewHolder.delGoods.setVisibility(View.VISIBLE);
childViewHolder.goodsDataNoedit.setVisibility(View.VISIBLE);
childViewHolder.editGoodsData.setVisibility(View.GONE);
}
}
final ShoppingcartGoodsInfo child = (ShoppingcartGoodsInfo) getChild(groupPosition, childPosition);
if (child != null) {
childViewHolder.goodsName.setText(child.getDesc());
childViewHolder.goodsPrice.setText("¥" + child.getPrice() + "");
childViewHolder.goodsNumEdit.setText(String.valueOf(child.getCount()));
childViewHolder.goodsImage.setImageResource(R.mipmap.goods_img);
childViewHolder.goods_size.setText("类型:" + child.getColor() + ",颜色:" + child.getSize());
childViewHolder.goodsSizeEdit.setText("类型:" + child.getColor() + ",颜色:" + child.getSize());
//设置打折前的原价
SpannableString spannableString = new SpannableString("¥" + child.getPrime_price() + "");
StrikethroughSpan span = new StrikethroughSpan();
spannableString.setSpan(span,0,spannableString.length()-1+1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
//避免无限次的append
// if (childViewHolder.goodsPrimePrice.length() > 0) {
// childViewHolder.goodsPrimePrice.setText("");
// }
// childViewHolder.goodsPrimePrice.setText(spannableString);
childViewHolder.goodsBuyNum.setText("x" + child.getCount() + "");
childViewHolder.childCheckBox.setChecked(child.isChoosed());
childViewHolder.childCheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
child.setChoosed(((CheckBox) v).isChecked());
childViewHolder.childCheckBox.setChecked(((CheckBox) v).isChecked());
checkInterface.checkChild(groupPosition, childPosition, ((CheckBox) v).isChecked());
}
});
childViewHolder.increaseGoodsNum.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
modifyCountInterface.doIncrease(groupPosition, childPosition, childViewHolder.goodsNumEdit, childViewHolder.childCheckBox.isChecked());
}
});
childViewHolder.reduceGoodsNum.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
modifyCountInterface.doDecrease(groupPosition, childPosition, childViewHolder.goodsNumEdit, childViewHolder.childCheckBox.isChecked());
}
});
childViewHolder.goodsNumEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// showDialog(groupPosition,childPosition,childViewHolder.goodsNumEdit,childViewHolder.childCheckBox.isChecked(),child);
}
});
childViewHolder.delGoods.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final CustomBaseDialog dialog = new CustomBaseDialog(mcontext);
dialog.setContent("确定删除该商品吗?");
dialog.setCustomOnClickListener(new CustomBaseDialog.OnCustomDialogListener() {
@Override
public void setYesOnclick() {
modifyCountInterface.childDelete(groupPosition,childPosition);
dialog.dismiss();
}
@Override
public void setNoOnclick() {
dialog.dismiss();
}
});
dialog.show();
}
});
}
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
/**
* 使某个小组处于编辑状态
*/
private class GroupViewClick implements View.OnClickListener {
private ShoppingcartStoreInfo group;
private int groupPosition;
private TextView editor;
public GroupViewClick(ShoppingcartStoreInfo group, int groupPosition, TextView editor) {
this.group = group;
this.groupPosition = groupPosition;
this.editor = editor;
}
@Override
public void onClick(View v) {
if (editor.getId() == v.getId()) {
groupEditorListener.groupEditor(groupPosition);
if (group.isEditor()) {
group.setEditor(false);
} else {
group.setEditor(true);
}
notifyDataSetChanged();
}
}
}
/**
* 店铺的复选框
*/
public interface CheckInterface {
/**
* 组选框状态改变触发的事件
*
* @param groupPosition 组元素的位置
* @param isChecked 组元素的选中与否
*/
void checkGroup(int groupPosition, boolean isChecked);
/**
* 子选框状态改变触发的事件
*
* @param groupPosition 组元素的位置
* @param childPosition 子元素的位置
* @param isChecked 子元素的选中与否
*/
void checkChild(int groupPosition, int childPosition, boolean isChecked);
}
/**
* 改变数量的接口
*/
public interface ModifyCountInterface {
/**
* 增加操作
*
* @param groupPosition 组元素的位置
* @param childPosition 子元素的位置
* @param showCountView 用于展示变化后数量的View
* @param isChecked 子元素选中与否
*/
void doIncrease(int groupPosition, int childPosition, View showCountView, boolean isChecked);
void doDecrease(int groupPosition, int childPosition, View showCountView, boolean isChecked);
void doUpdate(int groupPosition, int childPosition, View showCountView, boolean isChecked);
/**
* 删除子Item
*
* @param groupPosition
* @param childPosition
*/
void childDelete(int groupPosition, int childPosition);
}
public GroupEditorListener getGroupEditorListener() {
return groupEditorListener;
}
public void setGroupEditorListener(GroupEditorListener groupEditorListener) {
this.groupEditorListener = groupEditorListener;
}
public CheckInterface getCheckInterface() {
return checkInterface;
}
public void setCheckInterface(CheckInterface checkInterface) {
this.checkInterface = checkInterface;
}
public ModifyCountInterface getModifyCountInterface() {
return modifyCountInterface;
}
public void setModifyCountInterface(ModifyCountInterface modifyCountInterface) {
this.modifyCountInterface = modifyCountInterface;
}
/**
* 监听编辑状态
*/
public interface GroupEditorListener {
void groupEditor(int groupPosition);
}
static class GroupViewHolder {
@BindView(R.id.item_shopcart_group_checkBox)
CheckBox storeCheckBox;
@BindView(R.id.item_shopcart_group_nameTv)
TextView storeName;
@BindView(R.id.item_shopcart_group_editBtn)
TextView storeEdit;
public GroupViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
static class ChildViewHolder {
@BindView(R.id.item_shopcart_child_checkBox)
CheckBox childCheckBox;
@BindView(R.id.item_shopcart_child_goods_image)
ImageView goodsImage;
@BindView(R.id.item_shopcart_child_goods_name)
TextView goodsName;
@BindView(R.id.item_shopcart_child_goods_size)
TextView goods_size;
@BindView(R.id.item_shopcart_child_goods_price)
TextView goodsPrice;
// @BindView(R.id.goods_prime_price)
// TextView goodsPrimePrice;
@BindView(R.id.item_shopcart_child_goods_buyNum)
TextView goodsBuyNum;
@BindView(R.id.item_shopcart_child_goods_data_noedit)
LinearLayout goodsDataNoedit;
@BindView(R.id.item_shopcart_child_reduce)
RelativeLayout reduceGoodsNum;
@BindView(R.id.item_shopcart_child_goods_Num_edit)
TextView goodsNumEdit;
@BindView(R.id.item_shopcart_child_add)
RelativeLayout increaseGoodsNum;
@BindView(R.id.item_shopcart_child_goods_sizeEdit)
TextView goodsSizeEdit;
@BindView(R.id.item_shopcart_child_del_goods)
TextView delGoods;
@BindView(R.id.item_shopcart_child_edit_goods_data)
LinearLayout editGoodsData;
public ChildViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}
3.重要的点击事件
右上角编辑按钮
flag = !flag;//false就是右上角编辑,ture就是完成
/**
* ActionBar标题上点编辑的时候,只显示每一个店铺的商品修改界面
* ActionBar标题上点完成的时候,只显示每一个店铺的商品信息界面
*/
private void setActionBarEditor() {
for (int i = 0; i < groups.size(); i++) {
ShoppingcartStoreInfo group = groups.get(i);
if (group.isActionBarEditor()) {
group.setActionBarEditor(false);
} else {
group.setActionBarEditor(true);
}
}
adapter.notifyDataSetChanged();
}
private void setVisiable() {
if (flag) {
orderInfo.setVisibility(View.GONE);
shareInfo.setVisibility(View.VISIBLE);
actionBarEdit.setText("完成");
} else {
orderInfo.setVisibility(View.VISIBLE);
shareInfo.setVisibility(View.GONE);
actionBarEdit.setText("编辑");
}
}
右上角为编辑状态下,删除按钮(选中删除)
/**
* 删除操作
* 1.不要边遍历边删除,容易出现数组越界的情况
* 2.把将要删除的对象放进相应的容器中,待遍历完,用removeAll的方式进行删除
*/
private void doDelete() {
List<ShoppingcartStoreInfo> toBeDeleteGroups = new ArrayList<ShoppingcartStoreInfo>(); //待删除的组元素
for (int i = 0; i < groups.size(); i++) {
ShoppingcartStoreInfo group = groups.get(i);
if (group.isChoosed()) {
toBeDeleteGroups.add(group);
}
List<ShoppingcartGoodsInfo> toBeDeleteChilds = new ArrayList<ShoppingcartGoodsInfo>();//待删除的子元素
List<ShoppingcartGoodsInfo> child = childs.get(group.getId());
for (int j = 0; j < child.size(); j++) {
if (child.get(j).isChoosed()) {
toBeDeleteChilds.add(child.get(j));
}
}
child.removeAll(toBeDeleteChilds);
}
groups.removeAll(toBeDeleteGroups);
//重新设置购物车
setCartNum();
adapter.notifyDataSetChanged();
}
底部左下角的全选、反选按钮
/**
* 全选和反选
* 错误标记:在这里出现过错误
*/
private void doCheckAll() {
for (int i = 0; i < groups.size(); i++) {
ShoppingcartStoreInfo group = groups.get(i);
group.setChoosed(allCheckBox.isChecked());
List<ShoppingcartGoodsInfo> child = childs.get(group.getId());
for (int j = 0; j < child.size(); j++) {
child.get(j).setChoosed(allCheckBox.isChecked());//这里出现过错误
}
}
adapter.notifyDataSetChanged();
calulate();
}
增加商品按钮
@Override
public void doIncrease(int groupPosition, int childPosition, View showCountView, boolean isChecked) {
ShoppingcartGoodsInfo good = (ShoppingcartGoodsInfo) adapter.getChild(groupPosition, childPosition);
int count = good.getCount();
count++;
good.setCount(count);
((TextView) showCountView).setText(String.valueOf(count));
adapter.notifyDataSetChanged();
calulate();
}
减少商品按钮
/**
* @param groupPosition
* @param childPosition
* @param showCountView
* @param isChecked
*/
@Override
public void doDecrease(int groupPosition, int childPosition, View showCountView, boolean isChecked) {
ShoppingcartGoodsInfo good = (ShoppingcartGoodsInfo) adapter.getChild(groupPosition, childPosition);
int count = good.getCount();
if (count == 1) {
return;
}
count--;
good.setCount(count);
((TextView) showCountView).setText("" + count);
adapter.notifyDataSetChanged();
calulate();
}
组group选中按钮
/**
* @param groupPosition 组元素的位置
* @param isChecked 组元素的选中与否
* 思路:组元素被选中了,那么下辖全部的子元素也被选中
*/
@Override
public void checkGroup(int groupPosition, boolean isChecked) {
ShoppingcartStoreInfo group = groups.get(groupPosition);
List<ShoppingcartGoodsInfo> child = childs.get(group.getId());
for (int i = 0; i < child.size(); i++) {
child.get(i).setChoosed(isChecked);
}
if (isCheckAll()) {
allCheckBox.setChecked(true);//全选
} else {
allCheckBox.setChecked(false);//反选
}
adapter.notifyDataSetChanged();
calulate();
}
子child选中按钮
/**
* @param groupPosition 组元素的位置
* @param childPosition 子元素的位置
* @param isChecked 子元素的选中与否
*/
@Override
public void checkChild(int groupPosition, int childPosition, boolean isChecked) {
boolean allChildSameState = true; //判断该组下面的所有子元素是否处于同一状态
ShoppingcartStoreInfo group = groups.get(groupPosition);
List<ShoppingcartGoodsInfo> child = childs.get(group.getId());
for (int i = 0; i < child.size(); i++) {
//不选全中
if (child.get(i).isChoosed() != isChecked) {
allChildSameState = false;
break;
}
}
if (allChildSameState) {
group.setChoosed(isChecked);//如果子元素状态相同,那么对应的组元素也设置成这一种的同一状态
} else {
group.setChoosed(false);//否则一律视为未选中
}
if (isCheckAll()) {
allCheckBox.setChecked(true);//全选
} else {
allCheckBox.setChecked(false);//反选
}
adapter.notifyDataSetChanged();
calulate();
}
子child中删除按钮
/**
* @param groupPosition
* @param childPosition 思路:当子元素=0,那么组元素也要删除
*/
@Override
public void childDelete(int groupPosition, int childPosition) {
ShoppingcartStoreInfo group = groups.get(groupPosition);
List<ShoppingcartGoodsInfo> child = childs.get(group.getId());
child.remove(childPosition);
if (child.size() == 0) {
groups.remove(groupPosition);
}
adapter.notifyDataSetChanged();
calulate();
}
计算商品价钱
/**
* 计算商品总价格,操作步骤
* 1.先清空全局计价,计数
* 2.遍历所有的子元素,只要是被选中的,就进行相关的计算操作
* 3.给textView填充数据
*/
private void calulate() {
mtotalPrice = 0.00;
mtotalCount = 0;
for (int i = 0; i < groups.size(); i++) {
ShoppingcartStoreInfo group = groups.get(i);
List<ShoppingcartGoodsInfo> child = childs.get(group.getId());
for (int j = 0; j < child.size(); j++) {
ShoppingcartGoodsInfo good = child.get(j);
if (good.isChoosed()) {
mtotalCount++;
mtotalPrice += good.getPrice() * good.getCount();
}
}
}
totalPrice.setText("¥" + mtotalPrice + "");
goPay.setText("结算(" + mtotalCount + ")");
if (mtotalCount == 0) {
setCartNum();
} else {
shoppingcatNum.setText("购物车(" + mtotalCount + ")");//类似淘宝标题栏的购物车数量
}
}
更新商品数量
@Override
public void doUpdate(int groupPosition, int childPosition, View showCountView, boolean isChecked) {
ShoppingcartGoodsInfo good = (ShoppingcartGoodsInfo) adapter.getChild(groupPosition, childPosition);
int count = good.getCount();
((TextView) showCountView).setText(String.valueOf(count));
adapter.notifyDataSetChanged();
calulate();
}
小结
购物车的逻辑还是比较复杂的,只要理清了其中的关系,代码写起来就简单多了。如果实在自己从头写不出来的话,就参考一下大神的代码。我就是参考大神的思路理清后才写出来的。欢迎大家查看。
网友评论