购物车的适配器
来源:互联网 发布:子程序铣螺纹怎么编程 编辑:程序博客网 时间:2024/05/16 04:40
public class Erjibase extends BaseExpandableListAdapter { Context context; List<Chachebean.DataBean> listG; List<List<Chachebean.DataBean.ListBean>> listC; Handler handler; public Erjibase(Context context, List<Chachebean.DataBean> listG, List<List<Chachebean.DataBean.ListBean>> listC, Handler handler) { this.context=context; this.listC=listC; this.listG=listG; this.handler=handler; } @Override public int getGroupCount() { return listG.size(); } @Override public int getChildrenCount(int gid) { return listC.get(gid).size(); } @Override public Object getGroup(int gid) { return listG.get(gid); } @Override public Object getChild(int gid, int cid) { return listC.get(gid).get(cid); } @Override public long getGroupId(int gid) { return gid; } @Override public long getChildId(int gid, int cid) { return cid; } @Override public boolean hasStableIds() { return true; } @Override public boolean isChildSelectable(int gid, int cid) { return true; } @Override public View getGroupView(final int gid, boolean b, View view, ViewGroup viewGroup) { GroupHolder holder; if (view == null){ view = View.inflate(context, R.layout.funqin,null); holder = new GroupHolder(); holder.check_group = view.findViewById(R.id.check_group); holder.text_group = view.findViewById(R.id.text_group); view.setTag(holder); }else { holder = (GroupHolder) view.getTag(); } final Chachebean.DataBean dataBean = listG.get(gid); //赋值 holder.check_group.setChecked(dataBean.isGroupChecked()); holder.text_group.setText(dataBean.getSellerName()); //设置点击事件 holder.check_group.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //2.1改变当前一级选中的状态 dataBean.setGroupChecked(! dataBean.isGroupChecked()); //2.2根据当前一级的状态,改变该组里面二级列表的状态 changeChildState(gid,dataBean.isGroupChecked()); //2.3通过判断所有的一级组是否选中,来决定是否全选选中 changeAllState(isAllGroupChecked());//2.4发送价格个数量: sendPriceAndCount(); notifyDataSetChanged(); } }); return view; } /** * 所有的一级列表是否选中 * @return */ private boolean isAllGroupChecked() { for (int i=0;i<listG.size();i++){ if (! listG.get(i).isGroupChecked()){ return false; } } return true; } /** * 根据当前一级列表的状态,,,改变当前组中所有二级列表的状态 * @param groupPosition * @param groupChecked */ private void changeChildState(int groupPosition, boolean groupChecked) { List<Chachebean.DataBean.ListBean> listBeans = listC.get(groupPosition); for (int i=0;i<listBeans.size();i++){ listBeans.get(i).setSelected(groupChecked? 1:0); } } /** * 根据所有一级列表是否选中,确定外面的全选是否选中 * @param allGroupChecked */ private void changeAllState(boolean allGroupChecked) { Message msg = Message.obtain(); msg.what =1; msg.obj = allGroupChecked; handler.sendMessage(msg); } @Override public View getChildView(final int gid, int cid, boolean b, View view, ViewGroup viewGroup) { ChildHolder holder; if (view == null){ view = View.inflate(context, R.layout.haizi,null); holder = new ChildHolder(); holder.text_add = view.findViewById(R.id.text_add); holder.text_num = view.findViewById(R.id.text_num); holder.text_jian = view.findViewById(R.id.text_jian); holder.text_title = view.findViewById(R.id.text_title); holder.text_price = view.findViewById(R.id.text_price); holder.image_good = view.findViewById(R.id.image_good); holder.check_child = view.findViewById(R.id.check_child); view.setTag(holder); }else { holder = (ChildHolder) view.getTag(); } //赋值 //赋值 final Chachebean.DataBean.ListBean listBean = listC.get(gid).get(cid); holder.text_num.setText(listBean.getNum()+"");//......注意 holder.text_price.setText("¥"+listBean.getPrice()); holder.text_title.setText(listBean.getTitle()); //listBean.getSelected().....0false,,,1true //设置checkBox选中状态 holder.check_child.setChecked(listBean.getSelected()==0? false:true); /*implementation 'com.github.bumptech.glide:glide:4.4.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.4.0'*/ // Glide.with(context).load(listBean.getImages().split("\\|")[0]).into(holder.image_good); ImageLoader.getInstance().displayImage(listBean.getImages().split("\\|")[0],holder.image_good, Tuutils.getDefaultOption()); //设置点击事件 holder.check_child.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //3.1点击改变当前子条目状态:....实际是改变当前的数据,,,刷新适配器 listBean.setSelected(listBean.getSelected() ==0? 1:0); //3.2发送价钱和数量给界面显示 sendPriceAndCount(); //3.3判断当前子条目是否选中 if (listBean.getSelected() == 1){ //判断一下当前组中所有的子条目是否全部选中 if (isAllChildSelected(gid)){ //如果全部选中改变一下当前组的状态 changeGroupState(gid,true); //.确定是否改变全选 changeAllState(isAllGroupChecked()); } }else { //如果没有选中改变一下当前组的状态 changeGroupState(gid,false); //.确定是否改变全选 changeAllState(isAllGroupChecked()); } //刷新适配器 notifyDataSetChanged(); } }); //加号: holder.text_add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //改变当前位置 中商品的数量 listBean.setNum(listBean.getNum()+1); //判断一下是否选中...计算价格数量 if (listBean.getSelected() == 1){ sendPriceAndCount(); } // notifyDataSetChanged(); } }); //减号 holder.text_jian.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int num = listBean.getNum(); if (num == 1){ return; } listBean.setNum(num -1); //判断是否选中 if (listBean.getSelected() == 1){ sendPriceAndCount(); } notifyDataSetChanged(); } }); return view; } /** * 改变当前组的状态 * @param groupPosition * @param b */ private void changeGroupState(int groupPosition, boolean b) { listG.get(groupPosition).setGroupChecked(b); } /** * 判断当前组中所有的二级是否选中 * @param groupPosition * @return */ private boolean isAllChildSelected(int groupPosition) { List<Chachebean.DataBean.ListBean> listBeans = listC.get(groupPosition); for (int i=0;i<listBeans.size();i++){ if (listBeans.get(i).getSelected() == 0){ return false; } } return true; } /** * 计算总价和数量,,,发送显示 */ private void sendPriceAndCount() { double price = 0; int count = 0; for (int i=0;i<listG.size();i++){ List<Chachebean.DataBean.ListBean> listBeans = listG.get(i).getList(); for (int j = 0;j<listBeans.size();j++){ Chachebean.DataBean.ListBean listBean = listBeans.get(j); if (listBean.getSelected()==1){ price += listBean.getPrice()* listBean.getNum(); count += listBean.getNum(); } } } CountPriceBean countPriceBean = new CountPriceBean(price, count); //显示到activity页面 Message msg = Message.obtain(); msg.what = 0; msg.obj = countPriceBean; handler.sendMessage(msg); } /** * 设置是否全选 * @param checked */ public void setIfCheckAll(boolean checked) { for (int i = 0;i<listG.size();i++){ Chachebean.DataBean dataBean = listG.get(i); //设置组上面的checkBox是否选中 dataBean.setGroupChecked(checked); List<Chachebean.DataBean.ListBean> listBeans = dataBean.getList(); for (int j = 0; j< listBeans.size(); j++){ //改变是否选中的状态...数据应该变的是 listBeans.get(j).setSelected(checked? 1:0); } } //计算价钱和数量并且发送到mainActivity显示 sendPriceAndCount(); //刷新适配器 notifyDataSetChanged(); } private class GroupHolder{ CheckBox check_group; TextView text_group; } private class ChildHolder{ CheckBox check_child; ImageView image_good; TextView text_title; TextView text_price; TextView text_jian; TextView text_num; TextView text_add; }
}
阅读全文
0 0
- 购物车的适配器
- 仿购物车的适配器
- 二级列表的购物车-适配器
- 购物车的逻辑代码与适配器
- 网络更新购物车的适配器
- 购物车适配器代码
- Fragment 购物车适配器
- 购物车适配器
- 购物车适配器
- 购物车适配器
- 购物车二级列表适配器
- 仿京东购物车专用适配器
- 简易购物车---购物车的实现
- 购物网站的购物车设计
- 购物车实现购物的原理
- 购物车实现购物的原理
- Asp的购物车
- 购物车的实现
- Python pip install requests产生的错误
- Ubuntu 16.04下无法将pip从8.1.2升级到9.0.1的解决办法
- spring-aop简单实例
- lintcode&九章算法——64 合并排序数组 II ? 待解决
- You don't appear to have the necessary permissions to install packages
- 购物车的适配器
- 个人理解 java虚拟机中的符号引用和直接引用
- LINTCODE—— Coin Change II
- 折腾了快两天的问题
- connect-mongo
- Anaconda使用介绍
- 一个简单网页游戏--丑陋的连连看
- 风投简介及互联网/IT的风投
- org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter找不到