您的位置:首页 > 其它

系统的MVP购物车(二级购物车全)

2017-12-19 14:14 316 查看
看一下效果,动态图太麻烦了  简单截了一张

加减商品也做了

首先依赖和权限:‘

依赖

testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.squareup.okio:okio:1.5.0'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
compile 'com.google.code.gson:gson:2.8.2'
compile 'com.android.support:design:26.+'
compile 'com.jcodecraeer:xrecyclerview:1.3.2'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
compile 'com.hjm:BottomTabBar:1.1.1'
compile 'com.youth.banner:banner:1.4.9'
compile 'com.xhb:xbanner:1.3.0'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'jp.wasabeef:glide-transformations:1.0.6'
compile 'cn.yipianfengye.android:zxing-library:2.1'
testCompile 'junit:junit:4.12'
compile 'org.greenrobot:eventbus:3.1.1'


权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<!-- 网络状态 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
MVP我思路是从molde开始的
Model层的接口
public interface Model_Cart {
void getFinsh();
}
Model层的实现类
public class MyMolde_Cart implements Model_Cart {

OnFinsh onFinsh;

public interface OnFinsh{
void Finsh1(MyCartBean bean);
}

public void setOnFinsh( OnFinsh onFinsh){
this.onFinsh = onFinsh;
}

@Override
public void getFinsh() {
OkHttp3Utils.doGet(RegApi.SELECT_CAR, new GsonObjectCallback<MyCartBean>() {

@Override
public void onUi(MyCartBean myCartBean) {
Log.e("TAG", LoginActivity.uid+"我是从登录来的");
onFinsh.Finsh1(myCartBean);
}

@Override
public void onFailed(Call call, IOException e) {

}
});

}
}
Presenter层的实现类
public class MyPresenter_Cart {
View_Cart view;
MyMolde_Cart model;

public MyPresenter_Cart(View_Cart view) {
this.view = view;
model = new MyMolde_Cart();
}

public void Action(){
model.getFinsh();
model.setOnFinsh(new MyMolde_Cart.OnFinsh() {
@Override
public void Finsh1(MyCartBean bean) {
view.getData_SelectCart(bean);
}
});
}
}

View层的接口
public interface View_Cart {
void getData_SelectCart(MyCartBean bean);
}

View层的视图可以是Fragment也可以是Activity
我这里是Fragment
public class My_Fragment extends Fragment implements View_Cart{
MyPresenter_Cart myPresenter_cart;
private TextView tv_num;
private TextView tv_price;
private CheckBox checkBox;
private ExpandableListView elv;
private View view;
private MyCartAdapter myCartAdapter;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.my,
container, false);
myPresenter_cart = new MyPresenter_Cart(this);
myPresenter_cart.Action();
initeView();
checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(checkBox.isChecked()){
myCartAdapter.allChecked(true);
myCartAdapter.AllPrice();
}else {
myCartAdapter.allChecked(false);
myCartAdapter.AllPrice();
}
}
});

return view;
}

private void initeView() {
elv = (ExpandableListView) view.findViewById(R.id.elv);
checkBox = view.findViewById(R.id.checkbox2);
tv_price = view.findViewById(R.id.tv_price);
tv_num = view.findViewById(R.id.tv_num);

}

@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}

@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}

@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event){
checkBox.setChecked(event.isGroupChecked());
tv_price.setText("合计:"+event.getPrice()+"");
tv_num.setText("结算("+event.getCount()+")");

}
@Override
public void getData_SelectCart(MyCartBean bean) {
if(bean.getCode().equals("0")){
List<MyCartBean.DataBean> data1 = bean.getData();
List<List<MyCartBean.DataBean.ListBean> > list = new ArrayList<>();
for (int i = 0; i < data1.size(); i++) {
list.add(data1.get(i).getList());
}

myCartAdapter = new MyCartAdapter(getActivity(), bean.getData(),list);
elv.setAdapter(myCartAdapter);
elv.setAdapter(myCartAdapter);
elv.setGroupIndicator(null);
for (int i = 0; i < data1.size(); i++) {
elv.expandGroup(i);
}
}else{
Gson gson = new Gson();
MyCartBean newbean = gson.fromJson(RegApi.WUYU, MyCartBean.class);
List<MyCartBean.DataBean> data1 = newbean.getData();
List<List<MyCartBean.DataBean.ListBean> > list = new ArrayList<>();
for (int i = 0; i < data1.size(); i++) {
list.add(data1.get(i).getList());
}
myCartAdapter = new MyCartAdapter(getActivity(), newbean.getData(),list);
elv.setAdapter(myCartAdapter);
elv.setAdapter(myCartAdapter);
elv.setGroupIndicator(null);
for (int i = 0; i < data1.size(); i++) {
elv.expandGroup(i);
}
}
}
}

