自学记录:无详解
BasePresenter:public class BasePresenter<T extends IBaseView> { private T t; public void attachView(T t){ this.t=t; } public T getView(){ return t; } public void detachView(){ t=null; } }CartPresenter:
public class CartPresenter extends BasePresenter<ICartView>{ private NetUtil netUtil; public CartPresenter() { netUtil = NetUtil.getInstance(); } public void getDataFromServer(String path) { netUtil.doGet(path, new HttpUtilsCallback() { @Override public void onSuccess(String success) { getView().onSuccess(success); } @Override public void onFail(int errCode, String errMsg) { } }); } }
CalcUtils:
/** * 功能描述: 用于对数值的精确计算工具类 * author:Created by WangZhiQiang on 2018/5/18. */ public class CalcUtils { public static final int TYPE_ADD = 0x00; // 加法 public static final int TYPE_MULTIPLY = 0x01; // 乘法 public static final int TYPE_DIVIDE = 0x02; // 除法 public static final int TYPE_SUBTRACT = 0x03; // 减法 /** * 加法 * @param a * @param b * @return */ public static Double add(Double a, Double b) { return calc(a, b, -1, TYPE_ADD, null); } /** * 减法 * @param a * @param b * @return */ public static Double sub(Double a, Double b) { return calc(a, b, -1, TYPE_SUBTRACT, null); } /** * 乘法 * @param a * @param b * @return */ public static Double multiply(Double a, Double b) { return calc(a, b, -1, TYPE_MULTIPLY, null); } /** * 除法 * @param a * @param b * @return */ public static Double divide(Double a, Double b) { return calc(a, b, -1, TYPE_DIVIDE, null); } /** * Scale * 是用来对利用BigDecimal对数值进行运算后保留的位数。 * * RouningMode * 该参数是BigDecimal是一个枚举类,包含有8个枚举类型,用来说明对经过计算后数值的取舍模式。 * * ROUND_UP:远离零方向舍入。向绝对值最大的方向舍入,只要舍弃位非0即进位。 * ROUND_DOWN:趋向零方向舍入。向绝对值最小的方向输入,所有的位都要舍弃,不存在进位情况。 * ROUND_CEILING:向正无穷方向舍入。向正最大方向靠拢。若是正数,舍入行为类似于ROUND_UP,若为负数,舍入行为类似于ROUND_DOWN。Math.round()方法就是使用的此模式。 * ROUND_FLOOR:向负无穷方向舍入。向负无穷方向靠拢。若是正数,舍入行为类似于ROUND_DOWN;若为负数,舍入行为类似于ROUND_UP。 * HALF_UP:最近数字舍入(5进)。这是我们最经典的四舍五入。 * HALF_DOWN:最近数字舍入(5舍)。在这里5是要舍弃的。 * HAIL_EVEN:银行家舍入法。 */ /** * 乘法 * @param a * @param b * @param scale 小数点后保留的位数 * @param mode 保留的模式 * @return */ public static Double multiply(Double a, Double b, int scale, RoundingMode mode) { return calc(a, b, scale, TYPE_MULTIPLY, mode); } /** * 除法 * @param a * @param b * @param scale 小数点后保留的位数 * @param mode 保留的模式 * @return */ public static Double divide(Double a, Double b, int scale, RoundingMode mode) { return calc(a, b, scale, TYPE_DIVIDE, mode); } /** * 计算 * @param a * @param b * @param scale * @param type * @param mode * @return */ private static Double calc(Double a, Double b, int scale, int type, RoundingMode mode) { BigDecimal result = null; BigDecimal bgA = new BigDecimal(String.valueOf(a)); BigDecimal bgB = new BigDecimal(String.valueOf(b)); switch (type) { case TYPE_ADD: result = bgA.add(bgB); break; case TYPE_MULTIPLY: result = bgA.multiply(bgB); break; case TYPE_DIVIDE: try { result = bgA.divide(bgB); } catch (ArithmeticException e) {// 防止无限循环而报错 采用四舍五入保留3位有效数字 result = bgA.divide(bgB,3,RoundingMode.HALF_DOWN); } break; case TYPE_SUBTRACT: result = bgA.subtract(bgB); break; } if (mode==null) { if(scale!=-1){ result = result.setScale(scale); } }else{ if(scale!=-1){ result = result.setScale(scale,mode); } } return result.doubleValue(); } }
FrescoUtil:
/** * Fresco加载图片工具类 * author:Created by WangZhiQiang on 2018/5/10. */ public class FrescoUtil { /** * 基础加载图片 * @param url 图片路径 * @param simpleDraweeView 控件 */ public static void setTu(String url,SimpleDraweeView simpleDraweeView){ Uri uri = Uri.parse(url); simpleDraweeView.setImageURI(uri); } /** * 渐进式加载图片 * @param url 图片路径 * @param simpleDraweeView 控件 */ public static void setJianJin(String url, SimpleDraweeView simpleDraweeView){ Uri uri = Uri.parse(url); ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri) .setProgressiveRenderingEnabled(true) .build(); DraweeController controller = Fresco.newDraweeControllerBuilder() .setImageRequest(request) .setOldController(simpleDraweeView.getController()) .build(); simpleDraweeView.setController(controller); } /** * 圆角图片 * @param url 图片路径 * @param simpleDraweeView 控件 * @param radius 角度 * @param color 描边线颜色 * @param width 描边线宽度 */ public static void setYuanJiao(String url,SimpleDraweeView simpleDraweeView,float radius,int color,float width){ Uri uri = Uri.parse(url); RoundingParams roundingParams = RoundingParams.fromCornersRadius(0f); if (width > 0f) { roundingParams.setBorder(color, width);//描边线 } roundingParams.setCornersRadius(radius);//总体圆角 simpleDraweeView.getHierarchy().setRoundingParams(roundingParams); simpleDraweeView.setImageURI(uri); } /** * 圆角图片 * 可控四角角度 * @param url 图片路径 * @param simpleDraweeView 控件 * @param topLeft 左上角 * @param topRight 右上角 * @param bottomRight 右下角 * @param bottomLeft 左下角 * @param color 描边线颜色 * @param width 描边线宽度 */ public static void setYuanJiao(String url,SimpleDraweeView simpleDraweeView,float topLeft, float topRight, float bottomRight, float bottomLeft,int color,float width){ Uri uri = Uri.parse(url); RoundingParams roundingParams = RoundingParams.fromCornersRadius(0f); if (width > 0f) { roundingParams.setBorder(color, width);//描边线 } roundingParams.setCornersRadii(topLeft,topRight,bottomRight,bottomLeft);//各角不同圆角 simpleDraweeView.getHierarchy().setRoundingParams(roundingParams); simpleDraweeView.setImageURI(uri); } /** * 圆形图片 * @param url 图片路径 * @param simpleDraweeView 控件 * @param color 描边线颜色 * @param width 描边线宽度 */ public static void setYuanQuan(String url,SimpleDraweeView simpleDraweeView,int color,float width){ Uri uri = Uri.parse(url); RoundingParams roundingParams = RoundingParams.fromCornersRadius(0f); if (width > 0f) { roundingParams.setBorder(color, width);//描边线 } roundingParams.setRoundAsCircle(true);//圆形 simpleDraweeView.getHierarchy().setRoundingParams(roundingParams); simpleDraweeView.setImageURI(uri); } /** * Gif动态图片 * @param url 图片路径 * @param simpleDraweeView 控件 */ public static void setDongTu(String url,SimpleDraweeView simpleDraweeView){ Uri uri = Uri.parse(url); DraweeController controller1 = Fresco.newDraweeControllerBuilder() .setUri(uri) .setTapToRetryEnabled(true) .setAutoPlayAnimations(true)//设置为true将循环播放Gif动画 .setOldController(simpleDraweeView.getController()) .build(); simpleDraweeView.setController(controller1); } }
BaseFragment:
public abstract class BaseFragment<P extends BasePresenter> extends Fragment implements IBaseView { private P p; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(setChildContentView(), container, false); initView(view); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); p=initPresenter(); if (p != null) { p.attachView(this); }else { try { throw new Exception("少年 prenter 没有设置 请在您的Activity 创建 presenter"); } catch (Exception e) { e.printStackTrace(); } } initData(); } public P getPresenter() { return p; } abstract void initView(View view); abstract void initData(); abstract P initPresenter(); abstract int setChildContentView(); }FragmentCart:
public class FragmentCart extends BaseFragment<CartPresenter> implements ICartView, CartsAdapter.UpdateView, View.OnClickListener, CartsAdapter.DeleteView { private String urlCarts="https://www.zhaoapi.cn/product/getCarts"; private String urlDeleteCarts="https://www.zhaoapi.cn/product/deleteCart"; private CartBean cartBean=new CartBean(); private DeleteCartBean deleteCartBean=new DeleteCartBean(); private String uid; private String token; private SharedPreferences sharedPreferences; private ExpandableListView elv_cart; private CheckBox cb_cart_select_all; private TextView tv_cart_all_money; private TextView tv_cart_transport; private Button btn_cart_settlement; private CartsAdapter adapter; private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what){ case 0: adapter.setData(cartBean); adapter.notifyDataSetChanged(); //展开所有的分组 if (cartBean!=null){ for (int i = 0; i < cartBean.getData().size(); i++) { elv_cart.expandGroup(i); } } break; case 1: Toast.makeText(getActivity(), deleteCartBean.getMsg(), Toast.LENGTH_SHORT).show(); break; } } }; @Override void initView(View view) { elv_cart = view.findViewById(R.id.elv_cart); cb_cart_select_all = view.findViewById(R.id.cb_cart_select_all); tv_cart_all_money = view.findViewById(R.id.tv_cart_all_money); tv_cart_transport = view.findViewById(R.id.tv_cart_transport); btn_cart_settlement = view.findViewById(R.id.btn_cart_settlement); sharedPreferences = getActivity().getSharedPreferences("user", getActivity().MODE_PRIVATE); getUidAndToken(); //去掉ExpandableListView 默认的箭头 elv_cart.setGroupIndicator(null); } // @Override // public void onHiddenChanged(boolean hidden) { // super.onHiddenChanged(hidden); // if (!hidden){ // Log.e("myMessage","购物车页面显示了"); // getData(); // } // } @Subscribe(threadMode = ThreadMode.MAIN, sticky = true) public void onMessageEvent(MessageEvent event) { if (event.isSuccessClassify()||event.isClickCart()){ Log.e("myMessage" , "isSuccessClassify:"+event.isSuccessClassify()); Log.e("myMessage" , "点击购物车:"+event.isClickCart()); getData(); EventBus.getDefault().removeStickyEvent(event); } if (event.isLoginOk()){ Log.e("myMessage" , "isLoginOk:"+event.isLoginOk()); getUidAndToken(); } } private void getUidAndToken() { uid=sharedPreferences.getString("uid",null); token=sharedPreferences.getString("token",null); } private void getData(){ getUidAndToken(); if (!(TextUtils.isEmpty(uid)||TextUtils.isEmpty(token))){ // Log.e("myMessage" , "查询购物车uid:"+uid+"--token:"+token); HashMap<String, String> map = new HashMap<>(); map.put("source","android"); map.put("uid",uid); map.put("token",token); getPresenter().getDataFromServer(urlCarts,map,1); }else { cartBean=null; adapter.setData(cartBean); adapter.notifyDataSetChanged(); } } @Override void initData() { adapter = new CartsAdapter(getActivity()); elv_cart.setAdapter(adapter); adapter.setUpdateViewListener(this); adapter.setDeleteViewListener(this); EventBus.getDefault().register(this); //全选按钮点击事件 cb_cart_select_all.setOnClickListener(this); } private boolean one=true; @Override public void onSuccess(String success, int flag) { Gson gson = new Gson(); switch (flag){ case 1: cartBean = gson.fromJson(success, CartBean.class); handler.sendEmptyMessage(0); if (one){ MessageEvent messageEvent = new MessageEvent(); messageEvent.setSuccessCart(true); EventBus.getDefault().postSticky(messageEvent); one=false; } break; case 2: deleteCartBean = gson.fromJson(success, DeleteCartBean.class); handler.sendEmptyMessage(1); break; } } @Override public void update(boolean isAllSelected, int count, double price) { btn_cart_settlement.setText("结算(" + count + ")"); tv_cart_all_money.setText("¥" + price); cb_cart_select_all.setChecked(isAllSelected); } @Override public void onClick(View v) { selectAll(); } private void selectAll() { int allCount = cartBean.getAllCount(); double allMoney = cartBean.getAllMoney(); if (cb_cart_select_all.isChecked()) { cartBean.setAllSelect(true); for (int i = 0; i < cartBean.getData().size(); i++) { cartBean.getData().get(i).setSelected(true); for (int n = 0; n < cartBean.getData().get(i).getList().size(); n++) { if (!cartBean.getData().get(i).getList().get(n).isSelected()) { allCount++; allMoney=CalcUtils.add(allMoney, CalcUtils.multiply((double) cartBean.getData().get(i).getList().get(n).getNum(), cartBean.getData().get(i).getList().get(n).getBargainPrice())); cartBean.getData().get(i).getList().get(n).setSelected(true); } } } } else { cartBean.setAllSelect(false); for (int i = 0; i < cartBean.getData().size(); i++) { cartBean.getData().get(i).setSelected(false); for (int n = 0; n < cartBean.getData().get(i).getList().size(); n++) { cartBean.getData().get(i).getList().get(n).setSelected(false); } allCount = 0; allMoney = 0; } } cartBean.setAllMoney(allMoney); cartBean.setAllCount(allCount); update(cartBean.isAllSelect(), allCount, allMoney); adapter.notifyDataSetChanged(); } @Override public void delete(final CartsAdapter.UpdateView updateViewListener, final CheckBox cbItem, final EditText etCount, ImageView imgDelete, final int groupPosition, final int childPosition) { //删除 imgDelete.setOnClickListener(new View.OnClickListener() { int allCount = cartBean.getAllCount(); double allMoney = cartBean.getAllMoney(); @Override public void onClick(View v) { HashMap<String, String> map = new HashMap<>(); map.put("uid",uid); map.put("token",token); map.put("pid",cartBean.getData().get(groupPosition).getList().get(childPosition).getPid()+""); getPresenter().getDataFromServer(urlDeleteCarts,map,2); if(cbItem.isChecked()){ allCount--; allMoney=CalcUtils.sub(allMoney, CalcUtils.multiply((double) cartBean.getData().get(groupPosition).getList().get(childPosition).getNum(), cartBean.getData().get(groupPosition).getList().get(childPosition).getBargainPrice())); cartBean.getData().get(groupPosition).getList().get(childPosition).setNum(Integer.valueOf(etCount.getText().toString())); } cartBean.getData().get(groupPosition).getList().remove(childPosition); if (cartBean.getData().get(groupPosition).getList().size() == 0) { cartBean.getData().remove(groupPosition); } cartBean.setAllCount(allCount); cartBean.setAllMoney(allMoney); adapter.notifyDataSetChanged(); updateViewListener.update(cartBean.isAllSelect(), allCount, allMoney); } }); } @Override CartPresenter initPresenter() { return new CartPresenter(); } @Override int setChildContentView() { return R.layout.fragment_cart; } @Override public void onError(String error) { } @Override public void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); getPresenter().detachView(); } }CartsAdapter:
public class CartsAdapter extends BaseExpandableListAdapter{ private Context mContext; private UpdateView updateViewListener; private DeleteView deleteView; private CartBean cartBean; public CartsAdapter(Context context) { this.mContext = context; } public void setData(CartBean cartBean){ this.cartBean=cartBean; } public void setUpdateViewListener(UpdateView listener) { if (updateViewListener == null) { this.updateViewListener = listener; } } public void setDeleteViewListener(DeleteView deleteView){ if (this.deleteView == null) { this.deleteView = deleteView; } } @Override public int getGroupCount() { if (cartBean==null){ return 0; } return cartBean.getData().size(); } @Override public int getChildrenCount(int groupPosition) { if (cartBean==null){ return 0; } return cartBean.getData().get(groupPosition).getList().size(); } @Override public Object getGroup(int groupPosition) { return cartBean.getData().get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return cartBean.getData().get(groupPosition).getList().get(childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return false; } @Override public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { updateViewListener.update(cartBean.isAllSelect(), cartBean.getAllCount(), cartBean.getAllMoney()); final GroupViewHolder holder; if (convertView==null){ convertView = View.inflate(mContext, R.layout.item_shopingcart_group, null); holder=new GroupViewHolder(convertView); convertView.setTag(holder); }else { holder= (GroupViewHolder) convertView.getTag(); } holder.cbGroupItem.setTag(groupPosition); holder.tvPosition.setText(cartBean.getData().get(groupPosition).getSellerName()); //根据获取的状态设置是否被选中 holder.cbGroupItem.setChecked(cartBean.getData().get(groupPosition).isSelected()); //选中店铺,商品全选 holder.cbGroupItem.setOnClickListener(new View.OnClickListener() { int allCount = cartBean.getAllCount();//被选中的item数量 double allMoney = cartBean.getAllMoney(); int childSize = cartBean.getData().get(groupPosition).getList().size(); @Override public void onClick(View v) { cartBean.getData().get(groupPosition).setSelected(holder.cbGroupItem.isChecked()); Log.e("--groupisSelected--",cartBean.getData().get(groupPosition).isSelected()+""); if (holder.cbGroupItem.isChecked()) { for (int i = 0; i < childSize; i++) { if (!cartBean.getData().get(groupPosition).getList().get(i).isSelected()) { allCount++; cartBean.getData().get(groupPosition).getList().get(i).setSelected(holder.cbGroupItem.isChecked()); allMoney=CalcUtils.add(allMoney, CalcUtils.multiply((double) cartBean.getData().get(groupPosition).getList().get(i).getNum(), cartBean.getData().get(groupPosition).getList().get(i).getBargainPrice())); } } } else { allCount -= childSize; for (int i = 0; i < childSize; i++) { cartBean.getData().get(groupPosition).getList().get(i).setSelected(holder.cbGroupItem.isChecked()); allMoney=CalcUtils.sub(allMoney, CalcUtils.multiply((double) cartBean.getData().get(groupPosition).getList().get(i).getNum(), cartBean.getData().get(groupPosition).getList().get(i).getBargainPrice())); } } //父item选中的数量 int fCount = 0; //判断是否所有的父item都被选中,决定全选按钮状态 for (int i = 0; i < cartBean.getData().size(); i++) { if (cartBean.getData().get(i).isSelected()) { fCount++; } } if (fCount == cartBean.getData().size()) { cartBean.setAllSelect(true); } else { cartBean.setAllSelect(false); } cartBean.setAllCount(allCount); cartBean.setAllMoney(allMoney); notifyDataSetChanged(); updateViewListener.update(cartBean.isAllSelect(), allCount, allMoney); } }); return convertView; } @Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final ChildViewHolder holder; if (convertView == null) { convertView = View.inflate(mContext,R.layout.item_shopingcart_child, null); holder = new ChildViewHolder(convertView); convertView.setTag(holder); } else { holder = (ChildViewHolder) convertView.getTag(); } String images = cartBean.getData().get(groupPosition).getList().get(childPosition).getImages(); String s=images; int i = images.indexOf("|"); if (i!=-1){ s = images.substring(0, i); } FrescoUtil.setJianJin(s,holder.imgIcon); holder.tvPrice.setText("¥" + cartBean.getData().get(groupPosition).getList().get(childPosition).getBargainPrice()); holder.tvGoodName.setText(cartBean.getData().get(groupPosition).getList().get(childPosition).getTitle()); holder.etCount.setText(String.valueOf(cartBean.getData().get(groupPosition).getList().get(childPosition).getNum())); //根据获取的状态设置是否被选中 holder.cbItem.setChecked(cartBean.getData().get(groupPosition).getList().get(childPosition).isSelected()); //添加商品数量 holder.tvAdd.setOnClickListener(new View.OnClickListener() { int allCount = cartBean.getAllCount(); double allMoney = cartBean.getAllMoney(); @Override public void onClick(View v) { int goodCount = cartBean.getData().get(groupPosition).getList().get(childPosition).getNum(); cartBean.getData().get(groupPosition).getList().get(childPosition).setNum(addCount(goodCount)); if (cartBean.getData().get(groupPosition).getList().get(childPosition).isSelected()) { allMoney=CalcUtils.add(allMoney,cartBean.getData().get(groupPosition).getList().get(childPosition).getBargainPrice()); updateViewListener.update(cartBean.isAllSelect(), allCount, allMoney); } cartBean.setAllMoney(allMoney); notifyDataSetChanged(); } }); //减少商品数量 holder.tvReduce.setOnClickListener(new View.OnClickListener() { int allCount = cartBean.getAllCount(); double allMoney = cartBean.getAllMoney(); @Override public void onClick(View v) { int goodCount = cartBean.getData().get(groupPosition).getList().get(childPosition).getNum(); if (Integer.valueOf(goodCount) > 1) { cartBean.getData().get(groupPosition).getList().get(childPosition).setNum(reduceCount(goodCount)); if (cartBean.getData().get(groupPosition).getList().get(childPosition).isSelected()) { allMoney=CalcUtils.sub(allMoney,cartBean.getData().get(groupPosition).getList().get(childPosition).getBargainPrice()); updateViewListener.update(cartBean.isAllSelect(), allCount, allMoney); } cartBean.setAllMoney(allMoney); notifyDataSetChanged(); } } }); //商品选中 holder.cbItem.setOnClickListener(new View.OnClickListener() { int allCount = cartBean.getAllCount(); double allMoney = cartBean.getAllMoney(); @Override public void onClick(View v) { int cCount = 0;//子item被选中的数量 int fcCount = 0;//父item被选中的数量 if(holder.cbItem.isChecked()){ cartBean.getData().get(groupPosition).getList().get(childPosition).setNum(Integer.valueOf(holder.etCount.getText().toString())); } cartBean.getData().get(groupPosition).getList().get(childPosition).setSelected(holder.cbItem.isChecked()); //遍历父item所有数据,统计被选中的item数量 for (int i = 0; i < cartBean.getData().get(groupPosition).getList().size(); i++) { if (cartBean.getData().get(groupPosition).getList().get(i).isSelected()) { cCount++; } } //判断是否所有的子item都被选中,决定父item状态 if (cCount == cartBean.getData().get(groupPosition).getList().size()) { cartBean.getData().get(groupPosition).setSelected(true); } else { cartBean.getData().get(groupPosition).setSelected(false); } //判断是否所有的父item都被选中,决定全选按钮状态 for (int i = 0; i < cartBean.getData().size(); i++) { if (cartBean.getData().get(i).isSelected()) { fcCount++; } } if (fcCount == cartBean.getData().size()) { cartBean.setAllSelect(true); } else { cartBean.setAllSelect(false); } //判断子item状态,更新结算总商品数和合计Money if (holder.cbItem.isChecked()) { allCount++; allMoney=CalcUtils.add(allMoney, CalcUtils.multiply((double) cartBean.getData().get(groupPosition).getList().get(childPosition).getNum(), cartBean.getData().get(groupPosition).getList().get(childPosition).getBargainPrice())); } else { allCount--; allMoney=CalcUtils.sub(allMoney, CalcUtils.multiply((double) cartBean.getData().get(groupPosition).getList().get(childPosition).getNum(), cartBean.getData().get(groupPosition).getList().get(childPosition).getBargainPrice())); cartBean.getData().get(groupPosition).getList().get(childPosition).setNum(Integer.valueOf(holder.etCount.getText().toString())); } // Log.e("adapter", "allMoney: "+allMoney); cartBean.setAllCount(allCount); cartBean.setAllMoney(allMoney); notifyDataSetChanged(); updateViewListener.update(cartBean.isAllSelect(), allCount, allMoney); } }); deleteView.delete(updateViewListener,holder.cbItem,holder.etCount,holder.imgDelete,groupPosition,childPosition); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; } private int addCount(int var) { Integer integer = Integer.valueOf(var); integer++; return integer; } private int reduceCount(int var) { Integer integer = Integer.valueOf(var); if (integer > 1) { integer--; } return integer; } class GroupViewHolder { CheckBox cbGroupItem; TextView tvPosition; public GroupViewHolder(View view) { cbGroupItem = view.findViewById(R.id.cb_cart_item_group); tvPosition = view.findViewById(R.id.tv_cart_item_group); } } class ChildViewHolder { CheckBox cbItem; TextView tvPrice; TextView tvGoodName; EditText etCount; TextView tvReduce; TextView tvAdd; ImageView imgDelete; SimpleDraweeView imgIcon; public ChildViewHolder(View view) { cbItem = view.findViewById(R.id.cb_cart_item_child); tvPrice = view.findViewById(R.id.tv_cart_item_child_price); tvGoodName = view.findViewById(R.id.tv_cart_item_child_name); etCount = view.findViewById(R.id.et_cart_item_child_count); tvReduce = view.findViewById(R.id.tv_cart_item_child_reduce); tvAdd = view.findViewById(R.id.tv_cart_item_child_add); imgDelete = view.findViewById(R.id.img_cart_item_child_delete); imgIcon = view.findViewById(R.id.sdv_cart_item_child); } } public interface UpdateView { void update(boolean isAllSelected, int count, double price); } public interface DeleteView { void delete(UpdateView updateViewListener, CheckBox cbItem, EditText etCount, ImageView imgDelete, int groupPosition, int childPosition); } }
IBaseView:
public interface IBaseView { }
ICartView:
public interface ICartView extends IBaseView { void onSuccess(String success); void onError(String error); }fragment_cart.xml
<?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"> <ExpandableListView android:id="@+id/elv_cart" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:scrollbars="none" android:divider="@null"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/divide_line"/> <LinearLayout android:layout_width="match_parent" android:layout_height="49dp" android:background="@android:color/white" android:gravity="center_vertical" android:orientation="horizontal"> <CheckBox android:id="@+id/cb_cart_select_all" android:layout_width="24dp" android:layout_height="24dp" android:layout_marginLeft="15dp"/> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_weight="0.69" android:text="全选" android:textColor="#333333" android:textSize="15sp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="end" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="合计" android:textColor="#333333" android:textSize="15sp" /> <TextView android:id="@+id/tv_cart_all_money" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="¥0" android:textColor="#FE3824" android:textSize="15sp" /> </LinearLayout> <TextView android:id="@+id/tv_cart_transport" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="运费:¥0" android:textColor="#999999" android:textSize="11sp" /> </LinearLayout> <Button android:id="@+id/btn_cart_settlement" android:layout_width="95dp" android:layout_height="match_parent" android:layout_marginLeft="15dp" android:background="#FE3824" android:text="结算(0)" android:textColor="@android:color/white" android:textSize="16sp" /> </LinearLayout> </LinearLayout>item_shopingcart_group.xml:
<?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="wrap_content" android:background="@android:color/white" android:orientation="vertical"> <View android:layout_width="match_parent" android:layout_height="3dp" android:background="@color/divide_line" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:padding="15dp"> <CheckBox android:id="@+id/cb_cart_item_group" android:layout_width="24dp" android:layout_height="24dp" android:focusable="false"/> <TextView android:id="@+id/tv_cart_item_group" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginLeft="15dp" android:drawableLeft="@drawable/dianpu" android:drawablePadding="5dp" android:gravity="center_vertical" android:text="京东旗舰店发货" android:textColor="#333333" android:textSize="15sp" /> </LinearLayout> </LinearLayout>item_shopingcart_child.xml:
<?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" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/white" android:orientation="horizontal" android:paddingBottom="15dp" android:paddingRight="15dp"> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:paddingLeft="15dp" android:paddingRight="17dp"> <CheckBox android:id="@+id/cb_cart_item_child" android:layout_width="24dp" android:layout_height="24dp" android:layout_gravity="center_vertical"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/divide_line" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:orientation="horizontal"> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/sdv_cart_item_child" android:layout_width="90dp" android:layout_height="90dp" app:placeholderImage="@mipmap/ic_launcher"/> <RelativeLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginLeft="15dp" android:layout_marginTop="8dp" android:layout_weight="1"> <TextView android:id="@+id/tv_cart_item_child_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:maxLines="1" android:ellipsize="end" android:text="" /> <TextView android:id="@+id/tv_cart_item_child_reduce" android:layout_width="30dp" android:layout_height="30dp" android:layout_below="@id/tv_cart_item_child_name" android:layout_marginTop="6dp" android:background="@drawable/selector_shopping_cart_subtract" android:gravity="center" android:text="-" android:textColor="@color/text_666666" android:textSize="15sp" /> <EditText android:id="@+id/et_cart_item_child_count" android:layout_width="49dp" android:layout_height="30dp" android:layout_alignTop="@+id/tv_cart_item_child_reduce" android:layout_toRightOf="@+id/tv_cart_item_child_reduce" android:layout_marginBottom="1dp" android:background="@drawable/bg_input_box" android:gravity="center" android:inputType="number" android:maxLength="6" android:text="1" android:textColor="@color/text_666666" android:textCursorDrawable="@null" android:textSize="12sp" /> <TextView android:id="@+id/tv_cart_item_child_add" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignTop="@+id/tv_cart_item_child_reduce" android:layout_toRightOf="@id/et_cart_item_child_count" android:background="@drawable/selector_shopping_cart_add" android:gravity="center" android:text="+" android:textColor="@color/text_666666" android:textSize="15sp" /> <TextView android:id="@+id/tv_cart_item_child_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/tv_cart_item_child_reduce" android:text="¥899" android:textColor="#FE3824" android:textSize="13sp" /> </RelativeLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="18dp" android:gravity="end" android:orientation="vertical"> </LinearLayout> <ImageView android:id="@+id/img_cart_item_child_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginTop="5dp" android:src="@drawable/rublish" /> </LinearLayout> </LinearLayout> </LinearLayout>colors.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="colorAccent">#FF4081</color> <color name = "main_grey" >#eeeeee</color > <color name="divide_line">#eeeeee</color> <color name = "text_666666" >#666666</color > </resources>src\main\res\drawable\selector_shopping_cart_add.xml:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/btn_shopping_cart_add_press" android:state_checked="true" /> <item android:drawable="@drawable/btn_shopping_cart_add_press" android:state_pressed="true" /> <item android:drawable="@drawable/btn_shopping_cart_add_press" android:state_selected="true" /> <item android:drawable="@drawable/btn_shopping_cart_add_normal" /> </selector>src\main\res\drawable\selector_shopping_cart_subtract.xml:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/btn_shopping_cart_subtract_press" android:state_checked="true" /> <item android:drawable="@drawable/btn_shopping_cart_subtract_press" android:state_pressed="true" /> <item android:drawable="@drawable/btn_shopping_cart_subtract_press" android:state_selected="true" /> <item android:drawable="@drawable/btn_shopping_cart_subtract_normal" /> </selector>CartBean:
public class CartBean { /** * msg : 请求成功 * code : 0 * data : [{"list":[{"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":1,"pid":1,"price":118,"pscid":1,"selected":0,"sellerid":17,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家17","sellerid":"17"},{"list":[{"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":1,"pid":2,"price":299,"pscid":1,"selected":0,"sellerid":18,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家18","sellerid":"18"},{"list":[{"bargainPrice":22.9,"createtime":"2017-10-03T23:53:28","detailUrl":"https://item.m.jd.com/product/2542855.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t1930/284/2865629620/390243/e3ade9c4/56f0a08fNbd3a1235.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2137/336/2802996626/155915/e5e90d7a/56f0a09cN33e01bd0.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t1882/31/2772215910/389956/c8dbf370/56f0a0a2Na0c86ea6.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t2620/166/2703833710/312660/531aa913/57709035N33857877.jpg!q70.jpg","num":1,"pid":44,"price":789,"pscid":2,"selected":0,"sellerid":21,"subhead":"三只松鼠零食特惠,专区满99减50,满199减100,火速抢购》","title":"三只松鼠 坚果炒货 零食奶油味 碧根果225g/袋"}],"sellerName":"商家21","sellerid":"21"}] */ private String msg; private String code; private int allCount; private double allMoney; private boolean isAllSelect; private List<DataBean> data; public boolean isAllSelect() { return isAllSelect; } public void setAllSelect(boolean allSelect) { isAllSelect = allSelect; } public double getAllMoney() { return allMoney; } public void setAllMoney(double allMoney) { this.allMoney = allMoney; } public int getAllCount() { return allCount; } public void setAllCount(int allCount) { this.allCount = allCount; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public List<DataBean> getData() { return data; } public void setData(List<DataBean> data) { this.data = data; } public static class DataBean { /** * list : [{"bargainPrice":111.99,"createtime":"2017-10-14T21:39:05","detailUrl":"https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg","num":1,"pid":1,"price":118,"pscid":1,"selected":0,"sellerid":17,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}] * sellerName : 商家17 * sellerid : 17 */ private String sellerName; private String sellerid; private boolean isSelected; private List<ListBean> list; public boolean isSelected() { return isSelected; } public void setSelected(boolean selected) { isSelected = selected; } public String getSellerName() { return sellerName; } public void setSellerName(String sellerName) { this.sellerName = sellerName; } public String getSellerid() { return sellerid; } public void setSellerid(String sellerid) { this.sellerid = sellerid; } public List<ListBean> getList() { return list; } public void setList(List<ListBean> list) { this.list = list; } public static class ListBean { /** * bargainPrice : 111.99 * createtime : 2017-10-14T21:39:05 * detailUrl : https://item.m.jd.com/product/4719303.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends * images : https://m.360buyimg.com/n0/jfs/t9004/210/1160833155/647627/ad6be059/59b4f4e1N9a2b1532.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7504/338/63721388/491286/f5957f53/598e95f1N7f2adb87.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t7441/10/64242474/419246/adb30a7d/598e95fbNd989ba0a.jpg!q70.jpg * num : 1 * pid : 1 * price : 118 * pscid : 1 * selected : 0 * sellerid : 17 * subhead : 每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下 * title : 北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g */ private double bargainPrice; private String createtime; private String detailUrl; private String images; private int num; private int pid; private double price; private int pscid; private int selected; private int sellerid; private String subhead; private String title; private boolean isSelected; public boolean isSelected() { return isSelected; } public void setSelected(boolean selected) { isSelected = selected; } public double getBargainPrice() { return bargainPrice; } public void setBargainPrice(double bargainPrice) { this.bargainPrice = bargainPrice; } public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } public String getDetailUrl() { return detailUrl; } public void setDetailUrl(String detailUrl) { this.detailUrl = detailUrl; } public String getImages() { return images; } public void setImages(String images) { this.images = images; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getPscid() { return pscid; } public void setPscid(int pscid) { this.pscid = pscid; } public int getSelected() { return selected; } public void setSelected(int selected) { this.selected = selected; } public int getSellerid() { return sellerid; } public void setSellerid(int sellerid) { this.sellerid = sellerid; } public String getSubhead() { return subhead; } public void setSubhead(String subhead) { this.subhead = subhead; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } } } }