购物车呢

MainActivity


【购物车呢】

package com.example.wocao; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.DragAndDropPermissions; import android.view.View; import android.widget.CheckBox; import android.widget.ExpandableListView; import android.widget.TextView; import com.example.wocao.util.RetrofitUtil; import java.util.List; import de.greenrobot.event.EventBus; import de.greenrobot.event.Subscribe; import io.reactivex.Observable; import io.reactivex.Observer; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.Disposable; import io.reactivex.schedulers.Schedulers; public class MainActivity extends AppCompatActivity { private MyAdapter adapter; private ExpandableListView expandableListView; private CheckBox btnCheckAll; private TextView tvAllPrice; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EventBus.getDefault().register(this); btnCheckAll = findViewById(R.id.btnCheckAll); expandableListView = findViewById(R.id.expandList); tvAllPrice = findViewById(R.id.tvAllPrice); initData(); }private void initData() {Observable guc = RetrofitUtil.getInstance().getZhujieApi().getGUC(); guc.observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe(new Observer() {@Override public void onSubscribe(Disposable d) {}@Override public void onNext(GucBean value) { List data = https://www.it610.com/article/value.getData(); Log.d("TAG", data.get(0).getSellerName()); adapter = new MyAdapter(data,MainActivity.this); expandableListView.setAdapter(adapter); for (int i = 0; i < data.size(); i++) { expandableListView.expandGroup(i); } }@Override public void onError(Throwable e) {}@Override public void onComplete() {} }); btnCheckAll.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { adapter.changeAllListCbState(btnCheckAll.isChecked()); } }); } @Subscribe public void onMessageEvent(MessageEvent event) { btnCheckAll.setChecked(event.isChecked()); } @Subscribe public void onMessageEvent(PriceAndCountEvent event) { Log.d("ssss", event.getPrice()+""); Log.d("ssss", event.getCount()+""); tvAllPrice.setText("结算:$"+event.getPrice()+""); //tvAllPrice.setText("¥" + event.getPrice()); } }

MyAdapter



package com.example.wocao; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.CheckBox; import android.widget.LinearLayout; import android.widget.TextView; import com.facebook.drawee.view.SimpleDraweeView; import java.util.List; import de.greenrobot.event.EventBus; public class MyAdapter extends BaseExpandableListAdapter { private List data; private Context context; public MyAdapter(List data, Context context) { this.data = https://www.it610.com/article/data; this.context = context; }@Override public int getGroupCount() { return data.size(); }@Override public int getChildrenCount(int groupPosition) { return data.get(groupPosition).getList().size(); }@Override public Object getGroup(int groupPosition) { return data.get(groupPosition); }@Override public Object getChild(int groupPosition, int childPosition) { return data.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 view, ViewGroup parent) { if(view==null) { view= LayoutInflater.from(context).inflate(R.layout.layout_group,null); }final CheckBox group_choosed = view.findViewById(R.id.group_choosed); TextView group_tv = view.findViewById(R.id.group_tv); group_tv.setText(data.get(groupPosition).getSellerName()); if(data.get(groupPosition).isChecked()){ group_choosed.setChecked(true); }else { group_choosed.setChecked(false); } group_choosed.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { data.get(groupPosition).setChecked(group_choosed.isChecked()); changeChild(groupPosition,group_choosed.isChecked()); changeAllCbState(isAllGroupCbSelected()); EventBus.getDefault().post(compute()); notifyDataSetChanged(); } }); return view; }@Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View view, ViewGroup parent) { final List list = data.get(groupPosition).getList(); if (view==null) { view = LayoutInflater.from(context).inflate(R.layout.layout_child, null); } final CheckBox child_choosed = view.findViewById(R.id.child_choosed); SimpleDraweeView img = view.findViewById(R.id.img); TextViewtitleName= view.findViewById(R.id.titleName); TextViewbtnSub = view.findViewById(R.id.btnSub); TextViewbtnAdd= view.findViewById(R.id.btnAdd); final TextViewshowNum = view.findViewById(R.id.showNum); /* showNumbtnSubbtnAdd*/ titleName.setText(list.get(childPosition).getTitle()); String images = list.get(childPosition).getImages(); String[] split = images.split("\\|"); img.setImageURI(split[0]); btnSub.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { /* int num = list.get(childPosition).getNum(); num++; list.get(childPosition).setNum(num); notifyDataSetChanged(); */ } }); btnAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { /*int num = list.get(childPosition).getNum(); num++; list.get(childPosition).setNum(num); showNum.setText(list.get(childPosition).getNum()); notifyDataSetChanged(); */ } }); if(list.get(childPosition).isChecked()){ child_choosed.setChecked(true); }else { child_choosed.setChecked(false); }child_choosed.setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) { //设置该条目对象里的setChildchecked属性值 list.get(childPosition).setChecked(child_choosed.isChecked()); //二级checkbox全部选中 if (isAllChildCbSelected(groupPosition)) { //一级框就选中 changeGroup(groupPosition, true); changeAllCbState(isAllGroupCbSelected()); } else { //否则不选中 changeGroup(groupPosition, false); } EventBus.getDefault().post(compute()); notifyDataSetChanged(); } }); return view; }@Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; }//改变一级列表状态 private void changeGroup(int groupPosition,boolean flag){GucBean.DataBean dataBean = data.get(groupPosition); dataBean.setChecked(flag); } //改变二级列表状态 private void changeChild(int groupPosition,boolean flag){List list = data.get(groupPosition).getList(); for (int i = 0; i < list.size(); i++) { GucBean.DataBean.ListBean listBean = list.get(i); listBean.setChecked(flag); }}//判断一级列表是否全部选中 private boolean isAllGroupCbSelected() { for (int i = 0; i < data.size(); i++) { GucBean.DataBean dataBean = data.get(i); if(!dataBean.isChecked()){ returnfalse; }} return true; } //判断二级列表是否全部选中 private boolean isAllChildCbSelected(int groupPosition){for (int i = 0; i < data.get(groupPosition).getList().size(); i++) {GucBean.DataBean.ListBean listBean = data.get(groupPosition).getList().get(i); if(!listBean.isChecked()){ return false; } } return true; }private void changeAllCbState(boolean flag) { MessageEvent messageEvent = new MessageEvent(); messageEvent.setChecked(flag); EventBus.getDefault().post(messageEvent); }private PriceAndCountEvent compute() { int count = 0; int price = 0; for (int i = 0; i < data.get(i).getList().size(); i++) { //List datasBeen = childList.get(i); List list = this.data.get(i).getList(); for (int j = 0; j < list.size(); j++) { GucBean.DataBean.ListBean listBean = list.get(j); if (listBean.isChecked()) { price += listBean.getNum() * listBean.getPrice(); count += listBean.getNum(); } } } PriceAndCountEvent priceAndCountEvent = new PriceAndCountEvent(); priceAndCountEvent.setCount(count); priceAndCountEvent.setPrice(price); return priceAndCountEvent; } //设置反选全选 public void changeAllListCbState(boolean flag) { for (int i = 0; i < data.size(); i++) { changeGroup(i, flag); changeChild(i, flag); } EventBus.getDefault().post(compute()); notifyDataSetChanged(); }}



PriceAndCountEvent



package com.example.wocao; /** * author: Wangxinrun * Date: 2018/6/28 * Time: 19:20 */ public class PriceAndCountEvent { private int price; private int 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; } }



MessageEvent



package com.example.wocao; /** * author: Wangxinrun * Date: 2018/6/28 * Time: 19:20 */ public class MessageEvent { private boolean checked; public boolean isChecked() { return checked; }public void setChecked(boolean checked) { this.checked = checked; } }

    推荐阅读