private Context context; private ShoppCardBean shoppCardBean; private CheckBox checkAll; private Button jiesuan; private TextView zongjia; private List<ShoppCardBean.DataBean> data; public MyExAdapter(Context context, ShoppCardBean shoppCardBean, CheckBox checkAll, TextView zongjia,Button jiesuan) { this.context = context; this.shoppCardBean = shoppCardBean; this.checkAll = checkAll; this.zongjia = zongjia; this.jiesuan = jiesuan; } @Override public int getGroupCount() { return shoppCardBean.getData().size(); } @Override public int getChildrenCount(int groupPosition) { return shoppCardBean.getData().get(groupPosition).getList().size(); } @Override public Object getGroup(int groupPosition) { return shoppCardBean.getData().get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return shoppCardBean.getData().get(groupPosition).getList().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 true; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { GroupHolder groupHolder = null; if (convertView == null) { groupHolder = new GroupHolder(); convertView= LayoutInflater.from(context).inflate(R.layout.group_layout, null); groupHolder.groupText = convertView.findViewById(R.id.group_sellerName); //绑定 convertView.setTag(groupHolder); }else{ groupHolder = (GroupHolder) convertView.getTag(); } //赋值 groupHolder.groupText.setText(shoppCardBean.getData().get(groupPosition).getSellerName()); return convertView; } @Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { ChildHolder childHolder = null; if (convertView == null) { childHolder = new ChildHolder(); convertView = LayoutInflater.from(context).inflate(R.layout.ex_child_layout, null); childHolder.child_name = convertView.findViewById(R.id.child_name); childHolder.ex_child_simple = convertView.findViewById(R.id.ex_child_simple); childHolder.child_price = convertView.findViewById(R.id.child_price); childHolder.jian = convertView.findViewById(R.id.jian); childHolder.add = convertView.findViewById(R.id.add); childHolder.child_check = convertView.findViewById(R.id.child_check); childHolder.count = convertView.findViewById(R.id.count); //绑定 convertView.setTag(childHolder); }else{ childHolder = (ChildHolder) convertView.getTag(); } //赋值 data = shoppCardBean.getData(); childHolder.child_name.setText(data.get(groupPosition).getList().get(childPosition).getTitle()); childHolder.child_price.setText("¥"+ data.get(groupPosition).getList().get(childPosition).getPrice()); childHolder.count.setText(data.get(groupPosition).getList().get(childPosition).getNum()+""); childHolder.ex_child_simple.setImageURI(data.get(groupPosition).getList().get(childPosition).getImages().split("\\|")[0]); //点击加号数量进行++ final ChildHolder finalChildHolder = childHolder; AddCount(groupPosition, childPosition, childHolder, finalChildHolder); //点击减号数量进行--,到1的时候不在进行-- final ChildHolder finalChildHolder1 = childHolder; JianCount(groupPosition, childPosition, childHolder, finalChildHolder1); //获取多选框的状态 if (data.get(groupPosition).getList().get(childPosition).getSelected()==1) { childHolder.child_check.setChecked(true); } else{ childHolder.child_check.setChecked(false); } //点击结算进行下单 jiesuan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { jiesuanCallBack.jiesuanclick(data.get(groupPosition).getList().get(childPosition).getPrice()); } }); //点击多选框进行计算总价 checkboxSums(groupPosition, childPosition, childHolder); //点击子条目进行弹框 clichitemAlert(groupPosition, childPosition, convertView); return convertView; } private void clichitemAlert(final int groupPosition, final int childPosition, View convertView) { convertView.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { final AlertDialog.Builder builder = new AlertDialog.Builder(context); View view = View.inflate(context, R.layout.alertdialog_layout, null); builder.setView(view); builder.setCancelable(true); Button quxiao = view.findViewById(R.id.quxiao); Button queding = view.findViewById(R.id.queding); final AlertDialog dialog = builder.create(); dialog.show(); quxiao.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); queding.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { childItemLongClickCallBack.itemclick(data.get(groupPosition).getList().get(childPosition).getPid()); dialog.dismiss(); } }); return false; } }); } public void Allcheck() { double sums = 0; for (int i = 0; i < data.size(); i++) { List<ShoppCardBean.DataBean.ListBean> list = data.get(i).getList(); for (int j = 0; j < list.size(); j++) { ShoppCardBean.DataBean.ListBean listBean = list.get(j); sums += list.get(j).getNum() * list.get(j).getPrice(); } } zongjia.setText("总价:¥"+sums); priceAllCallBack.itemAllPrice(sums); } private void checkboxSums(final int groupPosition, final int childPosition, final ChildHolder childHolder) { childHolder.child_check.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { double sums = 0; int flag = 0; int itemcount=0; //首先获取到多选框的状态 int selected = data.get(groupPosition).getList().get(childPosition).getSelected(); if (selected == 0) { data.get(groupPosition).getList().get(childPosition).setSelected(1); }else{ data.get(groupPosition).getList().get(childPosition).setSelected(0); } for (int i = 0; i < data.size(); i++) {//循环父类 List<ShoppCardBean.DataBean.ListBean> list = data.get(i).getList(); //循环子类 for (int j = 0; j < list.size(); j++) { ShoppCardBean.DataBean.ListBean listBean = list.get(j); if (listBean.getSelected() == 1) { sums += listBean.getNum() * listBean.getPrice(); flag++; } itemcount++; } } if (flag ==itemcount) { checkAll.setChecked(true); }else{ checkAll.setChecked(false); } //赋值 zongjia.setText("总价:¥"+sums); } }); } //点击减号数量进行--,到1的时候不在进行-- private void JianCount(final int groupPosition, final int childPosition, final ChildHolder childHolder, final ChildHolder finalChildHolder1) { childHolder.jian.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { double sums = 0; if (data.get(groupPosition).getList().get(childPosition).getNum() == 1) { childHolder.child_check.setChecked(false); Toast.makeText(context,"亲,最后一件商品了,不能再减了",Toast.LENGTH_LONG).show(); }else{ data.get(groupPosition).getList().get(childPosition).setNum(data.get(groupPosition).getList().get(childPosition).getNum() - 1); finalChildHolder1.count.setText(data.get(groupPosition).getList().get(childPosition).getNum()+""); } //点击减价格随之改变 for (int i = 0; i < data.size(); i++) {//循环父类 List<ShoppCardBean.DataBean.ListBean> list = data.get(i).getList(); for (int j = 0; j < list.size(); j++) { ShoppCardBean.DataBean.ListBean listBean = list.get(j); if (listBean.getSelected() == 1) { sums += listBean.getNum() * listBean.getPrice(); } } } zongjia.setText("总价:¥"+sums); } }); } //点击加号数量进行++ private void AddCount(final int groupPosition, final int childPosition, final ChildHolder childHolder, final ChildHolder finalChildHolder) { childHolder.add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { double sums = 0; //给bean类里面的数量set值 data.get(groupPosition).getList().get(childPosition).setNum(data.get(groupPosition).getList().get(childPosition).getNum()+1); Allcheck(); notifyDataSetChanged(); if (data.get(groupPosition).getList().get(childPosition).getNum() > 1) { childHolder.child_check.setChecked(true); } //给count控件赋值 finalChildHolder.count.setText(data.get(groupPosition).getList().get(childPosition).getNum()+""); //点击减价格随之改变 for (int i = 0; i < data.size(); i++) {//循环父类 List<ShoppCardBean.DataBean.ListBean> list = data.get(i).getList(); //循环子类 for (int j = 0; j < list.size(); j++) { ShoppCardBean.DataBean.ListBean listBean = list.get(j); if (listBean.getSelected() == 1) { sums += listBean.getNum() * listBean.getPrice(); } } } zongjia.setText("总价:¥"+sums); } }); } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } class GroupHolder{ TextView groupText; } class ChildHolder{ CheckBox child_check; SimpleDraweeView ex_child_simple; TextView child_name,child_price,count,jian,add; } //定义接口 public interface ChildItemLongClickCallBack{ void itemclick(int pid); } //定义价格接口 public interface JiesuanCallBack{ void jiesuanclick(double price); } private JiesuanCallBack jiesuanCallBack; public void setJiesuanCallBack(JiesuanCallBack jiesuanCallBack) { this.jiesuanCallBack = jiesuanCallBack; } //定义计算总价接口 public interface PriceAll{ void itemAllPrice(double nums); } private PriceAll priceAllCallBack; public void setPriceAllCallBack(PriceAll priceAllCallBack) { this.priceAllCallBack = priceAllCallBack; } private ChildItemLongClickCallBack childItemLongClickCallBack; public void setChildItemLongClickCallBack(ChildItemLongClickCallBack childItemLongClickCallBack) { this.childItemLongClickCallBack = childItemLongClickCallBack; }
购物车
最新推荐文章于 2021-10-09 23:39:18 发布