Adater
package com.example.dell.kejibeidou.shop; import android.content.Context; import android.content.DialogInterface; import android.support.v7.app.AlertDialog; import android.util.Log; import android.view.LayoutInflater; 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 com.example.dell.kejibeidou.R; import com.example.dell.kejibeidou.bean.ShopBean; import java.util.List; /** * Created by DELL on 2018/4/25. */ public class ShopAdater extends BaseExpandableListAdapter{ private List<ShopBean.DataBean> data; private Context context; private ModifyGoodsItemNumberListener modifyGoodsItemNumberListener; private CheckGroupItemListener checkGroupItemListener; private CheckBox ck_group_choosed; private CheckBox ck_child_choosed; private ImageView iv_show_pic; private TextView tv_commodity_name; private TextView tv_commodity_attr; private TextView tv_commodity_price; private TextView tv_commodity_num; private TextView iv_sub; private TextView tv_commodity_show_num; private TextView iv_add; private Button btn_commodity_delete; private String pid; //定义删除接口 private DeleteItemLinister deleteItemLinister; public void setDeleteItemLinister(DeleteItemLinister deleteItemLinister) { this.deleteItemLinister = deleteItemLinister; } public void setData(List<ShopBean.DataBean> data) { this.data = data; } //上下文 public ShopAdater(Context context) { this.context = context; } public void setCheckGroupItemListener(CheckGroupItemListener checkGroupItemListener) { this.checkGroupItemListener = checkGroupItemListener; } public void setModifyGoodsItemNumberListener(ModifyGoodsItemNumberListener modifyGoodsItemNumberListener) { this.modifyGoodsItemNumberListener = modifyGoodsItemNumberListener; } //判断是否显示删除按钮 private boolean isEditor; public void showDeleteButton(boolean isEditor){ this.isEditor = isEditor; //刷新适配器 notifyDataSetChanged(); } @Override public int getGroupCount() { return data!=null?data.size():0; } @Override public int getChildrenCount(int i) { return data!=null?data.get(i).getList().size():0; } @Override public Object getGroup(int i) { return data.get(i); } @Override public Object getChild(int i, int i1) { return data.get(i1).getList().get(i1); } @Override public long getGroupId(int i) { return i; } @Override public long getChildId(int i, int i1) { return i1; } @Override public boolean hasStableIds() { return false; } //getGroupView @Override public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) { if(view == null){ view = LayoutInflater.from(context).inflate(R.layout.erji,viewGroup,false); } ck_group_choosed = (CheckBox) view.findViewById(R.id.ck_group_choosed); if(data.get(i).isGroupChoosed()){ ck_group_choosed.setChecked(true); }else{ ck_group_choosed.setChecked(false); } ck_group_choosed.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { checkGroupItemListener.checkGroupItem(i,((CheckBox)view).isChecked()); } }); ck_group_choosed.setText(data.get(i).getSellerName()); return view; } //getChildView @Override public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) { pid = data.get(i).getList().get(i1).getPid(); if(view == null){ view = LayoutInflater.from(context).inflate(R.layout.zhong,viewGroup,false); } ck_child_choosed = (CheckBox) view.findViewById(R.id.ck_child_choose); //商品图片 iv_show_pic = (ImageView) view.findViewById(R.id.iv_show_pic); //商品主标题 tv_commodity_name = (TextView) view.findViewById(R.id.tv_commodity_name); //商品副标题 tv_commodity_attr = (TextView) view.findViewById(R.id.tv_commodity_attr); //商品价格 tv_commodity_price = (TextView) view.findViewById(R.id.tv_commodity_price); //商品数量 tv_commodity_num = (TextView) view.findViewById(R.id.tv_commodity_num); //商品减 iv_sub = (TextView) view.findViewById(R.id.iv_sub); //商品加减中的数量变化 tv_commodity_show_num = (TextView) view.findViewById(R.id.tv_commodity_show_num); //商品加 iv_add = (TextView) view.findViewById(R.id.iv_add); //删除按钮 btn_commodity_delete = (Button) view.findViewById(R.id.btn_commodity_delete); tv_commodity_name.setText(data.get(i).getList().get(i1).getTitle()); tv_commodity_attr.setText(data.get(i).getList().get(i1).getSubhead()); tv_commodity_price.setText("¥"+data.get(i).getList().get(i1).getPrice()); tv_commodity_num.setText("x"+data.get(i).getList().get(i1).getNum()); tv_commodity_show_num.setText(data.get(i).getList().get(i1).getNum()+""); String images = data.get(i).getList().get(i1).getImages(); String[] urls = images.split("\\|"); Glide.with(context) .load(urls[0]) .crossFade() .into(iv_show_pic); //加点击事件 iv_add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { modifyGoodsItemNumberListener.doIncrease(i,i1,tv_commodity_show_num); } }); //减的点击事件 iv_sub.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { modifyGoodsItemNumberListener.doDecrease(i,i1,tv_commodity_show_num); } }); ck_child_choosed.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //isChecked false true checkGroupItemListener.checkChildItem(i,i1,((CheckBox)view).isChecked()); } }); if(data.get(i).getList().get(i1).isChildChoosed()){ ck_child_choosed.setChecked(true); }else{ ck_child_choosed.setChecked(false); } //删除 if (isEditor){ btn_commodity_delete.setVisibility(View.VISIBLE); }else { btn_commodity_delete.setVisibility(View.GONE); } //点击删除出现对话框以及通过接口删除的pid btn_commodity_delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(final View view) { AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("提示"); builder.setMessage("确定删除此商品吗?"); builder.setNegativeButton("取消",null); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { deleteItemLinister.doDelete(pid,view,i,i1); Log.e("TAAG",pid+""); notifyDataSetChanged(); } }); //显示!!! builder.show(); } }); return view; } @Override public boolean isChildSelectable(int i, int i1) { return false; } public interface CheckGroupItemListener{ //商家的条目的复选框监听 void checkGroupItem(int groupPosition,boolean isChecked); //商品的 void checkChildItem(int groupPosition,int childPosition,boolean isChecked); } /** * 商品加减接口 */ public interface ModifyGoodsItemNumberListener{ //商品添加操作 void doIncrease(int groupPosition,int childPosition,View view); //商品减少操作 void doDecrease(int groupPosition,int childPosition,View view); } /** * 商品删除接口 */ public interface DeleteItemLinister{ //删除 void doDelete(String pid,View view,int i,int i1); } }
Activity
package com.example.dell.kejibeidou.shop; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Color; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.widget.SwipeRefreshLayout; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.CheckBox; import android.widget.ExpandableListView; import android.widget.TextView; import android.widget.Toast; import com.example.dell.kejibeidou.R; import com.example.dell.kejibeidou.bean.DeleteBean; import com.example.dell.kejibeidou.bean.ShopBean; import com.example.dell.kejibeidou.delete.persenter.DeleteMyPseneter; import com.example.dell.kejibeidou.delete.view.DeleteIView; import com.example.dell.kejibeidou.dingdan.DdActivity; import com.example.dell.kejibeidou.shop.shoppersenter.ShopMyPersenter; import com.example.dell.kejibeidou.shop.shopview.ShopIVIew; import java.util.ArrayList; import java.util.List; import static android.content.Context.MODE_PRIVATE; /** * Created by DELL on 2018/4/17. */ public class Shop extends Fragment implements ShopIVIew,ShopAdater.ModifyGoodsItemNumberListener,ShopAdater.CheckGroupItemListener,DeleteIView{ @Nullable private int totalNum = 0; private double totalPrice= 0.00; private ShopMyPersenter shopMyPersenter; private ExpandableListView expandList; private CheckBox btnCheckAll; private TextView tvTotalPrice; private TextView btnAmount; private TextView btnEditor; private View view; private ShopAdater adater; private boolean flag; private DeleteMyPseneter deleteMyPseneter; private SwipeRefreshLayout mSwip; private String uid; private List<ShopBean.DataBean> data = new ArrayList<>(); @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.shop_item,container,false); //登录接受过来的uid SharedPreferences sharedPreferences=getContext().getSharedPreferences("chenggong",MODE_PRIVATE); uid = sharedPreferences.getString("uid", ""); Log.e("TAG1",uid+""); //实例化p层 shopMyPersenter = new ShopMyPersenter(this); shopMyPersenter.Info(uid,"android"); //实例化删除p层 deleteMyPseneter = new DeleteMyPseneter(this); initview(); return view; } private void initview() { expandList = (ExpandableListView) view.findViewById(R.id.expandList); btnCheckAll = (CheckBox) view.findViewById(R.id.btnCheckAll); tvTotalPrice = (TextView) view.findViewById(R.id.tvTotalPrice); btnAmount = (TextView) view.findViewById(R.id.btnAmount); btnEditor = (TextView) view.findViewById(R.id.btnEditor); mSwip = (SwipeRefreshLayout) view.findViewById(R.id.swip); expandList.setGroupIndicator(null); adater = new ShopAdater(getActivity()); expandList.setAdapter(adater); adater.setModifyGoodsItemNumberListener(this); adater.setCheckGroupItemListener(this); //全选按钮 btnCheckAll.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { isChoosedAll(((CheckBox)view).isChecked()); //计算商品总价 statisticsPrice(); } }); //编译删除按钮 btnEditor.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (!flag) { flag = true; btnEditor.setText("完成"); adater.showDeleteButton(flag); } else { flag = false; btnEditor.setText("编辑"); adater.showDeleteButton(flag); } } }); //跳到订单 btnAmount.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(getActivity(), DdActivity.class); intent.putExtra("uid",uid); // intent.putExtra("price",String.valueOf(totalPrice)); startActivity(intent); } }); } @Override public void checkGroupItem(int groupPosition, boolean isChecked) { ShopBean.DataBean dataBean = data.get(groupPosition); dataBean.setGroupChoosed(isChecked); //遍历商家里面的商品,将其置为选中状态 for (ShopBean.DataBean.ListBean listBean : dataBean.getList()){ listBean.setChildChoosed(isChecked); } //底部结算那个checkbox状态(全选) if(isCheckAll()){ btnCheckAll.setChecked(true); }else{ btnCheckAll.setChecked(false); } //刷新适配器 adater.notifyDataSetChanged(); //计算价格 statisticsPrice(); } @Override public void checkChildItem(int groupPosition, int childPosition, boolean isChecked) { ShopBean.DataBean dataBean = data.get(groupPosition);//商家那一层 List<ShopBean.DataBean.ListBean> listBeans = dataBean.getList(); ShopBean.DataBean.ListBean listBean = listBeans.get(childPosition); //你点击商家的商品条目将其选中状态记录 listBean.setChildChoosed(isChecked); //检测商家里面的每一个商品是否被选中,如果被选中,返回boolean boolean result = isGoodsCheckAll(groupPosition); if(result){ dataBean.setGroupChoosed(result); }else{ dataBean.setGroupChoosed(result); } //底部结算那个checkbox状态(全选) if(isCheckAll()){ btnCheckAll.setChecked(true); }else{ btnCheckAll.setChecked(false); } //刷新适配器 adater.notifyDataSetChanged(); //计算总价 statisticsPrice(); } @Override public void doIncrease(int groupPosition, int childPosition, View view) { ShopBean.DataBean.ListBean listBean = data.get(groupPosition).getList().get(childPosition); //取出当前的商品数量 int currentNum = Integer.valueOf(listBean.getNum()); //商品++ currentNum++; //将商品数量设置javabean上 listBean.setNum(String.valueOf(currentNum)); //刷新适配器 adater.notifyDataSetChanged(); //计算商品价格 statisticsPrice(); } @Override public void doDecrease(int groupPosition, int childPosition, View view) { ShopBean.DataBean.ListBean listBean = data.get(groupPosition).getList().get(childPosition); //取出当前的商品数量 int currentNum = Integer.valueOf(listBean.getNum()); //直接结束这个方法 if(currentNum == 1){ return; } currentNum--; listBean.setNum(String.valueOf(currentNum)); //刷新适配器 adater.notifyDataSetChanged(); //计算商品价格 statisticsPrice(); } @Override public void chenggong(ShopBean shopBean) { data.addAll(shopBean.getData()); adater.setData(data); defaultExpand(); adater.notifyDataSetChanged(); //点击适配器拿到接口,删除商品 adater.setDeleteItemLinister(new ShopAdater.DeleteItemLinister() { @Override public void doDelete(String pid, View view, int i, int i1) { Log.e("TAG2",pid+""); deleteMyPseneter.DeleteInfo(uid,pid); adater.notifyDataSetChanged(); } }); //刷新 mSwip.setColorSchemeColors(Color.GRAY); mSwip.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { mSwip.setRefreshing(false); data.clear(); shopMyPersenter.Info(uid,"android"); Log.e("TAG3",uid+""); Toast.makeText(getContext(),"刷新成功",Toast.LENGTH_SHORT).show(); } }); } private void defaultExpand() { for (int i = 0; i < adater.getGroupCount();++i){ expandList.expandGroup(i); } } @Override public void shibai(String msg) { } private boolean isGoodsCheckAll(int groupPosition){ List<ShopBean.DataBean.ListBean> listBeans = this.data.get(groupPosition).getList(); //遍历某一个商家的每个商品是否被选中 for (ShopBean.DataBean.ListBean listBean : listBeans){ if(listBean.isChildChoosed()){//是选中状态 continue; }else{ return false; } } return true; } private boolean isCheckAll(){ for (ShopBean.DataBean dataBean : data){ if(!dataBean.isGroupChoosed()){ return false; } } return true; } private void isChoosedAll(boolean isChecked){ for (ShopBean.DataBean dataBean : data){ dataBean.setGroupChoosed(isChecked); for (ShopBean.DataBean.ListBean listBean : dataBean.getList()){ listBean.setChildChoosed(isChecked); } } //刷新适配器 adater.notifyDataSetChanged(); } private void statisticsPrice(){ //初始化值 totalNum = 0; totalPrice = 0.00; for (ShopBean.DataBean dataBean : data){ for (ShopBean.DataBean.ListBean listBean : dataBean.getList()){ if(listBean.isChildChoosed()){ totalNum++; System.out.println("number : "+totalNum); totalPrice += Integer.valueOf(listBean.getNum())*Double.valueOf(listBean.getPrice()); } } } //设置文本信息 合计、结算的商品数量 tvTotalPrice.setText("合计:¥"+totalPrice); btnAmount.setText("结算("+totalNum+")"); } @Override public void Deletechenggong(DeleteBean bean) { Toast.makeText(getActivity(),bean.getMsg(),Toast.LENGTH_SHORT).show(); } @Override public void Deleteshibai(DeleteBean deleteBean) { Toast.makeText(getActivity(),deleteBean.getMsg(),Toast.LENGTH_SHORT).show(); } }