二级列表的购物车-适配器
来源:互联网 发布:mac通讯录管理软件 编辑:程序博客网 时间:2024/05/19 02:02
package com.example.recycler.adapter;import android.content.Context;import android.view.View;import android.view.ViewGroup;import android.widget.BaseExpandableListAdapter;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.ImageView;import android.widget.TextView;import com.bumptech.glide.Glide;import com.example.recycler.Activity_show;import com.example.recycler.AddAndDownview;import com.example.recycler.R;import com.example.recycler.bean.childbean;import com.example.recycler.bean.fatherbean;import java.util.List;public class ExpandAdapter extends BaseExpandableListAdapter{ private Context ct; private List<fatherbean> list; private List<List<childbean>> list1; private Activity_show ct1; private float sum; public ExpandAdapter(Context ct, List<fatherbean> list, List<List<childbean>> list1) { this.ct = ct; this.list = list; this.list1 = list1; } @Override public int getGroupCount() { return list.size(); } @Override public int getChildrenCount(int i) { return list1.get(i).size(); } @Override public Object getGroup(int i) { return list.get(i); } @Override public Object getChild(int i, int i1) { return list1.get(i).get(i1); } @Override public long getGroupId(int i) { return i; } @Override public long getChildId(int i, int i1) { return i1; } @Override public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) { ct1 = (Activity_show) this.ct; final fatherbean bean = list.get(i); final List<childbean> childbeen = list1.get(i); view = View.inflate(this.ct, R.layout.fatherview, null); TextView textView = (TextView) view.findViewById(R.id.text_f); final CheckBox checkBox = (CheckBox) view.findViewById(R.id.cb_father); textView.setText(bean.getName()); checkBox.setChecked(bean.isFlag()); //复选框点击事件 checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { boolean checked = checkBox.isChecked(); if (checked==false){ ct1.cb.setChecked(checked); } bean.setFlag(checked); for (int j=0;j<childbeen.size();j++){ childbeen.get(j).setFlag(checked); } notifyDataSetChanged(); boolean bool=true; for (int j =0;j<list.size();j++){ if (list.get(j).isFlag()==false){ bool=false; break; } } ct1.cb.setChecked(bool); } }); checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { boolean checked = checkBox.isChecked(); List<childbean> beanList = list1.get(i); for (int j=0;j<beanList.size();j++){ childbean child = beanList.get(j); int count = child.getCount(); float price = child.getPrice(); if(checked){ sum+=price*count; }else{ sum-=price*count; } } ct1.num.setText("总价为:"+sum); } }); return view; } @Override public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) { childbean bean = list1.get(i).get(i1); //加载布局 view= View.inflate(ct, R.layout.childview,null); //获取构件 ImageView img = (ImageView) view.findViewById(R.id.img_child); TextView title = (TextView) view.findViewById(R.id.child_title); final TextView price = (TextView) view.findViewById(R.id.child_price); final AddAndDownview add = (AddAndDownview) view.findViewById(R.id.aadview); final CheckBox checkBox = (CheckBox) view.findViewById(R.id.cb_child); //修改内容 price.setText(bean.getPrice()+""); title.setText(bean.getTitle()); checkBox.setChecked(list1.get(i).get(i1).isFlag()); Glide.with(ct).load(bean.getImgurl()).into(img); add.setCount(list1.get(i).get(i1).getCount()); //自定义构件设置监听 add.setOnAddDeleteClick(new AddAndDownview.OnAddDeleteClickListener() { @Override public void onAddClick(View v) { int count = list1.get(i).get(i1).getCount(); list1.get(i).get(i1).setCount(count+1); notifyDataSetChanged(); } @Override public void onDelClick(View v) { int count = list1.get(i).get(i1).getCount(); if(count!=0){ list1.get(i).get(i1).setCount(count-1); notifyDataSetChanged(); } } }); checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { boolean checked = checkBox.isChecked(); if (checked==false){ ct1.cb.setChecked(checked); } list1.get(i).get(i1).setFlag(checked); boolean b=true; if (checked){ for (int j=0;j<list1.get(i).size();j++){ boolean flag = list1.get(i).get(j).isFlag(); if (flag==false){ b=false; } } //修改父级的选中状态 if (b){ list.get(i).setFlag(true); } //判断全选按钮 boolean bool=true; for (int j =0;j<list1.size();j++){ for (int k=0;k<list1.get(j).size();k++){ if (list1.get(j).get(k).isFlag()==false){ bool=false; break; } } } ct1.cb.setChecked(bool); }else{ list.get(i).setFlag(false); } notifyDataSetChanged(); } }); checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { boolean checked = checkBox.isChecked(); float price1 = list1.get(i).get(i1).getPrice(); int count = add.getCount(); if(checked) { sum=sum+price1*count; }else{ sum=sum-price1*count; } ct1.num.setText("总价为:"+sum); } }); return view; } @Override public boolean hasStableIds() { return true; } @Override public boolean isChildSelectable(int i, int i1) { return true; }}
阅读全文
0 0
- 二级列表的购物车-适配器
- 购物车二级列表适配器
- 二级列表购物车
- 购物车二级列表
- 购物车(二级列表)
- 二级列表购物车
- 购物车二级列表
- 购物车二级列表
- 二级列表购物车
- 二级列表购物车
- 购物车二级列表
- 二级列表购物车
- 二级列表购物车
- 二级列表 购物车
- 购物车二级列表
- 二级列表购物车++
- 购物车二级列表
- 二级列表购物车
- Java基础——Java NIO详解(一)
- 我只是想要8个积分 为什么那么难
- C语言enum的用法
- Android----MVC、MVP、MVVM、区别介绍
- 磁盘IO的概念
- 二级列表的购物车-适配器
- java生成图片验证码返回base64图片信息
- DispatcherServlet
- UVA 202
- 2017.11.20心得
- tomcat启动报错
- 数据库root权限恢复
- 二分法查找
- linux系统wifi被禁用的问题