一朋友管我要個單商家的購物車實現功能,我說這個東西不很簡單嘛,他說你幹了這麼久了連一個購物車的模板都沒有嗎?他這句引起了我的反思,購物車用的時候很少,基本每次都是現寫,這樣確實花費了不少時間,如果有一個現成的Demo或者模板,任何開發者拿過來只需要簡單的改一改,馬上就可以應用,想必也是極好的。
下面我說兩種常見的樣式,一種是單商家的購物車,另外一種類似淘寶那種多商家那種購物車。
**
單商家購物車
老規矩先上圖
長按Item刪除
主頁代碼實現
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alone_shop);
initView();
}
private void initView() {
initData();
initAdapter();
tvGoodsMoney = findViewById(R.id.tv_goods_money);
tvMoney = findViewById(R.id.tv_money);
allCheckBox = findViewById(R.id.all_checkBox);
rl_no_data_a = findViewById(R.id.rl_no_data_a);
rl_no_data_b = findViewById(R.id.rl_no_data_b);
allCheckBox.setOnClickListener(this);
llView = findViewById(R.id.ll_layout);
}
private void initData() {
list = new ArrayList<>();
list.add(new AloneShopBean("1", "架豆王", "遼寧瀋陽新鮮蔬菜", 1, "30.00"));
list.add(new AloneShopBean("2", "架豆王(精品)", "新鮮蔬菜", 1, "50.00"));
list.add(new AloneShopBean("3", "架豆王", "蔬菜不隔夜,聯繫方式xxx", 2, "25.00"));
list.add(new AloneShopBean("4", "架豆王(良品)", "歡迎採購", 1, "35.00"));
list.add(new AloneShopBean("5", "架豆王", "蔬菜不隔夜,聯繫方式xxx", 2, "25.00"));
}
/**
* 初始化適配器
*/
private void initAdapter() {
RecyclerView rcList = findViewById(R.id.rc_list);
rcList.setLayoutManager(new LinearLayoutManager(AloneShopActivity.this));
adapterX = new AloneShopAdapter(R.layout.item_shop_car, list);
adapterX.setCheckInterface(this);
adapterX.setModifyCountInterface(this);
rcList.setAdapter(adapterX);
//長按刪除
adapterX.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
ISDelete(position);
return true;
}
});
}
private void ISDelete(final int position) {
final View contentView = LayoutInflater.from(AloneShopActivity.this).inflate(R.layout.dialog_windows_delete, null);
mCustomPopWindowCause = new CustomPopWindow.PopupWindowBuilder(AloneShopActivity.this)
.setView(contentView)
.enableBackgroundDark(true) //彈出popWindow時,背景是否變暗
.setBgDarkAlpha(0.6f) // 控制亮度
.enableOutsideTouchableDissmiss(true)
.create();
mCustomPopWindowCause.showAtLocation(llView, Gravity.CENTER, 0, 0);
contentView.findViewById(R.id.dialog_btn_cancel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCustomPopWindowCause.dissmiss();
}
});
contentView.findViewById(R.id.dialog_btn_sure).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.remove(position);
mCustomPopWindowCause.dissmiss();
adapterX.notifyDataSetChanged();
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.all_checkBox:
doCheckAll();
break;
case R.id.tv_close_k:
//結算邏輯
break;
}
}
@Override
public void checkGroup(int groupPosition, boolean isChecked) {
list.get(groupPosition).setChecked(isChecked);
if (isCheckAll()) {
allCheckBox.setChecked(true);//全選
} else {
allCheckBox.setChecked(false);//反選
}
adapterX.notifyDataSetChanged();
calulate();
}
/**
* @return 判斷組元素是否全選
*/
private boolean isCheckAll() {
for (AloneShopBean group : list) {
if (!group.isChecked()) {
return false;
}
}
return true;
}
@Override
public void doIncrease(int groupPosition, View showCountView, boolean isChecked) {
AloneShopBean good = adapterX.getData().get(groupPosition);
int count = good.getNum();
count++;
good.setNum(count);
((TextView) showCountView).setText(String.valueOf(count));
adapterX.notifyDataSetChanged();
calulate();
}
@Override
public void doDecrease(int groupPosition, View showCountView, boolean isChecked) {
AloneShopBean good = adapterX.getData().get(groupPosition);
int count = good.getNum();
if (count == 1) {
return;
}
count--;
good.setNum(count);
((TextView) showCountView).setText("" + count);
adapterX.notifyDataSetChanged();
calulate();
}
@Override
public void childDelete(int groupPosition) {
calulate();
}
private void doCheckAll() {
for (int i = 0; i < list.size(); i++) {
AloneShopBean group = list.get(i);
group.setChecked(allCheckBox.isChecked());
}
adapterX.notifyDataSetChanged();
calulate();
}
private void calulate() {
mtotalPrice = 0.00;
for (int i = 0; i < list.size(); i++) {
AloneShopBean goods = list.get(i);
if (goods.isChecked()) {
BigDecimal b1 = new BigDecimal(goods.getMoney());
BigDecimal b2 = new BigDecimal(goods.getNum() + "");
mtotalPrice += b1.multiply(b2).doubleValue();
}
}
String format = String.format("%.2f", mtotalPrice);
tvMoney.setText("¥ " + format);
tvGoodsMoney.setText("商品合計: ¥ " + format);
}
適配器代碼實現
public class AloneShopAdapter extends BaseQuickAdapter<AloneShopBean, BaseViewHolder> {
private CheckInterface checkInterface;
private ModifyCountInterface modifyCountInterface;
private TextView tvNumber;
public AloneShopAdapter(int layoutResId, @Nullable List<AloneShopBean> data) {
super(layoutResId, data);
}
@Override
protected void convert(final BaseViewHolder helper, final AloneShopBean item) {
final CheckBox singleCheckBox = helper.getView(R.id.single_check_box);
ImageView ivAdd = helper.getView(R.id.iv_add);
ImageView ivMinus = helper.getView(R.id.iv_minus);
tvNumber = helper.getView(R.id.tv_number);
tvNumber.setText(String.valueOf(item.getNum()));
helper.setText(R.id.tv_goods_name, item.getName())
.setText(R.id.tv_describe, item.getDescribe());
String price = item.getMoney();
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("¥").append(price);
SpannableString spannableString = new SpannableString(stringBuffer);
AbsoluteSizeSpan absoluteSizeSpan17 = new AbsoluteSizeSpan(AutoSizeUtils.sp2px(mContext, 17));
AbsoluteSizeSpan absoluteSizeSpan14 = new AbsoluteSizeSpan(AutoSizeUtils.sp2px(mContext, 14));
spannableString.setSpan(absoluteSizeSpan17, 0, stringBuffer.indexOf("."), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
spannableString.setSpan(absoluteSizeSpan14, stringBuffer.indexOf("."), stringBuffer.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
helper.setText(R.id.price, spannableString);
singleCheckBox.setChecked(item.isChecked());
singleCheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
item.setChecked(((CheckBox) v).isChecked());
singleCheckBox.setChecked(((CheckBox) v).isChecked());
checkInterface.checkGroup(helper.getLayoutPosition(), ((CheckBox) v).isChecked());
}
});
ivAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
modifyCountInterface.doIncrease(helper.getLayoutPosition(), tvNumber, singleCheckBox.isChecked());
}
});
ivMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
modifyCountInterface.doDecrease(helper.getLayoutPosition(), tvNumber, singleCheckBox.isChecked());
}
});
}
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 CheckInterface {
/**
* 組選框狀態改變觸發的事件
*
* @param groupPosition 組元素的位置
* @param isChecked 組元素的選中與否
*/
void checkGroup(int groupPosition, boolean isChecked);
}
/**
* 改變數量的接口
*/
public interface ModifyCountInterface {
/**
* 增加操作
*
* @param groupPosition 組元素的位置
* @param showCountView 用於展示變化後數量的View
* @param isChecked 子元素選中與否
*/
void doIncrease(int groupPosition, View showCountView, boolean isChecked);
void doDecrease(int groupPosition, View showCountView, boolean isChecked);
/**
* 刪除子Item
*
* @param groupPosition
*
*/
void childDelete(int groupPosition);
}
}
多商家購物車
還是老規矩
主頁代碼實現
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multi_shop);
initView();
initPtrFrame();
initData();
initAdapter();
}
private void initPtrFrame() {
final PtrClassicDefaultHeader header = new PtrClassicDefaultHeader(this);
header.setPadding(dp2px(20), dp2px(20), 0, 0);
mPtrFrame.setHeaderView(header);
mPtrFrame.addPtrUIHandler(header);
mPtrFrame.setPtrHandler(new PtrDefaultHandler() {
@Override
public void onRefreshBegin(PtrFrameLayout frame) {
mPtrFrame.postDelayed(new Runnable() {
@Override
public void run() {
mPtrFrame.refreshComplete();
}
}, 2000);
}
@Override
public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
}
});
}
private void initView() {
headRight = findViewById(R.id.head_right);
headRight.setVisibility(View.VISIBLE);
headRight.setOnClickListener(this);
listView = findViewById(R.id.listView);
allCheckBox = findViewById(R.id.all_checkBox);
allCheckBox.setOnClickListener(this);
tvPrice = findViewById(R.id.tv_price);
llCalculate = findViewById(R.id.ll_calculate);
llCalculate.setOnClickListener(this);
rlOrderInfo = findViewById(R.id.rl_order_info);
llDelete = findViewById(R.id.ll_delete);
llDelete.setOnClickListener(this);
rlDeleteInfo = findViewById(R.id.rl_delete_info);
llCart = findViewById(R.id.ll_cart);
mPtrFrame = findViewById(R.id.mPtrframe);
llBottomView = findViewById(R.id.ll_bottom_view);
empty_shopcart = findViewById(R.id.layout_empty_shop);
}
/**
* 初始化數據
*/
private void initData() {
hashMapGoodsId = new HashMap<>();
xc = new ArrayList<>();//與Demo 無關
groups = new ArrayList<StoreBean>();
childs = new HashMap<>();
for (int i = 0; i < 3; i++) {
groups.add(new StoreBean(i + "", "天字第" + i + "號店鋪"));
List<MultiGoodsBean> goods = new ArrayList<>();
goods.add(new MultiGoodsBean("1", "架豆王", "遼寧瀋陽新鮮蔬菜", 1, "30.00"));
goods.add(new MultiGoodsBean("2", "架豆王(精品)", "新鮮蔬菜", 1, "50.00"));
goods.add(new MultiGoodsBean("3", "架豆王", "蔬菜不隔夜,聯繫方式xxx", 2, "25.00"));
goods.add(new MultiGoodsBean("4", "架豆王(良品)", "歡迎採購", 1, "35.00"));
childs.put(groups.get(i).getId(), goods);
}
}
private void initAdapter() {
adapterX = new MultiShopAdapter(groups, childs, MultiShopActivity.this);
adapterX.setCheckInterface(this);//關鍵步驟1:設置複選框的接口
adapterX.setModifyCountInterface(this); //關鍵步驟2:設置增刪減的接口
listView.setGroupIndicator(null); //設置屬性 GroupIndicator 去掉向下箭頭
listView.setAdapter(adapterX);
for (int i = 0; i < adapterX.getGroupCount(); i++) {
listView.expandGroup(i); //關鍵步驟4:初始化,將ExpandableListView以展開的方式顯示
}
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
int top = -1;
View firstView = view.getChildAt(firstVisibleItem);
if (firstView != null) {
top = firstView.getTop();
}
if (firstVisibleItem == 0 && top == 0) {
mPtrFrame.setEnabled(true);
} else {
mPtrFrame.setEnabled(false);
}
}
});
}
@Override
public void onClick(View v) {
AlertDialog dialog;
switch (v.getId()) {
case R.id.head_right:
flag = !flag;
setVisibilityX();
break;
case R.id.all_checkBox:
doCheckAll();
break;
case R.id.ll_calculate:
GetGoodsId();
if (xc.size() == 0) {
ToastUtilsX.showShortToast(MultiShopActivity.this, "請選擇您要結算的商品!");
return;
}
//結算操作。。。。
break;
case R.id.ll_delete:
if (mtotalCount == 0) {
ToastUtilsX.showLongToast(MultiShopActivity.this, "請選擇要刪除的商品!");
return;
}
dialog = new AlertDialog.Builder(MultiShopActivity.this).create();
dialog.setMessage("確認要刪除該商品嗎?");
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
doDelete();
}
});
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
return;
}
});
dialog.show();
break;
}
}
/**
* @return 判斷組元素是否全選
*/
private boolean isCheckAll() {
hashMapGoodsId.clear();
for (StoreBean group : groups) {
if (!group.isChecked()) {
return false;
}
}
return true;
}
/**
* 刪除操作
* 1.不要邊遍歷邊刪除,容易出現數組越界的情況
* 2.把將要刪除的對象放進相應的容器中,待遍歷完,用removeAll的方式進行刪除
*/
private void doDelete() {
List<StoreBean> toBeDeleteGroups = new ArrayList<StoreBean>(); //待刪除的組元素
for (int i = 0; i < groups.size(); i++) {
StoreBean group = groups.get(i);
if (group.isChecked()) {
toBeDeleteGroups.add(group);
}
List<MultiGoodsBean> toBeDeleteChilds = new ArrayList<MultiGoodsBean>();//待刪除的子元素
List<MultiGoodsBean> child = childs.get(group.getId());
for (int j = 0; j < child.size(); j++) {
if (child.get(j).isChecked()) {
toBeDeleteChilds.add(child.get(j));
}
}
//獲取購物車ID 與Demo 沒有關係
GetGoodsId();
child.removeAll(toBeDeleteChilds);
}
groups.removeAll(toBeDeleteGroups);
//重新設置購物車
setCartNum();
adapterX.notifyDataSetChanged();
}
@Override
public void checkGroup(int groupPosition, boolean isChecked) {
hashMapGoodsId.clear();
StoreBean group = groups.get(groupPosition);
List<MultiGoodsBean> child = childs.get(group.getId());
for (int i = 0; i < child.size(); i++) {
child.get(i).setChecked(isChecked);
}
if (isCheckAll()) {
allCheckBox.setChecked(true);//全選
} else {
allCheckBox.setChecked(false);//反選
}
adapterX.notifyDataSetChanged();
calulate();
}
@Override
public void checkChild(int groupPosition, int childPosition, boolean isChecked) {
hashMapGoodsId.clear();
boolean allChildSameState = true; //判斷該組下面的所有子元素是否處於同一狀態
StoreBean group = groups.get(groupPosition);
List<MultiGoodsBean> child = childs.get(group.getId());
for (int i = 0; i < child.size(); i++) {
//不選全中
if (child.get(i).isChecked() != isChecked) {
allChildSameState = false;
break;
}
}
if (allChildSameState) {
group.setChecked(isChecked);//如果子元素狀態相同,那麼對應的組元素也設置成這一種的同一狀態
} else {
group.setChecked(false);//否則一律視爲未選中
}
if (isCheckAll()) {
allCheckBox.setChecked(true);//全選
} else {
allCheckBox.setChecked(false);//反選
}
adapterX.notifyDataSetChanged();
calulate();
}
@Override
public void doIncrease(int groupPosition, int childPosition, View showCountView, boolean isChecked) {
MultiGoodsBean good = (MultiGoodsBean) adapterX.getChild(groupPosition, childPosition);
int count = good.getNum();
count++;
good.setNum(count);
((TextView) showCountView).setText(String.valueOf(count));
adapterX.notifyDataSetChanged();
calulate();
}
@Override
public void doDecrease(int groupPosition, int childPosition, View showCountView, boolean isChecked) {
MultiGoodsBean good = (MultiGoodsBean) adapterX.getChild(groupPosition, childPosition);
int count = good.getNum();
if (count == 1) {
return;
}
count--;
good.setNum(count);
((TextView) showCountView).setText("" + count);
adapterX.notifyDataSetChanged();
calulate();
}
@Override
public void doUpdate(int groupPosition, int childPosition, View showCountView, boolean isChecked) {
MultiGoodsBean good = (MultiGoodsBean) adapterX.getChild(groupPosition, childPosition);
int count = good.getNum();
((TextView) showCountView).setText(String.valueOf(count));
adapterX.notifyDataSetChanged();
calulate();
}
@Override
public void childDelete(int groupPosition, int childPosition) {
StoreBean group = groups.get(groupPosition);
List<MultiGoodsBean> child = childs.get(group.getId());
child.remove(childPosition);
if (child.size() == 0) {
groups.remove(groupPosition);
}
adapterX.notifyDataSetChanged();
calulate();
}
/**
* 全選和反選
*/
private void doCheckAll() {
for (int i = 0; i < groups.size(); i++) {
StoreBean group = groups.get(i);
group.setChecked(allCheckBox.isChecked());
List<MultiGoodsBean> child = childs.get(group.getId());
for (int j = 0; j < child.size(); j++) {
child.get(j).setChecked(allCheckBox.isChecked());
}
}
adapterX.notifyDataSetChanged();
calulate();
}
/**
* 計算商品總價格,操作步驟
* 1.先清空全局計價,計數
* 2.遍歷所有的子元素,只要是被選中的,就進行相關的計算操作
* 3.給textView填充數據
*/
private void calulate() {
mtotalPrice = 0.00;
mtotalCount = 0;
for (int i = 0; i < groups.size(); i++) {
StoreBean group = groups.get(i);
List<MultiGoodsBean> child = childs.get(group.getId());
for (int j = 0; j < child.size(); j++) {
MultiGoodsBean good = child.get(j);
if (good.isChecked()) {
mtotalCount++;
BigDecimal b1 = new BigDecimal(good.getMoney());
BigDecimal b2 = new BigDecimal(good.getNum() + "");
mtotalPrice += b1.multiply(b2).doubleValue();
}
}
}
String resultX = String.format("%.2f", mtotalPrice);
tvPrice.setText("¥" + resultX);
if (mtotalCount == 0) {
setCartNum();
}
}
/**
* 顯示/隱藏
*/
private void setVisibilityX() {
if (flag) {
rlOrderInfo.setVisibility(View.GONE);
rlDeleteInfo.setVisibility(View.VISIBLE);
headRight.setText("完成");
} else {
rlOrderInfo.setVisibility(View.VISIBLE);
rlDeleteInfo.setVisibility(View.GONE);
headRight.setText("管理");
}
}
/**
* 設置購物車的數量
*/
private void setCartNum() {
int count = 0;
for (int i = 0; i < groups.size(); i++) {
StoreBean group = groups.get(i);
group.setChecked(allCheckBox.isChecked());
List<MultiGoodsBean> Childs = childs.get(group.getId());
for (MultiGoodsBean childs : Childs) {
count++;
}
}
//購物車已經清空
if (count == 0) {
clearCart();
} else {
headRight.setVisibility(View.VISIBLE);
empty_shopcart.setVisibility(View.GONE);//這裏發生過錯誤
llBottomView.setVisibility(View.VISIBLE);
}
}
private void clearCart() {
empty_shopcart.setVisibility(View.VISIBLE);//這裏發生過錯誤
headRight.setVisibility(View.GONE);
llBottomView.setVisibility(View.GONE);
}
@Override
public void onDestroy() {
super.onDestroy();
adapterX = null;
childs.clear();
groups.clear();
mtotalPrice = 0.0;
mtotalCount = 0;
}
//獲取購物車ID 的拼接
private void GetGoodsId() {
StringBuffer permission = new StringBuffer();
List<StoreBean> toBeDeleteGroups = new ArrayList<StoreBean>(); //待刪除的組元素
for (int i = 0; i < groups.size(); i++) {
StoreBean group = groups.get(i);
if (group.isChecked()) {
toBeDeleteGroups.add(group);
xc.add(group);
}
List<MultiGoodsBean> toBeDeleteChilds = new ArrayList<MultiGoodsBean>();//複用獲取ID
List<MultiGoodsBean> child = childs.get(group.getId());
for (int j = 0; j < child.size(); j++) {
if (child.get(j).isChecked()) {
toBeDeleteChilds.add(child.get(j));
}
}
for (int k = 0; k < toBeDeleteChilds.size(); k++) {
permission.append(toBeDeleteChilds.get(k).getId() + ",");
hashMapGoodsId.put(toBeDeleteChilds.get(k).getId(), toBeDeleteChilds.get(k).getId());
}
if (permission.toString().length() > 0) {
String strX = permission.toString();
// 拼接完成的商品購物車ID
String goodsId = strX.substring(0, strX.length() - 1);
}
}
}
多商家適配器代碼實現
public class MultiShopAdapter extends BaseExpandableListAdapter {
private List<StoreBean> groups;
//這個String對應着StoreInfo的Id,也就是店鋪的Id
private Map<String, List<MultiGoodsBean>> childrens;
private Context mcontext;
private CheckInterface checkInterface;
private ModifyCountInterface modifyCountInterface;
public MultiShopAdapter(List<StoreBean> groups, Map<String, List<MultiGoodsBean>> 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) {
if (groups.size() == 0) {
return "";
} else {
return groups.get(groupPosition);
}
}
@Override
public Object getChild(int groupPosition, int childPosition) {
if (groups.size() == 0) {
return "";
} else {
List<MultiGoodsBean> 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_shopcar_group, null);
groupViewHolder = new GroupViewHolder(convertView);
convertView.setTag(groupViewHolder);
} else {
groupViewHolder = (GroupViewHolder) convertView.getTag();
}
if (groups.size() != 0) {
final StoreBean group = (StoreBean) getGroup(groupPosition);
groupViewHolder.tvShopName.setText(group.getName());
groupViewHolder.storeCheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
group.setChecked(((CheckBox) v).isChecked());
checkInterface.checkGroup(groupPosition, ((CheckBox) v).isChecked());
}
});
groupViewHolder.storeCheckBox.setChecked(group.isChecked());
} else {
groupViewHolder.llTitle.setVisibility(View.GONE);
}
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_shopcar_product, null);
childViewHolder = new ChildViewHolder(convertView);
convertView.setTag(childViewHolder);
} else {
childViewHolder = (ChildViewHolder) convertView.getTag();
}
if (groups.size() != 0) {
final MultiGoodsBean child = (MultiGoodsBean) getChild(groupPosition, childPosition);
if (child != null) {
childViewHolder.tvGoodsName.setText(child.getName());
childViewHolder.tvMoney.setText("¥" + child.getMoney());
childViewHolder.tvNumber.setText(child.getNum() + "");
childViewHolder.singleCheckBox.setChecked(child.isChecked());
childViewHolder.singleCheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
child.setChecked(((CheckBox) v).isChecked());
childViewHolder.singleCheckBox.setChecked(((CheckBox) v).isChecked());
checkInterface.checkChild(groupPosition, childPosition, ((CheckBox) v).isChecked());
}
});
childViewHolder.ivAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
modifyCountInterface.doIncrease(groupPosition, childPosition, childViewHolder.tvNumber, childViewHolder.singleCheckBox.isChecked());
}
});
childViewHolder.ivMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
modifyCountInterface.doDecrease(groupPosition, childPosition, childViewHolder.tvNumber, childViewHolder.singleCheckBox.isChecked());
}
});
}
}else {
childViewHolder.llTitleA.setVisibility(View.GONE);
}
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
static class GroupViewHolder {
CheckBox storeCheckBox;
TextView tvDiscount;
TextView tvShopName;
LinearLayout llTitle;
public GroupViewHolder(View view) {
tvDiscount = view.findViewById(R.id.tv_discount);
tvShopName = view.findViewById(R.id.tv_shop_name);
storeCheckBox = view.findViewById(R.id.store_check_box);
llTitle = view.findViewById(R.id.ll_title);
}
}
static class ChildViewHolder {
CheckBox singleCheckBox;
RoundedImageView imageView;
TextView tvGoodsName;
TextView tvSpec;
TextView tvMoney;
ImageView ivMinus;
TextView tvNumber;
ImageView ivAdd;
LinearLayout llTitleA;
public ChildViewHolder(View view) {
singleCheckBox = view.findViewById(R.id.single_check_box);
imageView = view.findViewById(R.id.image_View);
tvGoodsName = view.findViewById(R.id.tv_goods_name);
tvSpec = view.findViewById(R.id.tv_spec);
tvMoney = view.findViewById(R.id.tv_money);
ivMinus = view.findViewById(R.id.iv_minus);
tvNumber = view.findViewById(R.id.tv_number);
ivAdd = view.findViewById(R.id.iv_add);
llTitleA = view.findViewById(R.id.ll_title_a);
}
}
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 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);
}
}
代碼拿過來就可以使用,測試多次確保無誤。
如有問題,請直接聯繫我。