Bean类封装了自己新添加的两个属性所以封装Bean也发上来了
public class MyCartBean {

/**
* msg : 请求成功
* code : 0
* data : [{"list":[{"bargainPrice":3455,"createtime":"2017-10-14T21:48:08","detailUrl":"https://item.m.jd.com/product/12224420750.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9106/106/1785172479/537280/253bc0ab/59bf78a7N057e5ff7.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t9106/106/1785172479/537280/253bc0ab/59bf78a7N057e5ff7.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8461/5/1492479653/68388/7255e013/59ba5e84N91091843.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8461/5/1492479653/68388/7255e013/59ba5e84N91091843.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8803/356/1478945529/489755/2a163ace/59ba5e84N7bb9a666.jpg!q70.jpg","num":1,"pid":50,"price":444,"pscid":39,"selected":0,"sellerid":6,"subhead":"【现货新品抢购】全面屏2.0震撼来袭,骁龙835处理器,四曲面陶瓷机","title":"小米(MI) 小米MIX2 手机 黑色 全网通 (6GB+64GB)【标配版】"}],"sellerName":"商家6","sellerid":"6"},{"list":[{"bargainPrice":3455,"createtime":"2017-10-14T21:38:26","detailUrl":"https://item.m.jd.com/product/12224420750.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9106/106/1785172479/537280/253bc0ab/59bf78a7N057e5ff7.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t9106/106/1785172479/537280/253bc0ab/59bf78a7N057e5ff7.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8461/5/1492479653/68388/7255e013/59ba5e84N91091843.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8461/5/1492479653/68388/7255e013/59ba5e84N91091843.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8803/356/1478945529/489755/2a163ace/59ba5e84N7bb9a666.jpg!q70.jpg","num":1,"pid":51,"price":555,"pscid":39,"selected":0,"sellerid":7,"subhead":"【现货新品抢购】全面屏2.0震撼来袭,骁龙835处理器,四曲面陶瓷机","title":"小米(MI) 小米MIX2 手机 黑色 全网通 (6GB+64GB)【标配版】"}],"sellerName":"商家7","sellerid":"7"},{"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":2,"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":8,"pid":2,"price":299,"pscid":1,"selected":0,"sellerid":18,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家18","sellerid":"18"},{"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":2,"pid":3,"price":198,"pscid":1,"selected":0,"sellerid":19,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家19","sellerid":"19"},{"list":[{"bargainPrice":111.99,"createtime":"2017-10-14T21:48:08","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":4,"price":999,"pscid":1,"selected":0,"sellerid":20,"subhead":"每个中秋都不能简单,无论身在何处,你总需要一块饼让生活更圆满,京东月饼让爱更圆满京东自营,闪电配送,更多惊喜,快用手指戳一下","title":"北京稻香村 稻香村中秋节月饼 老北京月饼礼盒655g"}],"sellerName":"商家20","sellerid":"20"}]
*/

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":3455,"createtime":"2017-10-14T21:48:08","detailUrl":"https://item.m.jd.com/product/12224420750.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends","images":"https://m.360buyimg.com/n0/jfs/t9106/106/1785172479/537280/253bc0ab/59bf78a7N057e5ff7.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t9106/106/1785172479/537280/253bc0ab/59bf78a7N057e5ff7.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8461/5/1492479653/68388/7255e013/59ba5e84N91091843.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8461/5/1492479653/68388/7255e013/59ba5e84N91091843.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8803/356/1478945529/489755/2a163ace/59ba5e84N7bb9a666.jpg!q70.jpg","num":1,"pid":50,"price":444,"pscid":39,"selected":0,"sellerid":6,"subhead":"【现货新品抢购】全面屏2.0震撼来袭,骁龙835处理器,四曲面陶瓷机","title":"小米(MI) 小米MIX2 手机 黑色 全网通 (6GB+64GB)【标配版】"}]
* sellerName : 商家6
* sellerid : 6
*/
private boolean groupChecked;
private String sellerName;
private String sellerid;
private List<ListBean> list;

public boolean isGroupChecked() {
return groupChecked;
}

public void setGroupChecked(boolean groupChecked) {
this.groupChecked = groupChecked;
}

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 : 3455.0
* createtime : 2017-10-14T21:48:08
* detailUrl : https://item.m.jd.com/product/12224420750.html?utm_source=androidapp&utm_medium=appshare&utm_campaign=t_335139774&utm_term=QQfriends * images : https://m.360buyimg.com/n0/jfs/t9106/106/1785172479/537280/253bc0ab/59bf78a7N057e5ff7.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t9106/106/1785172479/537280/253bc0ab/59bf78a7N057e5ff7.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8461/5/1492479653/68388/7255e013/59ba5e84N91091843.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8461/5/1492479653/68388/7255e013/59ba5e84N91091843.jpg!q70.jpg|https://m.360buyimg.com/n0/jfs/t8803/356/1478945529/489755/2a163ace/59ba5e84N7bb9a666.jpg!q70.jpg * num : 1
* pid : 50
* price : 444.0
* pscid : 39
* selected : 0
* sellerid : 6
* subhead : 【现货新品抢购】全面屏2.0震撼来袭,骁龙835处理器,四曲面陶瓷机
* title : 小米(MI) 小米MIX2 手机 黑色 全网通 (6GB+64GB)【标配版】
*/

private boolean childrenChecked;
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 boolean isChildrenChecked() {
return childrenChecked;
}

public void setChildrenChecked(boolean childrenChecked) {
this.childrenChecked = childrenChecked;
}

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;
}
}
}
}


