一.在购物车中主要控制选择按钮,编辑按钮,加减按钮,删除按钮(不暂时增加商品和结算功能)
在适配器中与主页交互使用EventBus传值
导入EventBus的依赖
compile 'org.greenrobot:eventbus:3.1.1'
其中有关于EventBus的文件
一
public class MessageEvent {
private boolean checkd;
public boolean isCheckd(){
return checkd;
}
public void setCheckd(boolean checkd){
this.checkd=checkd;
}
}
二
public class PriceAndCountEvent {
private double price;
private int count;
private int to;
public double getPrice() {
return price;
}
public int getCount() {
return count;
}
public int getTo(){
return to;
}
public void setPrice(double price) {
this.price = price;
}
public void setCount(int count) {
this.count = count;
}
public void setTo(int to) {
this.to = to;
}
}
二.购物车的适配器,其中有删除,和修改数量的接口实现(数据操作不展示)
public class MyCarShowAdapter extends BaseExpandableListAdapter implements IUpdateGoodsView,IDeleteGoodsView{
private Context context;
private List data;
private List> list;
private boolean isVisible;
private int uid;
private UpdateGoodsPresenter updateGoodsPresenter;
private DeleteGoodsPresenter deleteGoodsPresenter;
public MyCarShowAdapter(Context context, List data, List> list) {
this.context = context;
this.data = data;
this.list = list;
}
@Override
public int getGroupCount() {
return data.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return list.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return data.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return list.get(groupPosition).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 ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = View.inflate(context, R.layout.group1, null);
holder.textView = (TextView) convertView.findViewById(R.id.g_title);
holder.che = (CheckBox) convertView.findViewById(R.id.che);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(data.get(groupPosition).getSellerName());
//设置一级列表checkBox的状态
holder.che.setChecked(data.get(groupPosition).isChecked());
//一级列表checkBox的点击事件
holder.che.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//判断一级列表复选框的状态 设置为true或false
data.get(groupPosition).setChecked(holder.che.isChecked());
//改变二级checkbod的状态
changeChildCbState(groupPosition, holder.che.isChecked());
//算钱
EventBus.getDefault().post(computer());
//改变全选状态 isAllGroupCbSelect判断一级是否全部选中
changeAllCbState(isAllGroupCbSelect());
//必刷新
notifyDataSetChanged();
}
});
return convertView;
}
//二级标题
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
Fresco.initialize(context);
final SelectCarBean.DataBean.ListBean listBean = list.get(groupPosition).get(childPosition);
final ViewHolder2 holder2;
if (convertView == null) {
holder2 = new ViewHolder2();
convertView = View.inflate(context, R.layout.chidren, null);
holder2.z_title = (TextView) convertView.findViewById(R.id.z_title);
holder2.z_che = (CheckBox) convertView.findViewById(R.id.z_che);
holder2.img = (SimpleDraweeView) convertView.findViewById(R.id.z_img);
holder2.price = (TextView) convertView.findViewById(R.id.z_price);
holder2.xiangqing = (TextView) convertView.findViewById(R.id.z_shuxing);
holder2.jian = (ImageView) convertView.findViewById(R.id.iv_jian);
holder2.jia = (ImageView) convertView.findViewById(R.id.iv_add);
holder2.del = (ImageView) convertView.findViewById(R.id.del);
holder2.num = (TextView) convertView.findViewById(R.id.tv_num);
convertView.setTag(holder2);
} else {
holder2 = (ViewHolder2) convertView.getTag();
}
holder2.num.setText(list.get(groupPosition).get(childPosition).getNum() + "");
holder2.z_title.setText(list.get(groupPosition).get(childPosition).getTitle());
holder2.price.setText("¥" + list.get(groupPosition).get(childPosition).getBargainPrice());
holder2.xiangqing.setText(list.get(groupPosition).get(childPosition).getSubhead());
String images = list.get(groupPosition).get(childPosition).getImages();
String[] split = images.split("\\|");
// ImageLoader.getInstance().displayImage(split[0], holder2.img);
Uri parse = Uri.parse(split[0]);
holder2.img.setImageURI(parse);
//判断是否需要显示删除按钮
if (!isVisible) {
//隐藏
holder2.del.setVisibility(View.GONE);
} else {
holder2.del.setVisibility(View.VISIBLE);
}
//设置二级列表checkbox的属性
holder2.z_che.setChecked(list.get(groupPosition).get(childPosition).isChecked());
//二级列表的点击事件
holder2.z_che.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//设置该条目中的checkbox属性值
listBean.setChecked(holder2.z_che.isChecked());
//计算价钱
PriceAndCountEvent priceAndCountEvent = computer();
EventBus.getDefault().post(priceAndCountEvent);
//判断当前checkbox是选中的状态
if (holder2.z_che.isChecked()) {
//如果全部选中(isAllChildCbSelected)
if (isAllChildCbSelected(groupPosition)) {
//改变一级列表的状态
changeGroupCbState(groupPosition, true);
//改变全选的状态
changeAllCbState(isAllGroupCbSelect());
}
} else {
//如果没有全部选中,一级列表的checkbox为false不为选中
changeGroupCbState(groupPosition, false);
changeAllCbState(isAllGroupCbSelect());
}
notifyDataSetChanged();
}
});
updateGoodsPresenter = new UpdateGoodsPresenter(this);
this.uid = MApp.sp.getInt("uid", 0);
//加号
holder2.jia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int num = listBean.getNum();
//num为int类型所以要加空字符串
holder2.num.setText(++num + "");
listBean.setNum(num);
updateGoodsPresenter.updateGoods(uid+"",listBean.getPid()+"",listBean.getSellerid()+"",listBean.getSelected()+"",num+"");
//如果二级列表的checkbox为选中,计算价钱
if (holder2.z_che.isChecked()) {
PriceAndCountEvent priceAndCountEvent = computer();
EventBus.getDefault().post(priceAndCountEvent);
}
}
});
//减号
holder2.jian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int num = listBean.getNum();
if (num == 1) {
Toast.makeText(context, "已经不能再减了,", Toast.LENGTH_SHORT).show();
return;
}
holder2.num.setText(--num + "");
listBean.setNum(num);
updateGoodsPresenter.updateGoods(uid+"",listBean.getPid()+"",listBean.getSellerid()+"",listBean.getSelected()+"",num+"");
if (holder2.z_che.isChecked()) {
PriceAndCountEvent priceAndCountEvent = computer();
EventBus.getDefault().post(priceAndCountEvent);
}
}
});
deleteGoodsPresenter = new DeleteGoodsPresenter(this);
//点击删除购物车里面的内容
holder2.del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (holder2.z_che.isChecked()) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("确认要删除吗?");
builder.setTitle("提示");
builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
deleteGoodsPresenter.deleteGoods(uid+"",list.get(groupPosition).get(childPosition).getPid() + "");
Toast.makeText(context, "删除成功", Toast.LENGTH_SHORT).show();
List listBeen = list.get(groupPosition);
SelectCarBean.DataBean.ListBean remove = listBeen.remove(childPosition);
if (listBeen.size() == 0) {
//先移除二级列表的集合,再移除一级列表的集合
list.remove(groupPosition);
data.remove(groupPosition);
}
//算钱
EventBus.getDefault().post(computer());
notifyDataSetChanged();
dialog.dismiss();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
//点击传值
} else {
Toast.makeText(context, "请选择商品", Toast.LENGTH_SHORT).show();
}
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
@Override
public void onSuccess(UpdateGoodsBean updateGoodsBean) {
Log.e("Update",updateGoodsBean.getMsg());
}
@Override
public void onSuccess(DeleteGoodsBean deleteGoodsBean) {
}
@Override
public void onFailed(Throwable throwable) {
Log.e("Update",throwable.toString());
}
class ViewHolder {
TextView textView;
CheckBox che;
}
class ViewHolder2 {
SimpleDraweeView img;
TextView z_title;
CheckBox z_che;
TextView price;
TextView xiangqing;
ImageView jia;
ImageView jian;
ImageView del;
TextView num;
}
//改变二级列表的checkbox的状态 如果一级选中,控制二级也选中
private void changeChildCbState(int groupPosition, boolean flag) {
List listBeen = list.get(groupPosition);
for (int j = 0; j < listBeen.size(); j++) {
SelectCarBean.DataBean.ListBean listBean = listBeen.get(j);
listBean.setChecked(flag);
}
}
//判断一级列表是否全部选中
public boolean isAllGroupCbSelect() {
for (int i = 0; i < list.size(); i++) {
SelectCarBean.DataBean dataBean = data.get(i);
if (!dataBean.isChecked()) {
return false;
}
}
return true;
}
//改变全选的状态
private void changeAllCbState(boolean flag) {
MessageEvent messageEvent = new MessageEvent();
messageEvent.setCheckd(flag);
EventBus.getDefault().post(messageEvent);
}
//改变一级列表的checkbox的状态
private void changeGroupCbState(int i, boolean flag) {
SelectCarBean.DataBean dataBean = data.get(i);
dataBean.setChecked(flag);
}
//判断二级列表是否全部选中
private boolean isAllChildCbSelected(int i) {
List listBeen = list.get(i);
for (int j = 0; j < listBeen.size(); j++) {
SelectCarBean.DataBean.ListBean listBean = listBeen.get(j);
if (!listBean.isChecked()) {
return false;
}
}
return true;
}
//设置全选,反选
public void changeAllListCbState(boolean flag) {
for (int i = 0; i < list.size(); i++) {
changeGroupCbState(i, flag);
changeChildCbState(i, flag);
}
//算钱
EventBus.getDefault().post(computer());
notifyDataSetChanged();
}
//计算列表的价钱
private PriceAndCountEvent computer() {
int count = 0;
double price = 0;
int to = 0;
for (int i = 0; i < list.size(); i++) {
List listBeen = list.get(i);
for (int j = 0; j < listBeen.size(); j++) {
SelectCarBean.DataBean.ListBean listBean = listBeen.get(j);
if (listBean.isChecked()) {
price += listBean.getNum() * listBean.getBargainPrice();
count += listBean.getNum();
to += listBean.getNum();
}
}
}
PriceAndCountEvent priceAndCountEvent = new PriceAndCountEvent();
priceAndCountEvent.setCount(count);
priceAndCountEvent.setPrice(price);
priceAndCountEvent.setTo(to);
return priceAndCountEvent;
}
/**
* 是否隐藏删除按钮
*
* @param isVisible
* @return
*/
public void showDelete(boolean isVisible) {
this.isVisible = isVisible;
notifyDataSetChanged();
}
}
三. 购物车页面(多数的App购物车都走Fragment中,所有此处用Fragment展示)
public class ShoppingCartFragment extends Fragment implements ISelectCarView {
@BindView(R.id.btnBack)
TextView btnBack;
@BindView(R.id.btnEditor)
TextView btnEditor;
@BindView(R.id.exlv)
ExpandableListView exlv;
@BindView(R.id.checkbox)
CheckBox checkbox;
@BindView(R.id.price)
TextView price;
@BindView(num)
TextView nums;
@BindView(R.id.jiesuan)
Button jiesuan;
Unbinder unbinder;
@BindView(wei_login)
Button weiLogin;
@BindView(wei_text)
TextView weiText;
@BindView(R.id.qx)
TextView qx;
@BindView(R.id.rl)
RelativeLayout rl;
//定义属性
private boolean flag;
private View view;
private boolean isLogin;
private SelectCarPresenter selectCarPresenter;
private List grouplist = new ArrayList<>();
private List> childlist = new ArrayList<>();
private MyCarShowAdapter myCarShowAdapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_shoppingcar, container, false);
EventBus.getDefault().register(this);
Unbinder bind = ButterKnife.bind(this, view);
unbinder = ButterKnife.bind(this, view);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
selectCarPresenter = new SelectCarPresenter(this);
//登录判断
login();
}
/**
* 判断是否登录
*/
private void login() {
isLogin = MApp.sp.getBoolean("isLogin", false);
if (isLogin){
int uid = MApp.sp.getInt("uid", 0);
selectCarPresenter.selectCar(uid+"");
weiLogin.setVisibility(View.GONE);
rl.setVisibility(View.VISIBLE);
exlv.setVisibility(View.VISIBLE);
}else{
weiLogin.setVisibility(View.VISIBLE);
weiText.setText("您还没有登录,请登录查看您的购物车!");
weiText.setVisibility(View.VISIBLE);
rl.setVisibility(View.GONE);
exlv.setVisibility(View.GONE);
}
}
/**
* 请求失败
*
* @param throwable
*/
@Override
public void onFailed(Throwable throwable) {
}
/**
* 请求成功
*
* @param selectCarBean
*/
@Override
public void onSuccess(SelectCarBean selectCarBean) {
List data = selectCarBean.getData();
if (data.size()==0){
weiText.setText("购物车为空");
}else{
weiText.setVisibility(View.GONE);
grouplist.clear();
childlist.clear();
grouplist.addAll(data);
for (int i = 0; i < data.size(); i++) {
List list = data.get(i).getList();
childlist.add(list);
}
myCarShowAdapter = new MyCarShowAdapter(getContext(), grouplist, childlist);
exlv.setAdapter(myCarShowAdapter);
exlv.setGroupIndicator(null);
// 展开
for (int i = 0; i < data.size(); i++) {
exlv.expandGroup(i);
}
myCarShowAdapter.notifyDataSetChanged();
}
}
/**
* 销毁fragment
*/
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
selectCarPresenter.unBind();
}
@OnClick({R.id.btnBack, R.id.btnEditor, R.id.checkbox, R.id.jiesuan})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.btnBack:
break;
case R.id.btnEditor://编辑按钮
if (!flag) {// false 隐藏 true 代表显示
//隐藏
flag = true;
myCarShowAdapter.showDelete(flag);
btnEditor.setText("完成");
} else {
//显示
flag = false;
myCarShowAdapter.showDelete(flag);
btnEditor.setText("编辑");
}
break;
case R.id.checkbox:
if (grouplist.size() <= 0 || childlist.size() <= 0) {
checkbox.setChecked(false);
} else {
//设置全选
myCarShowAdapter.changeAllListCbState(checkbox.isChecked());
}
break;
case R.id.jiesuan:
break;
}
}
@OnClick(wei_login)
public void onViewClicked() {
}
@Override
public void onResume() {
super.onResume();
login();
}
@Subscribe
public void onMessageEvent(MessageEvent event) {
checkbox.setChecked(event.isCheckd());
}
@Subscribe
public void onMessageEven(PriceAndCountEvent event) {
price.setText("¥"+event.getPrice());
nums.setText("共" + event.getCount() + "件商品");
jiesuan.setText("去结算(" + event.getTo() + ")");
}
}
四.导入布局文件child
group
主页的布局
示例中用到的bean
public class SelectCarBean {
/**
* msg : 请求成功
* code : 0
* data : [{"list":[{"bargainPrice":11800,"createtime":"2017-10-03T23:53:28","detailUrl":"https://mitem.jd.hk/ware/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1","images":"https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5815/178/2614671118/51656/7f52d137/593276c7N107b725a.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5878/60/2557817477/30873/4502b606/593276caN5a7d6357.jpg!q70.jpg","num":1,"pid":71,"price":32999,"pscid":40,"selected":0,"sellerid":15,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}],"sellerName":"商家15","sellerid":"15"}]
*/
private String msg;
private String code;
private List 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 getData() {
return data;
}
public void setData(List data) {
this.data = data;
}
public static class DataBean {
/**
* list : [{"bargainPrice":11800,"createtime":"2017-10-03T23:53:28","detailUrl":"https://mitem.jd.hk/ware/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1","images":"https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5815/178/2614671118/51656/7f52d137/593276c7N107b725a.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5878/60/2557817477/30873/4502b606/593276caN5a7d6357.jpg!q70.jpg","num":1,"pid":71,"price":32999,"pscid":40,"selected":0,"sellerid":15,"subhead":"购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)","title":"全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G"}]
* sellerName : 商家15
* sellerid : 15
*/
private String sellerName;
private String sellerid;
private List list;
private boolean checked;
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
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 getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public static class ListBean {
/**
* bargainPrice : 11800.0
* createtime : 2017-10-03T23:53:28
* detailUrl : https://mitem.jd.hk/ware/view.action?wareId=1988853309&cachekey=1acb07a701ece8d2434a6ae7fa6870a1
* images : https://m.360buyimg.com/n0/jfs/t6130/97/1370670410/180682/1109582a/593276b1Nd81fe723.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5698/110/2617517836/202970/c9388feb/593276b7Nbd94ef1f.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5815/178/2614671118/51656/7f52d137/593276c7N107b725a.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t5878/60/2557817477/30873/4502b606/593276caN5a7d6357.jpg!q70.jpg
* num : 1
* pid : 71
* price : 32999.0
* pscid : 40
* selected : 0
* sellerid : 15
* subhead : 购买电脑办公部分商品满1元返火车票5元优惠券(返完即止)
* title : 全球购 新款Apple MacBook Pro 苹果笔记本电脑 银色VP2新13英寸Bar i5/8G/256G
*/
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 checked;
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
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;
}
}
}
}
布局中会用一些图片文件