public class GetCartBean {
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 {
private boolean checked;//自己添加的
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
private String sellerName;
private String sellerid;
private List 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 getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public static class ListBean {
private boolean checked;//自己添加的
private int count = 1;//自己添加的
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
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;
}
}
}
}
callback
public interface CarCallBack {
void onSuccess(List group, List> child);
void onFailed(String error);
}
//用于计算价钱和总数的bean放在mvp里
public class PriceAndCount {
private double price;
private int count;
public PriceAndCount(double price, int count) {
this.price = price;
this.count = count;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
view层
public interface CarView {
void onCarSuccess(List group, List> child);
void onCarFailed(String error);
}
model层
public class CarModel {
public void carData(List group, List> child, int pid, final CarCallBack carCallBack) {
new OkHttpUtil().OkHttpGet(Apis.CAR_URL + pid).setOkHttpListener(new OkHttpUtil.OkHttpListener() {
@Override
public void OkHttpSuccess(String data) {
Gson gson = new Gson();
GetCartBean getCartBean = gson.fromJson(data, GetCartBean.class);
List dataBeans = getCartBean.getData();
List> lists = new ArrayList<>();
for (int i = 0; i < dataBeans.size(); i++){
lists.add(dataBeans.get(i).getList());
}
if (getCartBean.getCode().equals("0")) {
carCallBack.onSuccess(dataBeans, lists);
} else {
carCallBack.onFailed(getCartBean.getMsg());
}
}
@Override
public void OkHttpError(String error) {
carCallBack.onFailed(error);
}
});
}
}
presenter层
public class CarPresenter {
private CarView mICarView;
private CarModel mCarModel;
public CarPresenter(CarView ICarView) {
mICarView = ICarView;
mCarModel = new CarModel();
}
public void datechView() {
mICarView = null;
}
public void carDatas(List group, List> child, int pid) {
mCarModel.carData(group, child, pid, new CarCallBack() {
@Override
public void onSuccess(List group, List> child) {
mICarView.onCarSuccess(group, child);
}
@Override
public void onFailed(String error) {
mICarView.onCarFailed(error);
}
});
}
}
ExpandableListView适配器
public class ElvAdapter extends BaseExpandableListAdapter {
private final LayoutInflater inflater;
private Context context;
private List group;
private List> child;
public ElvAdapter(Context context, List group, List> child) {
this.context=context;
this.group=group;
this.child=child;
inflater = LayoutInflater.from(context);
}
@Override
public int getGroupCount() {
return group.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return child.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return group.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return child.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) {
View view;
final GroupViewHolder holder;
if (convertView == null) {
view = inflater.inflate(R.layout.elv_group, null);
holder = new GroupViewHolder();
holder.tv = view.findViewById(R.id.tvGroup);
holder.cbGroup = view.findViewById(R.id.cbGroup);
view.setTag(holder);
} else {
view = convertView;
holder = (GroupViewHolder) view.getTag();
}
final GetCartBean.DataBean dataBean = group.get(groupPosition);
holder.tv.setText(dataBean.getSellerName());
holder.cbGroup.setChecked(dataBean.isChecked());
holder.cbGroup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//需要改变三个checkbox的状态值
//1.一级列表的checkbox状态值
dataBean.setChecked(holder.cbGroup.isChecked());
//2.二级列表的checkbox状态值
setChildrenCb(groupPosition, holder.cbGroup.isChecked());
//3.全选的checkbox状态值
((CarActivity) context).setAllChecked(isAllGroupCbChecked());
//计算钱和数量并显示
setPriceAndCount();
//刷新界面
notifyDataSetChanged();
}
});
return view;
}
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, final View convertView, ViewGroup parent) {
View view;
final ChildViewHolder holder;
if (convertView == null) {
view = inflater.inflate(R.layout.elv_child, null);
holder = new ChildViewHolder();
holder.iv = view.findViewById(R.id.iv);
holder.tvTitle = view.findViewById(R.id.tvTitle);
holder.tvSubhead = view.findViewById(R.id.tvSubhead);
holder.tvPrice = view.findViewById(R.id.tvPrice);
holder.cbChild = view.findViewById(R.id.cbChild);
holder.btDel = view.findViewById(R.id.btDel);
holder.tvNum = view.findViewById(R.id.tvNum);
holder.ivDel = view.findViewById(R.id.ivDel);
holder.ivAdd = view.findViewById(R.id.ivAdd);
view.setTag(holder);
} else {
view = convertView;
holder = (ChildViewHolder) view.getTag();
}
final GetCartBean.DataBean.ListBean listBean = child.get(groupPosition).get(childPosition);
String images = listBean.getImages();
Glide.with(context).load(images.split("\\|")[0]).into(holder.iv);
holder.tvTitle.setText(listBean.getTitle());
holder.cbChild.setChecked(child.get(groupPosition).get(childPosition).isChecked());
holder.tvSubhead.setText(listBean.getSubhead());
holder.tvPrice.setText("¥"+listBean.getPrice());
holder.tvNum.setText(listBean.getCount() + "");
//给checkbox设置点击事件
holder.cbChild.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//需要改变三个checkbox的状态值
//1.二级列表的checkbox状态值
listBean.setChecked(holder.cbChild.isChecked());
//2.一级列表的checkbox状态值
group.get(groupPosition).setChecked(isAllChildCbChecked(groupPosition));
//3.全选的checkbox状态值
((CarActivity) context).setAllChecked(isAllGroupCbChecked());
//计算钱和数量并显示
setPriceAndCount();
//刷新界面
notifyDataSetChanged();
}
});
holder.ivAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取目前显示的值
int count = listBean.getCount();
count++;
//改变JavaBean里的状态值
listBean.setCount(count);
//计算钱和数量并显示
setPriceAndCount();
//刷新列表
notifyDataSetChanged();
}
});
holder.ivDel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取目前显示的值
int count = listBean.getCount();
if (count <= 1) {
count = 1;
} else {
count--;
}
//改变JavaBean里的状态值
listBean.setCount(count);
//计算钱和数量并显示
setPriceAndCount();
//刷新列表
notifyDataSetChanged();
}
});
holder.btDel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//其实就是删除集合
List listBeans = child.get(groupPosition);
if (listBeans.size() > 0) {
listBeans.remove(childPosition);
}
if (listBeans.size() == 0) {
child.remove(groupPosition);
group.remove(groupPosition);
}
//计算钱和数量并显示
setPriceAndCount();
//改变全选状态
((CarActivity) context).setAllChecked(isAllGroupCbChecked());
//刷新列表
notifyDataSetChanged();
}
});
return view;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
class GroupViewHolder {
TextView tv;
CheckBox cbGroup;
}
class ChildViewHolder {
ImageView iv;
TextView tvTitle;
TextView tvSubhead;
TextView tvPrice;
CheckBox cbChild;
Button btDel;
TextView tvNum;
ImageView ivDel;
ImageView ivAdd;
}
/**
* 设置一级列表对应的二级列表checkbox状态
*
* @param groupPosition
* @param bool
*/
private void setChildrenCb(int groupPosition, boolean bool) {
List listBeans = child.get(groupPosition);
for (int i = 0; i < listBeans.size(); i++) {
listBeans.get(i).setChecked(bool);
}
}
/**
* 判断一级列表checkbox状态
*
* @return
*/
private boolean isAllGroupCbChecked() {
if (group.size() == 0) {
return false;
}
for (int i = 0; i < group.size(); i++) {
if (!group.get(i).isChecked()) {
return false;
}
}
return true;
}
/**
* 判断二级列表checkbox状态
*
* @return
*/
private boolean isAllChildCbChecked(int groupPosition) {
List listBeans = child.get(groupPosition);
for (int i = 0; i < listBeans.size(); i++) {
if (!listBeans.get(i).isChecked()) {
return false;
}
}
return true;
}
/**
* 设置钱和数量
*/
private void setPriceAndCount() {
((CarActivity) context).setPriceAndCount(compute());
}
/**
* 计算钱和数量
*/
private PriceAndCount compute() {
double price = 0;
int count = 0;
for (int i = 0; i < group.size(); i++) {
List listBeans = child.get(i);
for (int j = 0; j < listBeans.size(); j++) {
if (listBeans.get(j).isChecked()) {
price += listBeans.get(j).getPrice() * listBeans.get(j).getCount();
count += listBeans.get(j).getCount();
}
}
}
return new PriceAndCount(price, count);
}
/**
* 全选或者全不选
*
* @param bool
*/
public void AllOrNone(boolean bool) {
for (int i = 0; i < group.size(); i++) {
group.get(i).setChecked(bool);
setChildrenCb(i, bool);
}
setPriceAndCount();
notifyDataSetChanged();
}
}
购物车的Activity
public class CarActivity extends AppCompatActivity implements CarView {
private ImageView mBack;
private LinearLayout mTopBar;
private TextView mEdit;
private ExpandableListView mElv;
private CheckBox mCb;
private TextView mTvTotal;
private TextView mTvCount;
private ElvAdapter elvAdapter;
private CarPresenter mCarPresenter;
private List mDataBeans;
private List> mLists;
private int mPid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_car);
mPid = getIntent().getIntExtra("pid", 1);
initView();
}
private void initView() {
mBack = (ImageView) findViewById(R.id.back);
mEdit = (TextView) findViewById(R.id.edit);
mTopBar = (LinearLayout) findViewById(R.id.top_bar);
mElv = (ExpandableListView) findViewById(R.id.elv);
mCb = (CheckBox) findViewById(R.id.cb);
mTvTotal = (TextView) findViewById(R.id.tvTotal);
mTvCount = (TextView) findViewById(R.id.tvCount);
mCarPresenter = new CarPresenter(this);
mCarPresenter.carDatas(mDataBeans,mLists,mPid);
}
@Override
public void onCarSuccess(List group, List> child) {
mDataBeans = group;
mLists = child;
for (int i = 0; i < group.size(); i++) {
List list = group.get(i).getList();
if (list.size() == 0) {
group.remove(i);
child.remove(i);
}
}
elvAdapter = new ElvAdapter(this,group,child);
mElv.setGroupIndicator(null);
mElv.setAdapter(elvAdapter);
for (int i = 0; i < group.size(); i++) {
mElv.expandGroup(i);
}
}
@Override
public void onCarFailed(String error) {
}
public void setPriceAndCount(PriceAndCount priceAndCount) {
mTvTotal.setText("合计:"+"¥" + priceAndCount.getPrice());
mTvCount.setText("去结算(" + priceAndCount.getCount() + ")");
}
public void setAllChecked(boolean bool) {
mCb.setChecked(bool);
}
}
布局文件
activity主布局
商家布局
商家子布局