现在是关键的地方了适配器:Adapter

public class MyCartAdapter extends BaseExpandableListAdapter {
Context context;
List<MyCartBean.DataBean> groupList;
List<List<MyCartBean.DataBean.ListBean>> childrenlist;
private final LayoutInflater inflater;
private int price;
private int count;

public MyCartAdapter(Context context, List<MyCartBean.DataBean> groupList, List<List<MyCartBean.DataBean.ListBean>> childrenlist) {
this.context = context;
this.groupList = groupList;
this.childrenlist = childrenlist;
inflater = LayoutInflater.from(context);
}

@Override
public int getGroupCount() {
return groupList.size();
}

@Override
public int getChildrenCount(int groupPosition) {
return childrenlist.get(groupPosition).size();
}

@Override
public Object getGroup(int groupPosition) {
return groupList.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
return childrenlist.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 = null;
final GroupViewHolder holder;
if (convertView == null) {
holder = new GroupViewHolder();
view = inflater.inflate(R.layout.group_item_layout, null);
holder.check_group = view.findViewById(R.id.check_group);
holder.text_group = view.findViewById(R.id.text_group);
view.setTag(holder);
} else {
view = convertView;
holder = (GroupViewHolder) view.getTag();
}
holder.text_group.setText(groupList.get(groupPosition).getSellerName());
holder.check_group.setChecked(groupList.get(groupPosition).isGroupChecked());
holder.check_group.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (holder.check_group.isChecked()) {
groupList.get(groupPosition).setGroupChecked(true);
for (int i = 0; i < childrenlist.get(groupPosition).size(); i++) {
childrenlist.get(groupPosition).get(i).setChildrenChecked(true);
}
AllPrice();
notifyDataSetChanged();
} else {
groupList.get(groupPosition).setGroupChecked(false);
for (int i = 0; i < childrenlist.get(groupPosition).size(); i++) {
childrenlist.get(groupPosition).get(i).setChildrenChecked(false);
}
AllPrice();
}
notifyDataSetChanged();
}
});
return view;
}

@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View view = null;
final ChildViewHolder holder;
if (convertView == null) {
holder = new ChildViewHolder();
view = inflater.inflate(R.layout.child_item_layout, null);
holder.check_child = view.findViewById(R.id.check_child);
holder.image_good = view.findViewById(R.id.image_good);
holder.text_price = view.findViewById(R.id.text_price);
holder.text_title = view.findViewById(R.id.text_title);
holder.text_num = view.findViewById(R.id.text_num);
holder.text_add = view.findViewById(R.id.text_add);
holder.text_jian = view.findViewById(R.id.text_jian);
view.setTag(holder);
} else {
view = convertView;
holder = (ChildViewHolder) view.getTag();
}
final MyCartBean.DataBean.ListBean listBean = childrenlist.get(groupPosition).get(childPosition);
String images = listBean.getImages();
holder.check_child.setChecked(listBean.isChildrenChecked());
Glide.with(context).load(images.split("!")[0]).into(holder.image_good);
holder.text_price.setText(listBean.getBargainPrice() + "");
holder.text_title.setText(listBean.getTitle());
holder.text_num.setText(listBean.getNum() + "");
holder.text_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int num = listBean.getNum();
listBean.setNum(num + 1);
AllPrice();
}
});

holder.text_jian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int num = listBean.getNum();
if (num > 1) {
listBean.setNum(num - 1);
AllPrice();
}
}
});

holder.check_child.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (holder.check_child.isChecked()) {
childrenlist.get(groupPosition).get(childPosition).setChildrenChecked(true);
AllPrice();
if (isAllChildListChecked(groupPosition)) {
groupList.get(groupPosition).setGroupChecked(true);
EventBus.getDefault().post(new MessageEvent(isAllGroupChecked(), price, count));
} else {
groupList.get(groupPosition).setGroupChecked(false);
}
notifyDataSetChanged();
} else {
childrenlist.get(groupPosition).get(childPosition).setChildrenChecked(false);
groupList.get(groupPosition).setGroupChecked(false);
AllPrice();
}
notifyDataSetChanged();
}
});
return view;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}

