private Context context; private List<ShoopingCard.DataBean> grouplist; private List<List<ShoopingCard.DataBean.ListBean>> childelist; private double price = 0; public Madapter(Context context, List<ShoopingCard.DataBean> grouplist, List<List<ShoopingCard.DataBean.ListBean>> childelist) { this.context = context; this.grouplist = grouplist; this.childelist = childelist; } @Override public int getGroupCount() { return grouplist.size(); } @Override public int getChildrenCount(int i) { return childelist.get(i).size(); } @Override public Object getGroup(int i) { return grouplist.get(i); } @Override public Object getChild(int i, int i1) { return childelist.get(i).get(i1); } @Override public long getGroupId(int i) { return i; } @Override public long getChildId(int i, int i1) { return i1; } @Override public boolean hasStableIds() { return true; } @Override public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) { View groupitem; final ViewHolder1 viewHolder1; if (view == null) { viewHolder1 = new ViewHolder1(); groupitem = View.inflate(context, R.layout.group, null); viewHolder1.groupchecked = groupitem.findViewById(R.id.groupsataus); groupitem.setTag(viewHolder1); } else { groupitem = view; viewHolder1 = (ViewHolder1) groupitem.getTag(); } viewHolder1.groupchecked.setChecked(grouplist.get(i).isgroupchecked()); viewHolder1.groupchecked.setText(grouplist.get(i).getSellerName()); //父项选中,选中全部子项/反选 viewHolder1.groupchecked.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (viewHolder1.groupchecked.isChecked()) { grouplist.get(i).setIsgroupchecked(viewHolder1.groupchecked.isChecked()); isAllChild(i, viewHolder1.groupchecked.isChecked()); //判断所有父项是否选中全选中,则选中全选/反选 boolean all = isAll(); if (all) { EventBus.getDefault().post(new Status(all)); } //计算价格 priceAndCount(); } else { grouplist.get(i).setIsgroupchecked(viewHolder1.groupchecked.isChecked()); isAllChild(i, viewHolder1.groupchecked.isChecked()); boolean all = isAll(); if (!all) { EventBus.getDefault().post(new Status(all)); } //计算价格 priceAndCount(); } } }); return groupitem; } @Override public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) { View childitem; final ViewHolder2 viewHolder2; if (view == null) { viewHolder2 = new ViewHolder2(); childitem = View.inflate(context, R.layout.child, null); viewHolder2.childchecked = childitem.findViewById(R.id.childsataus); viewHolder2.img = childitem.findViewById(R.id.img); viewHolder2.tiltess = childitem.findViewById(R.id.tielss); viewHolder2.miaoshu = childitem.findViewById(R.id.miaoshu); viewHolder2.newprice = childitem.findViewById(R.id.newprice); viewHolder2.oldprice = childitem.findViewById(R.id.oldprice); viewHolder2.jianjian = childitem.findViewById(R.id.jianjian); viewHolder2.num = childitem.findViewById(R.id.num); viewHolder2.jiajia = childitem.findViewById(R.id.jiajia); childitem.setTag(viewHolder2); } else { childitem = view; viewHolder2 = (ViewHolder2) childitem.getTag(); } viewHolder2.childchecked.setChecked(childelist.get(i).get(i1).ischildchecked()); viewHolder2.img.setImageURI(Uri.parse(childelist.get(i).get(i1).getImages().split("!")[0])); viewHolder2.tiltess.setText(childelist.get(i).get(i1).getTitle()); viewHolder2.miaoshu.setText(childelist.get(i).get(i1).getSubhead()); viewHolder2.newprice.setText("¥" + childelist.get(i).get(i1).getBargainPrice() + ""); //加下划线 viewHolder2.oldprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); viewHolder2.oldprice.setText("¥" + childelist.get(i).get(i1).getPrice()); viewHolder2.num.setText(childelist.get(i).get(i1).getNum() + ""); //事件加加监听 viewHolder2.jiajia.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int num = childelist.get(i).get(i1).getNum(); childelist.get(i).get(i1).setNum(num += 1); //刷新适配器 notifyDataSetChanged(); //计算价格 priceAndCount(); } }); //事件减减监听 viewHolder2.jianjian.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int num = childelist.get(i).get(i1).getNum(); if (num == 1) { Toast.makeText(context, "最少数量为1", Toast.LENGTH_SHORT).show(); } else { childelist.get(i).get(i1).setNum(num -= 1); //刷新适配器 notifyDataSetChanged(); //计算价格 priceAndCount(); } } }); //子选项状态 viewHolder2.childchecked.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (viewHolder2.childchecked.isChecked()) { //状态值设为true childelist.get(i).get(i1).setIschildchecked(viewHolder2.childchecked.isChecked()); //遍历同级选项如果都选中选中父项 boolean isallchildchecked = isallchildchecked(i); if (isallchildchecked) { grouplist.get(i).setIsgroupchecked(viewHolder2.childchecked.isChecked()); } notifyDataSetChanged(); boolean all = isAll(); if (all) { EventBus.getDefault().post(new Status(all)); } //计算价格 priceAndCount(); } else { childelist.get(i).get(i1).setIschildchecked(viewHolder2.childchecked.isChecked()); boolean isallchildchecked = isallchildchecked(i); if (!isallchildchecked) { grouplist.get(i).setIsgroupchecked(viewHolder2.childchecked.isChecked()); } notifyDataSetChanged(); boolean all = isAll(); if (!all) { EventBus.getDefault().post(new Status(all)); } //计算价格 priceAndCount(); } } }); return childitem; } @Override public boolean isChildSelectable(int i, int i1) { return false; } //优化类 class ViewHolder1 { CheckBox groupchecked; } class ViewHolder2 { CheckBox childchecked; SimpleDraweeView img; TextView tiltess; TextView miaoshu; TextView newprice; TextView oldprice; TextView jianjian; TextView num; TextView jiajia; } //遍历同级选项如果都选中选中父项 public boolean isallchildchecked(int i) { List<ShoopingCard.DataBean.ListBean> listBeen = childelist.get(i); for (int j = 0; j < listBeen.size(); j++) { if (!listBeen.get(j).ischildchecked()) { return false; } } return true; } // //父项选中,选中全部子项/反选 public void isAllChild(int postion, boolean b) { List<ShoopingCard.DataBean.ListBean> listBeen = childelist.get(postion); for (int i = 0; i < listBeen.size(); i++) { listBeen.get(i).setIschildchecked(b); } //刷新适配器 notifyDataSetChanged(); } //判断所有父项是否选中全选中,则选中全选/反选 public boolean isAll() { for (int i = 0; i < grouplist.size(); i++) { if (!grouplist.get(i).isgroupchecked()) { return false; } } return true; } //全选/反选 public void all(boolean b) { for (int i = 0; i < grouplist.size(); i++) { grouplist.get(i).setIsgroupchecked(b); } for (int i = 0; i < childelist.size(); i++) { for (int j = 0; j < childelist.get(i).size(); j++) { childelist.get(i).get(j).setIschildchecked(b); } } //刷新适配器 notifyDataSetChanged(); //计算价格 priceAndCount(); } //计算价格 public void priceAndCount() { price = 0; for (int i = 0; i < childelist.size(); i++) { List<ShoopingCard.DataBean.ListBean> listBeen = childelist.get(i); for (int j = 0; j < listBeen.size(); j++) { if (listBeen.get(j).ischildchecked()) { price += listBeen.get(j).getNum() * listBeen.get(j).getBargainPrice(); } } } //发送总价钱 EventBus.getDefault().post(new Money(price)); } //点击删除判断判断是否有选中的商品 public boolean isdel() { for (int i = 0; i < childelist.size(); i++) { List<ShoopingCard.DataBean.ListBean> listBeen = childelist.get(i); for (int j = 0; j < listBeen.size(); j++) { if (listBeen.get(j).ischildchecked()) { return true; } } } return false; } //返回选择的商品id public void del() { for (int i = 0; i < childelist.size(); i++) { List<ShoopingCard.DataBean.ListBean> listBeen = childelist.get(i); for (int j = 0; j < listBeen.size(); j++) { if (listBeen.get(j).ischildchecked()) { listBeen.get(j).setIschildchecked(false); EventBus.getDefault().post(new GoodsId(listBeen.get(j).getPid())); //计算价格 priceAndCount(); } } } }
购物车逻辑重构
最新推荐文章于 2023-08-19 14:30:50 发布