首先是购物车的Fragment:
public class ShopCarFragment extends BaseFragment<CartPresenter> implements ICartView, CarttAdapter.CheckInterface,
CarttAdapter.ModifyCountInterface {
@BindView(R.id.back)
ImageView back;
@BindView(R.id.ex_ListView)
ExpandableListView exListView;
@BindView(R.id.chekbox_all)
CheckBox chekboxAll;
@BindView(R.id.price_all)
TextView priceAll;
@BindView(R.id.goto_pay)
TextView gotoPay;
Unbinder unbinder;
// 组元素数据列表
private List<StoreInfo> groups = new ArrayList<>();
// 子元素数据列表
private Map<String, List<GoodsInfo>> children = new HashMap<>();
// 购买的商品总价
private double totalPrice = 0.00;
// 购买的商品总数量
private int totalCount = 0;
private List<GoodsInfo> products;
private CarttAdapter selva;
@Override
protected void initView() {
}
@Override
protected int isLayoutId() {
return R.layout.fragment_shopcar;
}
@Override
protected BaseModel initModel() {
return new CartModel();
}
@Override
protected CartPresenter initPresenter() {
return new CartPresenter();
}
@Override
protected void initDate() {
presenter.select(71);
}
@Override
public void onSelect(SelectCartBean selectCartBean) {
List<SelectCartBean.DataBean> data = selectCartBean.getData();
for (int i = 0; i < data.size(); i++) {
groups.add(new StoreInfo(data.get(i).getSellerid(), data.get(i).getSellerName()));
products = new ArrayList<>();
for (int j = 0; j < data.get(i).getList().size(); j++) {
String images = data.get(i).getList().get(j).getImages();
String[] split = images.split("[|]");
String[] split1 = split[0].split("[!]");
List<SelectCartBean.DataBean.ListBean> list = data.get(i).getList();
products.add(new GoodsInfo(list.get(j).getPid() + "", "商品", list.get(j).getTitle(), Double.valueOf(list.get(j).getPrice() + ""), Integer.parseInt(list.get(j).getNum() + ""), "", "1", split1[0], Double.valueOf(list.get(j).getBargainPrice())));
}
// 将组元素的一个唯一值,这里取Id,作为子元素List的Key
children.put(groups.get(i).getId(), products);
}
selva = new CarttAdapter(groups, children, getContext());
selva.setCheckInterface(this);// 关键步骤1,设置复选框接口
selva.setModifyCountInterface(this);// 关键步骤2,设置数量增减接口
exListView.setAdapter(selva);
//默认展开
for (int i = 0; i < selva.getGroupCount(); i++) {
// 关键步骤3,初始化时,将ExpandableListView以展开的方式呈现
exListView.expandGroup(i);
}
}
@Override
public void onDelete(DeleteCartBean deleteCartBean) {
Toast.makeText(getContext(), deleteCartBean.getMsg(), Toast.LENGTH_SHORT).show();
}
@OnClick({R.id.back, R.id.chekbox_all, R.id.goto_pay})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.back:
break;
case R.id.chekbox_all:
doCheckAll();
break;
case R.id.goto_pay:
/**
* 支付提示
*/
AlertDialog alert = new AlertDialog.Builder(getContext()).create();
alert.setTitle("操作提示");
alert.setMessage("总计:\n" + totalCount + "种商品\n" + totalPrice + "元");
alert.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
return;
}
});
alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (totalCount == 0) {
Toast.makeText(getContext(), "请选择要支付的商品", Toast.LENGTH_LONG).show();
return;
}
}
});
alert.show();
break;
}
}
/**
* 全选反选
*/
private void doCheckAll() {
for (int i = 0; i < groups.size(); i++) {
groups.get(i).setChoosed(chekboxAll.isChecked());
StoreInfo group = groups.get(i);
List<GoodsInfo> childs = children.get(group.getId());
for (int j = 0; j < childs.size(); j++) {
childs.get(j).setChoosed(chekboxAll.isChecked());
}
}
//调用计算总数量与总价方法
calculate();
//刷新
selva.notifyDataSetChanged();
}
/**
* 计算总数量与总价
*/
private void calculate() {
totalCount = 0;
totalPrice = 0.00;
for (int i = 0; i < groups.size(); i++) {
StoreInfo group = groups.get(i);
List<GoodsInfo> childs = children.get(group.getId());
for (int j = 0; j < childs.size(); j++) {
GoodsInfo product = childs.get(j);
if (product.isChoosed()) {
totalCount++;
totalPrice += product.getDiscountPrice() * product.getCount();
}
}
}
priceAll.setText("¥" + totalPrice);
gotoPay.setText("结算(" + totalCount + ")");
}
/**
* 判断所有组选框状态是否全部选中
*
* @return
*/
private boolean isAllCheck() {
for (StoreInfo group : groups) {
if (!group.isChoosed())
return false;
}
return true;
}
/**
* 组选框状态改变触发的事件
*
* @param groupPosition 组元素位置
* @param isChecked 组元素选中与否
*/
@Override
public void checkGroup(int groupPosition, boolean isChecked) {
StoreInfo group = groups.get(groupPosition);
List<GoodsInfo> childs = children.get(group.getId());
for (int i = 0; i < childs.size(); i++) {
childs.get(i).setChoosed(isChecked);
}
if (isAllCheck())
chekboxAll.setChecked(true);
else
chekboxAll.setChecked(false);
selva.notifyDataSetChanged();
calculate();
}
/**
* 子选框状态改变时触发的事件
*
* @param groupPosition 组元素位置
* @param childPosition 子元素位置
* @param isChecked 子元素选中与否
*/
@Override
public void checkChild(int groupPosition, int childPosition, boolean isChecked) {
// 判断改组下面的所有子元素是否是同一种状态
boolean allChildSameState = true;
StoreInfo group = groups.get(groupPosition);
List<GoodsInfo> childs = children.get(group.getId());
for (int i = 0; i < childs.size(); i++) {
// 不全选中
if (childs.get(i).isChoosed() != isChecked) {
allChildSameState = false;
break;
}
}
//获取店铺选中商品的总金额
if (allChildSameState) {
// 如果所有子元素状态相同,那么对应的组元素被设为这种统一状态
group.setChoosed(isChecked);
} else {
// 否则,组元素一律设置为未选中状态
group.setChoosed(false);
}
if (isAllCheck()) {
// 全选
chekboxAll.setChecked(true);
} else {
// 反选
chekboxAll.setChecked(false);
}
selva.notifyDataSetChanged();
calculate();
}
/**
* 添加
*
*/
@Override
public void doIncrease(int groupPosition, int childPosition, View showCountView, boolean isChecked) {
GoodsInfo product = (GoodsInfo) selva.getChild(groupPosition, childPosition);
int currentCount = product.getCount();
currentCount++;
product.setCount(currentCount);
((TextView) showCountView).setText(currentCount + "");
calculate();
selva.notifyDataSetChanged();
}
/**
* 删除
*
*/
@Override
public void doDecrease(int groupPosition, int childPosition, View showCountView, boolean isChecked) {
GoodsInfo product = (GoodsInfo) selva.getChild(groupPosition, childPosition);
int currentCount = product.getCount();
if (currentCount == 1) {
Toast.makeText(getContext(), "数量不能少于1", Toast.LENGTH_SHORT).show();
return;
}
currentCount--;
product.setCount(currentCount);
((TextView) showCountView).setText(currentCount + "");
calculate();
selva.notifyDataSetChanged();
}
/**
* 删除Child
*
*/
@Override
public void childDelete(final int groupPosition, final int childPosition, int pid) {
children.get(groups.get(groupPosition).getId()).remove(childPosition);
StoreInfo group = groups.get(groupPosition);
List<GoodsInfo> childs = children.get(group.getId());
if (childs.size() == 0) {
presenter.delete(71, pid);
// Log.e("del----pid", pid + "");
groups.remove(groupPosition);
}
selva.notifyDataSetChanged();
}
}
model层:
public class CartModel extends BaseModel {
public void getSelect(int uid, final ICartModel iCartModel) {
RetrofitFactory.getINSTANCE().getHomeApi()
.getSelect(uid)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<SelectCartBean>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(SelectCartBean selectCartBean) {
iCartModel.onSelect(selectCartBean);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
public void getDelete(int uid, int pid, final ICartModel iCartModel) {
RetrofitFactory.getINSTANCE().getHomeApi()
.getDelete(uid, pid)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<DeleteCartBean>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(DeleteCartBean deleteCartBean) {
iCartModel.onDelete(deleteCartBean);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
public interface ICartModel {
void onSelect(SelectCartBean selectCartBean);
void onDelete(DeleteCartBean deleteCartBean);
}
}
presenter层:
public class CartPresenter extends BasePresenter<CartModel, ICartView> {
public void select(int uid) {
model.getSelect(uid, new CartModel.ICartModel() {
@Override
public void onSelect(SelectCartBean selectCartBean) {
view.onSelect(selectCartBean);
}
@Override
public void onDelete(DeleteCartBean deleteCartBean) {
}
});
}
public void delete(int uid, int pid) {
model.getDelete(uid, pid, new CartModel.ICartModel() {
@Override
public void onSelect(SelectCartBean selectCartBean) {
}
@Override
public void onDelete(DeleteCartBean deleteCartBean) {
view.onDelete(deleteCartBean);
}
});
}
}
view接口:
public interface ICartView extends IBaseView {
void onSelect(SelectCartBean selectCartBean);
void onDelete(DeleteCartBean deleteCartBean);
}
bean类
查询购物车:
public class SelectCartBean {
/**
* msg : 请求成功
* code : 0
* data : [{"list":[{"bargainPrice":111.99,"createtime":"2017-10-03T23:53:28","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":6,"price":7.99,"pscid":1,"selected":1,"sellerid":22,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家22","sellerid":"22"}]
*/
private String msg;
private String code;
private List<DataBean> data;
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-03T23:53:28","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":6,"price":7.99,"pscid":1,"selected":1,"sellerid":22,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}]
* sellerName : 商家22
* sellerid : 22
*/
private String sellerName;
private String sellerid;
private List<ListBean> list;
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-03T23:53:28
* 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 : 6
* price : 7.99
* pscid : 1
* selected : 1
* sellerid : 22
* 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;
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;
}
}
}
}
商家信息bean类
public class StoreInfo
{
protected String Id;
protected String name;
protected boolean isChoosed;
private boolean isEdtor;
public boolean isEdtor() {
return isEdtor;
}
public void setIsEdtor(boolean isEdtor) {
this.isEdtor = isEdtor;
}
public StoreInfo(String id, String name) {
Id = id;
this.name = name;
}
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isChoosed() {
return isChoosed;
}
public void setChoosed(boolean isChoosed) {
this.isChoosed = isChoosed;
}
}
商品信息bean类:
public class GoodsInfo {
protected String Id;
protected String name;
protected boolean isChoosed;
private String imageUrl;
private String desc;
private double price;
private int count;
private int position;// 绝对位置,只在ListView构造的购物车中,在删除时有效
private String color;
private String size;
private String goodsImg;
private double discountPrice;
public double getDiscountPrice() {
return discountPrice;
}
public void setDiscountPrice(double discountPrice) {
this.discountPrice = discountPrice;
}
public String getGoodsImg() {
return goodsImg;
}
public void setGoodsImg(String goodsImg) {
this.goodsImg = goodsImg;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public GoodsInfo(String id, String name, String desc, double price, int count, String color,
String size, String goodsImg, double discountPrice) {
this.Id = id;
this.name = name;
this.desc = desc;
this.price = price;
this.count = count;
this.color = color;
this.size = size;
this.goodsImg = goodsImg;
this.discountPrice = discountPrice;
}
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isChoosed() {
return isChoosed;
}
public void setChoosed(boolean isChoosed) {
this.isChoosed = isChoosed;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
}
删除bean类
public class DeleteCartBean {
/**
* msg : 购物车数据不存在
* code : 1
*/
private String msg;
private String code;
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;
}
}
购物车适配器 ExpandableListview实现
import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.StrikethroughSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import java.util.List;
import java.util.Map;
import bwie.com.R;
import bwie.com.shoppingcar.bean.GoodsInfo;
import bwie.com.shoppingcar.bean.StoreInfo;
public class CarttAdapter extends BaseExpandableListAdapter {
private List<StoreInfo> groups;
private Map<String, List<GoodsInfo>> children;
private Context context;
private CheckInterface checkInterface;
private ModifyCountInterface modifyCountInterface;
/**
* 构造函数
*
* @param groups 组元素列表
* @param children 子元素列表
* @param context
*/
public CarttAdapter(List<StoreInfo> groups, Map<String, List<GoodsInfo>> children, Context context) {
this.groups = groups;
this.children = children;
this.context = context;
}
public void setCheckInterface(CheckInterface checkInterface) {
this.checkInterface = checkInterface;
}
public void setModifyCountInterface(ModifyCountInterface modifyCountInterface) {
this.modifyCountInterface = modifyCountInterface;
}
@Override
public int getGroupCount() {
return groups.size();
}
@Override
public int getChildrenCount(int groupPosition) {
String groupId = groups.get(groupPosition).getId();
return children.get(groupId).size();
}
@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
List<GoodsInfo> childs = children.get(groups.get(groupPosition).getId());
return childs.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) {
final GroupViewHolder gholder;
if (convertView == null) {
gholder = new GroupViewHolder();
//父列表
convertView = View.inflate(context, R.layout.cart_group_item, null);
gholder.cb_check = (CheckBox) convertView.findViewById(R.id.determine_chekbox);
gholder.tv_group_name = (TextView) convertView.findViewById(R.id.tv_source_name);
convertView.setTag(gholder);
} else {
gholder = (GroupViewHolder) convertView.getTag();
}
final StoreInfo group = (StoreInfo) getGroup(groupPosition);
if (group != null) {
//商家名称
gholder.tv_group_name.setText(group.getName());
//商家复选框
gholder.cb_check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//复选框状态
group.setChoosed(((CheckBox) v).isChecked());
// 暴露组选接口
checkInterface.checkGroup(groupPosition, ((CheckBox) v).isChecked());
}
});
gholder.cb_check.setChecked(group.isChoosed());
} else {
groups.remove(groupPosition);
}
return convertView;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, final boolean isLastChild, View convertView, final ViewGroup parent) {
final ChildViewHolder cholder;
if (convertView == null) {
cholder = new ChildViewHolder();
//子列表
convertView = View.inflate(context, R.layout.cart_child_item, null);
cholder.cb_check = (CheckBox) convertView.findViewById(R.id.check_box);
cholder.tv_product_desc = (TextView) convertView.findViewById(R.id.tv_intro);
cholder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
cholder.iv_increase = (TextView) convertView.findViewById(R.id.tv_add);
cholder.iv_decrease = (TextView) convertView.findViewById(R.id.tv_reduce);
cholder.tv_count = (TextView) convertView.findViewById(R.id.tv_num);
cholder.rl_no_edtor = (RelativeLayout) convertView.findViewById(R.id.rl_no_edtor);
cholder.tv_discount_price = (TextView) convertView.findViewById(R.id.tv_discount_price);
cholder.tv_buy_num = (TextView) convertView.findViewById(R.id.tv_buy_num);
cholder.tv_goods_delete = (TextView) convertView.findViewById(R.id.tv_goods_delete);
cholder.iv_adapter_list_pic = (ImageView) convertView.findViewById(R.id.iv_adapter_list_pic);
convertView.setTag(cholder);
} else {
cholder = (ChildViewHolder) convertView.getTag();
}
final GoodsInfo goodsInfo = (GoodsInfo) getChild(groupPosition, childPosition);
if (goodsInfo != null) {
cholder.tv_product_desc.setText(goodsInfo.getDesc());
cholder.tv_price.setText("¥" + goodsInfo.getDiscountPrice() + "");
cholder.tv_count.setText(goodsInfo.getCount() + "");
Glide.with(context).load(goodsInfo.getGoodsImg()).into(cholder.iv_adapter_list_pic);
//给原价加横线
SpannableString spanString = new SpannableString("¥" + String.valueOf(goodsInfo.getPrice()));
StrikethroughSpan span = new StrikethroughSpan();
spanString.setSpan(span, 0, String.valueOf(goodsInfo.getPrice()).length() + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
cholder.tv_discount_price.setText(spanString);
cholder.tv_buy_num.setText("x" + goodsInfo.getCount());
cholder.cb_check.setChecked(goodsInfo.isChoosed());
//商品复选框点击事件
cholder.cb_check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//复选框状态
goodsInfo.setChoosed(((CheckBox) v).isChecked());
cholder.cb_check.setChecked(((CheckBox) v).isChecked());
// 暴露子选接口
checkInterface.checkChild(groupPosition, childPosition, ((CheckBox) v).isChecked());
}
});
//增加
cholder.iv_increase.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 暴露增加接口
modifyCountInterface.doIncrease(groupPosition, childPosition, cholder.tv_count, cholder.cb_check.isChecked());
}
});
//删减
cholder.iv_decrease.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 暴露删减接口
modifyCountInterface.doDecrease(groupPosition, childPosition, cholder.tv_count, cholder.cb_check.isChecked());
}
});
//删除 购物车
cholder.tv_goods_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
modifyCountInterface.childDelete(groupPosition, childPosition, Integer.valueOf(goodsInfo.getId()));
}
});
}
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
/**
* 组元素绑定器
*/
private class GroupViewHolder {
CheckBox cb_check;
TextView tv_group_name;
}
/**
* 子元素绑定器
*/
private class ChildViewHolder {
CheckBox cb_check;
ImageView iv_adapter_list_pic;
TextView tv_product_desc;
TextView tv_price;
TextView iv_increase;
TextView tv_count;
TextView iv_decrease;
RelativeLayout rl_no_edtor;
TextView tv_discount_price;
TextView tv_buy_num;
TextView tv_goods_delete;
}
/**
* 复选框接口
*/
public interface CheckInterface {
/**
* 组选框状态改变触发的事件
*
* @param groupPosition 组元素位置
* @param isChecked 组元素选中与否
*/
public void checkGroup(int groupPosition, boolean isChecked);
/**
* 子选框状态改变时触发的事件
*
* @param groupPosition 组元素位置
* @param childPosition 子元素位置
* @param isChecked 子元素选中与否
*/
public void checkChild(int groupPosition, int childPosition, boolean isChecked);
}
/**
* 改变数量的接口
*/
public interface ModifyCountInterface {
/**
* 增加操作
*
* @param groupPosition 组元素位置
* @param childPosition 子元素位置
* @param showCountView 用于展示变化后数量的View
* @param isChecked 子元素选中与否
*/
public void doIncrease(int groupPosition, int childPosition, View showCountView, boolean isChecked);
/**
* 删减操作
*
* @param groupPosition 组元素位置
* @param childPosition 子元素位置
* @param showCountView 用于展示变化后数量的View
* @param isChecked 子元素选中与否
*/
public void doDecrease(int groupPosition, int childPosition, View showCountView, boolean isChecked);
/**
* 删除子item
*
* @param groupPosition
* @param childPosition
*/
public void childDelete(int groupPosition, int childPosition, int pid);
}
}
最后附上github地址
https://github.com/Wittman0980/New_AndroidJD_06
ps:萌新勿喷..