class GroupViewHolder {
CheckBox check_group;
TextView text_group;
}

class ChildViewHolder {
CheckBox check_child;
TextView text_title;
TextView text_price;
ImageView image_good;
TextView text_num;
TextView text_jian;
TextView text_add;
}

private boolean isAllGroupChecked() {
for (int i = 0; i < groupList.size(); i++) {
if (!groupList.get(i).isGroupChecked()) {
return false;
}
}
return true;
}

private boolean isAllChildListChecked(int groupPosition) {
List<MyCartBean.DataBean.ListBean> list = childrenlist.get(groupPosition);
for (int i = 0; i < list.size(); i++) {
MyCartBean.DataBean.ListBean listBean = list.get(i);
if (!listBean.isChildrenChecked()) {
return false;
}
}
return true;
}

public void allChecked(boolean flag) {
for (int i = 0; i < groupList.size(); i++) {
groupList.get(i).setGroupChecked(flag);
List<MyCartBean.DataBean.ListBean> list = groupList.get(i).getList();
for (int j = 0; j < list.size(); j++) {
list.get(j).setChildrenChecked(flag);
}
}
notifyDataSetChanged();
}

public void AllPrice() {
price = 0;
count = 0;
for (int i = 0; i < groupList.size(); i++) {
int size = childrenlist.get(i).size();
for (int j = 0; j < size; j++) {
MyCartBean.DataBean.ListBean listBean = childrenlist.get(i).get(j);
if (listBean.isChildrenChecked()) {
count += listBean.getNum();
price += listBean.getBargainPrice() * listBean.getNum();
}
}
}
notifyDataSetChanged();
EventBus.getDefault().post(new MessageEvent(isAllGroupChecked(), price, count));
}
}


补充适配器向Activity传值的话有Handler和EventBus

在这里博主使用的是EventBus

这是用来传值的Event类

需要什么值就在这个类中封装什么属性

public class MessageEvent {
private boolean groupChecked;

private int price;

private int count;

public MessageEvent(boolean groupChecked, int price, int count) {
this.groupChecked = groupChecked;
this.price = price;
this.count = count;
}

public int getPrice() {
return price;
}

public void setPrice(int price) {
this.price = price;
}

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}

public MessageEvent(boolean groupChecked) {
this.groupChecked = groupChecked;
}

public boolean isGroupChecked() {
return groupChecked;
}

public void setGroupChecked(boolean groupChecked) {
this.groupChecked = groupChecked;
}


为了省时省力博主把布局也附上了

Fragment的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#ee0066"
android:gravity="center"
android:text="购物车"
android:textSize="25sp" />

<ExpandableListView
android:id="@+id/elv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="@android:color/white"
android:gravity="center_vertical">

<CheckBox
android:id="@+id/checkbox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:focusable="false" />

<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/checkbox2"
android:gravity="center_vertical"
android:text="全选"
android:textSize="20sp" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="合计 :" />

<TextView
android:id="@+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:paddingRight="10dp"
android:text="0"
android:textColor="@android:color/holo_red_light" />

<TextView
android:id="@+id/tv_num"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:background="@android:color/holo_red_dark"
android:gravity="center"
android:padding="10dp"
android:text="结算(0)"
android:textColor="@android:color/white" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>

购物车二级列表Children条目布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">

<CheckBox
android:id="@+id/check_child"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentStart="true" />

<ImageView
android:id="@+id/image_good"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/check_child"
android:layout_marginLeft="10dp"
android:layout_width="80dp"
android:layout_height="80dp" />

<TextView
android:text="12345674"
android:id="@+id/text_title"
android:layout_toRightOf="@+id/image_good"
android:layout_marginLeft="10dp"
android:layout_alignTop="@+id/image_good"
android:maxLines="2"
android:minLines="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/text_price"
android:layout_toRightOf="@+id/image_good"
android:layout_marginLeft="10dp"
android:layout_alignBottom="@+id/image_good"
android:text="¥99.99"
android:textColor="#ff0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<LinearLayout
android:layout_alignParentRight="true"
android:layout_alignBottom="@+id/image_good"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<TextView
android:id="@+id/text_jian"
android:text="一"
android:padding="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:gravity="center"
android:id="@+id/text_num"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_width="wrap_content"
android:layout_height="match_parent" />

<TextView
android:id="@+id/text_add"
android:text="十"
android:padding="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

</RelativeLayout>


购物车二级列表的Group布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:gravity="center_vertical"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">

<CheckBox

android:id="@+id/check_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:layout_marginLeft="10dp"
android:text="京东自营"
android:id="@+id/text_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: