EventBus与适配器
来源:互联网 发布:js获取传过来的参数 编辑:程序博客网 时间:2024/05/18 21:07
EventBus导入依赖:compile 'org.greenrobot:eventbus:3.0.0'
//注册EventBus
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_shopping, null); //注册EventBus EventBus.getDefault().register(this); initView(); return view;}//订阅方法,当接收到事件的时候,会调用该方法//获取价格@Subscribe(threadMode = ThreadMode.MAIN)public void onEvent(MoneyAndCount moneyAndCount) { //double类型保留2位小数点 DecimalFormat df = new DecimalFormat("######0.00"); contprice.setText("总价:" + df.format(moneyAndCount.getPrice())); jiesuan.setText("结算(" + moneyAndCount.getCount() + ")");}
//解除注册
@Overridepublic void onDestroy() { super.onDestroy(); //解除注册 EventBus.getDefault().unregister(this);}二级列表适配器:private List<CheckShoppingCardBean.DataBean> grouplist;private List<List<CheckShoppingCardBean.DataBean.ListBean>> childlist;private Context context;private double money;private int counts;public MExpandlbAdapter(List<CheckShoppingCardBean.DataBean> grouplist, List<List<CheckShoppingCardBean.DataBean.ListBean>> childlist, Context context) { this.grouplist = grouplist; this.childlist = childlist; this.context = context;}@Overridepublic int getGroupCount() { return grouplist.size();}@Overridepublic int getChildrenCount(int i) { return childlist.get(i).size();}@Overridepublic Object getGroup(int i) { return grouplist.get(i);}@Overridepublic Object getChild(int i, int i1) { return childlist.get(i1);}@Overridepublic long getGroupId(int i) { return i;}@Overridepublic long getChildId(int i, int i1) { return i1;}@Overridepublic boolean hasStableIds() { return false;}@Overridepublic View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) { View view1; final V1 v1; if (view == null) { v1 = new V1(); view1 = View.inflate(context, R.layout.cardshopsitem, null); v1.groupchecked = view1.findViewById(R.id.grouchecked); view1.setTag(v1); } else { view1 = view; v1 = (V1) view1.getTag(); } v1.groupchecked.setChecked(grouplist.get(i).isGroupChecked()); v1.groupchecked.setText(grouplist.get(i).getSellerName()); //父列表点击选中,然后选中所有子列表 v1.groupchecked.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (v1.groupchecked.isChecked()) { grouplist.get(i).setGroupChecked(v1.groupchecked.isChecked()); isGroupAllChild(v1.groupchecked.isChecked(), i); //父列表点击全选中,然后全选 boolean all = isAll(); Log.e("All", all + ""); if (all) { Toast.makeText(context, "全选", Toast.LENGTH_SHORT).show(); EventBus.getDefault().post(new All(v1.groupchecked.isChecked())); } //计算价格总和,和数量总和 allPriceAndCount(); } else { grouplist.get(i).setGroupChecked(v1.groupchecked.isChecked()); isGroupAllChild(v1.groupchecked.isChecked(), i); EventBus.getDefault().post(new All(v1.groupchecked.isChecked())); //计算价格总和,和数量总和 allPriceAndCount(); } } }); return view1;}@Overridepublic View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) { View view1; final V2 v2; if (view == null) { v2 = new V2(); view1 = View.inflate(context, R.layout.goodsitem, null); v2.childchecked = view1.findViewById(R.id.childchecked); v2.goodsimg = view1.findViewById(R.id.goodsimg); v2.titles = view1.findViewById(R.id.titles); v2.price = view1.findViewById(R.id.price); v2.numjiajia = view1.findViewById(R.id.numjiajia); v2.numjianjian = view1.findViewById(R.id.numjianjian); v2.num = view1.findViewById(R.id.num); view1.setTag(v2); } else { view1 = view; v2 = (V2) view1.getTag(); } ImageLoader instance = ImageLoader.getInstance(); v2.childchecked.setChecked(childlist.get(i).get(i1).ischildckecked()); instance.displayImage(childlist.get(i).get(i1).getImages().split("\\|")[0], v2.goodsimg); v2.titles.setText(childlist.get(i).get(i1).getTitle()); v2.price.setText(childlist.get(i).get(i1).getPrice() + ""); v2.num.setText(childlist.get(i).get(i1).getNum() + ""); //数量加加减减监听 v2.numjiajia.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int num = childlist.get(i).get(i1).getNum(); if (num < 9) { childlist.get(i).get(i1).setNum(num + 1); notifyDataSetChanged(); //计算价格总和,和数量总和 allPriceAndCount(); } else { Toast.makeText(context, "数量以到达商品库存上线", Toast.LENGTH_SHORT).show(); } } }); v2.numjianjian.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int num = childlist.get(i).get(i1).getNum(); if (num > 1) { childlist.get(i).get(i1).setNum(num - 1); notifyDataSetChanged(); //计算价格总和,和数量总和 allPriceAndCount(); } else { Toast.makeText(context, "商品数量已经是1了", Toast.LENGTH_SHORT).show(); } } }); //子列表点击选中,判断子列表是否都选中,然后选中父列表 v2.childchecked.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (v2.childchecked.isChecked()) { childlist.get(i).get(i1).setIschildckecked(v2.childchecked.isChecked()); boolean childChecked = isChildChecked(i); if (childChecked) { grouplist.get(i).setGroupChecked(v2.childchecked.isChecked()); } //刷新适配器 notifyDataSetChanged(); boolean all = isAll(); Log.e("All", all + ""); if (all) { Toast.makeText(context, "全选", Toast.LENGTH_SHORT).show(); EventBus.getDefault().post(new All(v2.childchecked.isChecked())); } //计算价格总和,和数量总和 allPriceAndCount(); } else { childlist.get(i).get(i1).setIschildckecked(v2.childchecked.isChecked()); grouplist.get(i).setGroupChecked(v2.childchecked.isChecked()); //刷新适配器 notifyDataSetChanged(); EventBus.getDefault().post(new All(v2.childchecked.isChecked())); //计算价格总和,和数量总和 allPriceAndCount(); } } }); return view1;}@Overridepublic boolean isChildSelectable(int i, int i1) { return true;}//优化类class V1 { CheckBox groupchecked;}class V2 { CheckBox childchecked; ImageView goodsimg; TextView titles; TextView price; TextView numjianjian; TextView num; TextView numjiajia;}/*** * 子列表点击选中,判断子列表是否都选中,然后选中父列表 */public boolean isChildChecked(int positon) { List<CheckShoppingCardBean.DataBean.ListBean> listBeen = childlist.get(positon); for (int i = 0; i < listBeen.size(); i++) { if (!listBeen.get(i).ischildckecked()) { return false; } } return true;}/*** * 父列表点击选中,然后选中所有子列表 */public void isGroupAllChild(boolean b, int positon) { List<CheckShoppingCardBean.DataBean.ListBean> listBeen = childlist.get(positon); if (b) { for (int i = 0; i < listBeen.size(); i++) { listBeen.get(i).setIschildckecked(b); } } else { for (int i = 0; i < listBeen.size(); i++) { listBeen.get(i).setIschildckecked(b); } } notifyDataSetChanged();}/*** * 父列表点击全选中,然后全选 */public boolean isAll() { for (int i = 0; i < grouplist.size(); i++) { if (!grouplist.get(i).isGroupChecked()) { return false; } } return true;}/*** * 点击全选父列表,子列表全选 */public void setAll(boolean b) { for (int i = 0; i < grouplist.size(); i++) { grouplist.get(i).setGroupChecked(b); } for (int i = 0; i < childlist.size(); i++) { List<CheckShoppingCardBean.DataBean.ListBean> listBeen = childlist.get(i); for (int j = 0; j < listBeen.size(); j++) { listBeen.get(j).setIschildckecked(b); } } //刷新适配器 notifyDataSetChanged(); //计算价格总和,和数量总和 allPriceAndCount();}/*** * 计算价格总和,和数量总和 */public void allPriceAndCount() { money = 0; counts = 0; for (int i = 0; i < childlist.size(); i++) { List<CheckShoppingCardBean.DataBean.ListBean> listBeen = childlist.get(i); for (int j = 0; j < listBeen.size(); j++) { if (listBeen.get(j).ischildckecked()) { money += listBeen.get(j).getPrice() * listBeen.get(j).getNum(); counts += listBeen.get(j).getNum(); } } } //发送价格数量进行显示 EventBus.getDefault().post(new MoneyAndCount(money, counts));}
阅读全文
0 0
- EventBus与适配器
- EventBus使用与解析
- EventBus使用与分析
- EventBus学习与进阶
- EventBus介绍与使用
- otto与EventBus对比
- 对象适配器与类适配器
- 观察者模式与Guava EventBus
- EventBus的学习与使用
- EventBus 利弊与源码解析
- Otto与EventBus框架比较
- androidEventBus 与 greenrobot的EventBus
- EventBus源码学习与探究
- EventBus使用与源码解析
- android handler与eventbus对比
- EventBus的优点与使用
- EventBus的使用与原理
- 观察者模式与Guava EventBus
- 查看Openstack版本信息
- 设计模式知识连载(25)---享元模式:
- (收集)一些好用的漏洞库网站:含工控安全
- 数据结构实验之查找六:顺序查找
- node.js http客户端
- EventBus与适配器
- 辩证思维解答转载内容与seo的关系
- git push
- 一个Exception catch不住的"异常"
- 如何避免与公司名称混淆
- 详细解读Jquery各Ajax函数:$.get(),$.post(),$.ajax(),$.getJSON()
- json to dict
- Spring mvc通过Spring security进行密码验证加盐
- Tomcat配置任意目录下的web应用