MainActivity:import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.ExpandableListView; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import bawei.com.gouwuche.R; import bawei.com.gouwuche.adapter.ExpanListAdapter; import bawei.com.gouwuche.bean.BeanDeleteCart; import bawei.com.gouwuche.bean.BeanQueryCart; import bawei.com.gouwuche.presenter.Presenter; import de.greenrobot.event.EventBus; import de.greenrobot.event.Subscribe; import de.greenrobot.event.ThreadMode; public class MainActivity extends AppCompatActivity implements ViewInterface.ViewQueryCart, View.OnClickListener, ViewInterface.ViewDeleteCart { List<BeanQueryCart.DataBean> list = new ArrayList<>(); private ExpandableListView edv; private ExpanListAdapter expanListAdapter; private CheckBox box; private TextView priceTV; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //注册事件 EventBus.getDefault().register(this); initView(); } /* * 初始化 * */ private void initView() { Presenter presenter = new Presenter((ViewInterface.ViewQueryCart) this); presenter.presenQueryCart("10794"); //找到控件 edv = findViewById(R.id.main_ebv); box = findViewById(R.id.main_box); priceTV = findViewById(R.id.main_price); Button shan = findViewById(R.id.main_shan); //实例化适配器 expanListAdapter = new ExpanListAdapter(this, list); //去掉箭头 edv.setGroupIndicator(null); //点击事件 box.setOnClickListener(this); shan.setOnClickListener(this); } /* * 重写查询购物车方法 * */ @Override public void sueeccQueryCart(BeanQueryCart queryCart) { if (queryCart == null) { Toast.makeText(this, "购物车是空的", Toast.LENGTH_SHORT).show(); return; } String code = queryCart.getCode(); if (code.equals("0")) { List<BeanQueryCart.DataBean> data = queryCart.getData(); list.addAll(data); edv.setAdapter(expanListAdapter); //默认展开 for (int i = 0; i < expanListAdapter.getGroupCount(); i++) { edv.expandGroup(i); } } } @Override public void errorQueryCart(String err) { Log.d("哈哈", "errorQueryCart: " + err); } /* * 重写删除购物车的方法 * */ @Override public void sueeccDeleteCart(BeanDeleteCart deleteCart) { String code = deleteCart.getCode(); if (code.equals("0")) { //清空集合 list.clear(); //删除时再次查询购物车 Presenter presenter1 = new Presenter((ViewInterface.ViewQueryCart) this);//查询 presenter1.presenQueryCart("10794"); //删除时调用计算方法 jiSuan(); } } @Override public void errorDeleteCart(String err) { Log.d("哈哈", "errorDeleteCart: "+err); } /* *点击事件 * */ @Override public void onClick(View view) { switch (view.getId()) { case R.id.main_box: //获取购物车底部复选框的状态 boolean checked = box.isChecked(); //遍历父复选框 for (BeanQueryCart.DataBean dataBean : list) { dataBean.setBox(checked); //遍历子复选框 for (BeanQueryCart.DataBean.ListBean listBean : dataBean.getList()) { listBean.setBoxChild(checked); } } //调用计算的方法 jiSuan(); //刷新适配器 expanListAdapter.notifyDataSetChanged(); break; case R.id.main_shan: Presenter presenter = new Presenter((ViewInterface.ViewDeleteCart) this); Map<String, String> map = new HashMap<>(); map.put("uid", "10794"); for (BeanQueryCart.DataBean dataBean : list) { for (BeanQueryCart.DataBean.ListBean listBean : dataBean.getList()) { boolean boxChild = listBean.getBoxChild(); if (boxChild) { String pid = listBean.getPid(); map.put("pid", pid); } } } presenter.presenDeleteCart(map); if (list.size()==1){ list.clear(); } expanListAdapter.notifyDataSetChanged(); break; } } /* * 处理事件 * Subscribe 1:在主线程里 2:接收传递过来的值 * */ @Subscribe(threadMode = ThreadMode.MainThread, sticky = true) public void eventBus(String user) { boolean flg = true; if (user.equals("哈哈")) { Log.d("哈哈", "eventBus: " + "aaaa"); //遍历父复选框 for (BeanQueryCart.DataBean dataBean : list) { boolean box = dataBean.getBox(); dataBean.setBox(box); if (!box) { flg = false; } //遍历子复选框 for (BeanQueryCart.DataBean.ListBean listBean : dataBean.getList()) { boolean boxChild = listBean.getBoxChild(); listBean.setBoxChild(boxChild); if (!boxChild) { flg = false; } } } //给购物车底部CheckBox赋值 box.setChecked(flg); //调用计算的方法 jiSuan(); //刷新适配器 expanListAdapter.notifyDataSetChanged(); } } /* * 计算的方法 * */ public void jiSuan() { double priceCount = 0; //遍历父复选框 for (BeanQueryCart.DataBean dataBean : list) { //获取子复选框 List<BeanQueryCart.DataBean.ListBean> listBeans = dataBean.getList(); //遍历子复选框 for (BeanQueryCart.DataBean.ListBean listBean : listBeans) { //获取子复选框 boolean boxChild = listBean.getBoxChild(); if (boxChild) { //获取价格 String price = listBean.getPrice(); //获取数量 String num = listBean.getNum(); //转换 Double aDouble = Double.valueOf(price); Integer integer = Integer.valueOf(num); priceCount += aDouble * integer; } } } priceTV.setText(priceCount + ""); } @Override protected void onDestroy() { super.onDestroy(); //解除注册 EventBus.getDefault().unregister(this); } }
适配器:import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.TextView; import com.bumptech.glide.Glide; import java.util.List; import bawei.com.gouwuche.R; import bawei.com.gouwuche.bean.BeanQueryCart; import de.greenrobot.event.EventBus; /** * Created by Think on 2018/5/3. */ public class ExpanListAdapter extends BaseExpandableListAdapter { private final Context context; private final List<BeanQueryCart.DataBean> list; public ExpanListAdapter(Context context, List<BeanQueryCart.DataBean> list) { this.context = context; this.list = list; } //返回父的长度 @Override public int getGroupCount() { return list.size(); } //返回子的长度 @Override public int getChildrenCount(int i) { return list.get(i).getList().size(); } //返回每个父的item @Override public Object getGroup(int i) { return list.get(i); } //返回每个子的item @Override public Object getChild(int i, int i1) { return list.get(i).getList().get(i1); } //返回每个父的id @Override public long getGroupId(int i) { return i; } //返回每个字的id @Override public long getChildId(int i, int i1) { return i1; } @Override public boolean hasStableIds() { return false; } //返回父的视图 @Override public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) { FuViewHodel fu = null; if (view == null) { //找到布局文件 view = View.inflate(context, R.layout.adapterfu, null); //找到控件 TextView tv = view.findViewById(R.id.adapterfu_tv); CheckBox box = view.findViewById(R.id.adapterfu_box); //把控件放入到子的优化类中 fu = new FuViewHodel(tv, box); //放到容器中 view.setTag(fu); } else { fu = (FuViewHodel) view.getTag(); } //赋值 fu.tv.setText(list.get(i).getSellerName()); fu.box.setChecked(list.get(i).getBox()); //点击事件 final FuViewHodel finalFu = fu; fu.box.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //获取当前点击CheckBox的状态 boolean checked = finalFu.box.isChecked(); //给JAVABean对象里的复选框赋值 list.get(i).setBox(checked); //找到子的复选框 List<BeanQueryCart.DataBean.ListBean> listBeans = ExpanListAdapter.this.list.get(i).getList(); //遍历子复选框 for (BeanQueryCart.DataBean.ListBean listBean : listBeans) { listBean.setBoxChild(checked); } String aa="哈哈"; EventBus.getDefault().post(aa); //刷新适配器 notifyDataSetChanged(); } }); return view; } //返回子的视图 @Override public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) { ZiViewHodel zi = null; if (view == null) { //找到布局文件 view = View.inflate(context, R.layout.adapterzi, null); //找到控件 ImageView iv = view.findViewById(R.id.adapterzi_iv); TextView title = view.findViewById(R.id.adapterzi_title); TextView subhead = view.findViewById(R.id.adapterzi_subhead); TextView price = view.findViewById(R.id.adapterzi_price); Button jia = view.findViewById(R.id.adapterzi_jia); Button jian = view.findViewById(R.id.adapterzi_jian); TextView count = view.findViewById(R.id.adapterzi_count); CheckBox box = view.findViewById(R.id.adapterzi_box); //把控件放到优化类中 zi = new ZiViewHodel(iv, title, subhead, price, jia, jian, count, box); //放到容器中 view.setTag(zi); } else { zi = (ZiViewHodel) view.getTag(); } //获取数据 String images = list.get(i).getList().get(i1).getImages();//图片 //拆分 String[] split = images.split("\\|"); String s = split[0]; String title = list.get(i).getList().get(i1).getTitle();//标题 String subhead = list.get(i).getList().get(i1).getSubhead();//内容 String price = list.get(i).getList().get(i1).getPrice();//价格 String num = list.get(i).getList().get(i1).getNum();//数量 final boolean boxChild = list.get(i).getList().get(i1).getBoxChild(); //赋值 Glide.with(context).load(s).into(zi.iv); zi.title.setText(title); zi.subhead.setText(subhead); zi.price.setText(price); zi.count.setText(num); zi.box.setChecked(boxChild); //复选框的点击事件 final ZiViewHodel finalZi = zi; zi.box.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //获取当前点击CheckBox的状态 boolean checked = finalZi.box.isChecked(); list.get(i).getList().get(i1).setBoxChild(checked); List<BeanQueryCart.DataBean.ListBean> listBeans = ExpanListAdapter.this.list.get(i).getList(); boolean flg=true; for (BeanQueryCart.DataBean.ListBean listBean : listBeans) { boolean boxChild1 = listBean.getBoxChild(); if (!boxChild1) { flg=false; } } list.get(i).setBox(flg); String aa="哈哈"; EventBus.getDefault().post(aa); notifyDataSetChanged(); } }); /* * 加减点击事件 * */ //加的点击事件 zi.jia.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { } }); //减的点击事件 zi.jian.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { } }); return view; } @Override public boolean isChildSelectable(int i, int i1) { return false; } //父的优化类 class FuViewHodel { public TextView tv; public CheckBox box; public FuViewHodel(TextView tv, CheckBox box) { this.tv = tv; this.box = box; } } /* * 子的优化类 * */ class ZiViewHodel { public ImageView iv; public TextView title; public TextView subhead; public TextView price; public Button jia; public Button jian; public TextView count; public CheckBox box; public ZiViewHodel(ImageView iv, TextView title, TextView subhead, TextView price, Button jia, Button jian, TextView count, CheckBox box) { this.iv = iv; this.title = title; this.subhead = subhead; this.price = price; this.jia = jia; this.jian = jian; this.count = count; this.box = box; } } }布局文件:
mainActivity布局:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="bawei.com.gouwuche.view.MainActivity"> <ExpandableListView android:id="@+id/main_ebv" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" android:orientation="horizontal"> <CheckBox android:id="@+id/main_box" android:layout_gravity="center_vertical" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> <TextView android:id="@+id/main_price" android:layout_gravity="center_vertical" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:textSize="20sp" android:text="价格:" /> <Button android:id="@+id/main_shan" android:layout_gravity="center_vertical" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:textSize="20sp" android:text="删除" /> </LinearLayout> </LinearLayout>
适配器的父类 adapterfu:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <CheckBox android:id="@+id/adapterfu_box" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/adapterfu_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="商家1" android:textSize="20sp" /> </LinearLayout> </LinearLayout> 适配器的字类:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <CheckBox android:id="@+id/adapterzi_box" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" /> <ImageView android:id="@+id/adapterzi_iv" android:layout_width="60dp" android:layout_height="60dp" android:layout_gravity="center_vertical" /> <LinearLayout android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/adapterzi_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="end" android:maxLines="1" android:text="哈哈啊哈哈啊" android:textSize="18sp" /> <TextView android:id="@+id/adapterzi_subhead" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="end" android:maxLines="1" android:text="哈哈啊哈哈啊" android:textSize="18sp" /> <TextView android:id="@+id/adapterzi_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="end" android:maxLines="1" android:text="哈哈啊哈哈啊" android:textSize="18sp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/adapterzi_jia" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+" android:textSize="20sp" /> <TextView android:id="@+id/adapterzi_count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" android:textSize="20sp" /> <Button android:id="@+id/adapterzi_jian" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" android:textSize="20sp" /> </LinearLayout> </LinearLayout> </LinearLayout>加减://加 holder.jia.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { ++aa; //获取当前点击的pid sellerid num String sellerid = list.get(position).getSellerid(); String pid1 = list.get(position).getPid(); //获取num当前的数量 String num1 = list.get(position).getNum(); Integer integer = Integer.valueOf(num1); int count = integer + aa; //给map赋值 map.put("sellerid", sellerid); map.put("pid", pid1); map.put("num", count+""); Log.d("哈哈", "onClick: "+count); //通过EventBus传值到frag4购物车 EventBus.getDefault().post(map); } }); //减 holder.jian.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String num = list.get(position).getNum(); if (num.equals("1")){ Toast.makeText(context, "不能再减了", Toast.LENGTH_SHORT).show(); return; } --aa; //获取当前点击的pid sellerid num String sellerid = list.get(position).getSellerid(); String pid1 = list.get(position).getPid(); //获取num当前的数量 String num1 = list.get(position).getNum(); Integer integer = Integer.valueOf(num1); int count = integer + aa; //给map赋值 map.put("sellerid", sellerid); map.put("pid", pid1); map.put("num", count+""); Log.d("哈哈", "onClick: "+count); //通过EventBus传值到frag4购物车 EventBus.getDefault().post(map); } }); //EventBus@Subscribe(threadMode = ThreadMode.MainThread, sticky = true) public void eventBus(Map<String,String> map){ if (map!=null){ //更新购物车 Presenter presenter=new Presenter((ViewInterface.ViewGengXinCart) this); presenter.presenGengXinCart(map); } }
购物车2
最新推荐文章于 2024-02-18 09:15:41 发布