一朋友管我要个单商家的购物车实现功能,我说这个东西不很简单嘛,他说你干了这么久了连一个购物车的模板都没有吗?他这句引起了我的反思,购物车用的时候很少,基本每次都是现写,这样确实花费了不少时间,如果有一个现成的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);
}
}
代码拿过来就可以使用,测试多次确保无误。
[希望这篇文章可以帮到你]
如有问题,请直接联系我。
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/2821.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...