购物车页面布局
<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="wrap_content" android:text="购物车" android:background="#AAAAAA" android:gravity="center" android:textSize="25sp"/> <ExpandableListView android:id="@+id/elv" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"></ExpandableListView> <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:layout_marginRight="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>
二级列表父布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="50dp" android:layout_gravity="center_vertical" android:orientation="horizontal"> <CheckBox android:id="@+id/cb_parent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="20dp" android:focusable="false"/> <TextView android:id="@+id/tv_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="123456" android:layout_gravity="center_vertical"/> </LinearLayout>
二级列表 子布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" android:orientation="horizontal"> <CheckBox android:id="@+id/cb_child" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="30dp" android:layout_marginLeft="10dp" android:layout_marginTop="30dp" android:focusable="false" /> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/car_child_sdv" android:layout_width="70dp" android:layout_height="100dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/tv_tel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="iphone6" /> <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="什么手机" /> <RelativeLayout android:layout_marginTop="20dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/tv_pri" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="¥3000.00" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentTop="true" android:layout_marginEnd="17dp" android:gravity="center_vertical"> <ImageView android:id="@+id/iv_del" android:layout_width="20dp" android:layout_height="20dp" android:src="@mipmap/jian" /> <TextView android:id="@+id/tv_shuliang" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:paddingBottom="2dp" android:paddingLeft="20dp" android:paddingRight="20dp" android:paddingTop="2dp" android:text="1" /> <ImageView android:id="@+id/iv_add" android:layout_width="20dp" android:layout_height="20dp" android:layout_marginLeft="5dp" android:src="@mipmap/jia" /> </LinearLayout> </RelativeLayout> </LinearLayout> </LinearLayout>
view接口
public interface ICarView extends IBaseView { void sussecc(List<CarBean.DataBean> groupList, List<List<CarBean.DataBean.ListBean>> childList); }
model
public class CarModel extends BaseModel{ private RetrofitUtils retrofitUtils; public void getCar(String uid, String token, final ICarModel iCarModel){ retrofitUtils=RetrofitUtils.getInstance(); HashMap<String,String> params=new HashMap<>(); params.put("uid",uid); params.put("token",token); retrofitUtils.getApi().showcar(params) .subscribeOn(io.reactivex.schedulers.Schedulers.io()) .observeOn(io.reactivex.android.schedulers.AndroidSchedulers.mainThread()) .subscribe(new Observer<CarBean>() { @Override public void onSubscribe(Disposable d) { Log.e("carModel",d+""); } @Override public void onNext(CarBean carBean) { iCarModel.success(carBean); } @Override public void onError(Throwable e) { Log.e("carModel",e+""); } @Override public void onComplete() { } }); } public interface ICarModel{ void success(CarBean carBean); } }
presenter
public class CarPresenter extends BasePresenter<CarModel,ICarView> { public void getData(String uid,String token){ model.getCar(uid, token, new CarModel.ICarModel() { @Override public void success(CarBean carBean) { List<CarBean.DataBean> data = carBean.getData(); ArrayList<List<CarBean.DataBean.ListBean>> childList = new ArrayList<>(); for(int i=0;i<data.size();i++){ List<CarBean.DataBean.ListBean> list = data.get(i).getList(); childList.add(list); } view.sussecc(data,childList); } }); } }
eventBus两个类
public class MessageEvent { private boolean checked; public boolean isChecked() { return checked; } public void setChecked(boolean checked) { this.checked = checked; } }
public class PriceAndCountEvent { private String price; private int count; public String getPrice() { return price; } public int getCount() { return count; } public void setPrice(String price) { this.price = price; } public void setCount(int count) { this.count = count; } }
activity
public class Car extends BaseFragment<CarPresenter> implements ICarView { private ExpandableListView elv; private CheckBox ck; private TextView price; private TextView num; private CarAdapter carAdapter; @Override protected void initData() { presenter.getData("13597","578723454A87C80AD041A4F597C05CAE"); EventBus.getDefault().register(this); ck.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { carAdapter.changeAllListCbState(ck.isChecked()); } }); } @Override protected BaseModel initModel() { return new CarModel(); } @Override protected CarPresenter initPresenter() { return new CarPresenter(); } @Override protected void initView(View view) { elv = view.findViewById(R.id.elv); ck = view.findViewById(R.id.checkbox2); price = view.findViewById(R.id.tv_price); num = view.findViewById(R.id.tv_num); } @Override protected int bindLayoutId() { return R.layout.fragment_car; } @Override public void sussecc(List<CarBean.DataBean> groupList, List<List<CarBean.DataBean.ListBean>> childList) { carAdapter = new CarAdapter(getContext(), groupList, childList); elv.setAdapter(carAdapter); elv.setGroupIndicator(null); for(int i=0;i<groupList.size();i++){ elv.expandGroup(i); } } @Subscribe public void onMessageEvent(MessageEvent event){ ck.setChecked(event.isChecked()); } @Subscribe public void onMessageEvent(PriceAndCountEvent event){ num.setText("结算(" + event.getCount() + ")"); price.setText(event.getPrice()+""); } @Override public void onDestroy() { super.onDestroy(); presenter.detach(); } }
adapter
public class CarAdapter extends BaseExpandableListAdapter { private Context context; private List<CarBean.DataBean> groupList; private List<List<CarBean.DataBean.ListBean>> childList; private final LayoutInflater inflater; public CarAdapter(Context context, List<CarBean.DataBean> groupList, List<List<CarBean.DataBean.ListBean>> childList) { this.context = context; this.groupList = groupList; this.childList = childList; inflater = LayoutInflater.from(context); } @Override public int getGroupCount() { return groupList.size(); } @Override public int getChildrenCount(int groupPosition) { return childList.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { return groupList.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return childList.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 GroupViewHolder holder; if (convertView == null) { holder = new GroupViewHolder(); convertView = inflater.inflate(R.layout.item_parent_market, parent, false); holder.cbGroup = convertView.findViewById(R.id.cb_parent); holder.tv_number = convertView.findViewById(R.id.tv_number); convertView.setTag(holder); } else { holder = (GroupViewHolder) convertView.getTag(); } final CarBean.DataBean dataBean = groupList.get(groupPosition); holder.cbGroup.setChecked(dataBean.isChecked()); holder.tv_number.setText(dataBean.getSellerName()); //一级 holder.cbGroup.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dataBean.setChecked(holder.cbGroup.isChecked()); changeChildCbState(groupPosition, holder.cbGroup.isChecked()); EventBus.getDefault().post(compute() + ""); changeAllCbState(isAllGroupCbSelected()); notifyDataSetChanged(); } }); return convertView; } @Override public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final ChildViewHolder holder; if (convertView == null) { holder = new ChildViewHolder(); convertView = inflater.inflate(R.layout.item_child_market, parent, false); holder.cbChild = convertView.findViewById(R.id.cb_child); holder.tv_tel = convertView.findViewById(R.id.tv_tel); holder.tv_content = convertView.findViewById(R.id.tv_content); holder.iv_add = convertView.findViewById(R.id.iv_add); holder.iv_del = convertView.findViewById(R.id.iv_del); holder.tv_price = convertView.findViewById(R.id.tv_pri); holder.tv_num1 = convertView.findViewById(R.id.tv_shuliang); holder.sdv = convertView.findViewById(R.id.car_child_sdv); convertView.setTag(holder); } else { holder = (ChildViewHolder) convertView.getTag(); } final CarBean.DataBean.ListBean listBean = childList.get(groupPosition).get(childPosition); String images = listBean.getImages(); String[] split = images.split("\\|"); holder.sdv.setImageURI(split[0]); holder.cbChild.setChecked(listBean.isChecked()); holder.tv_tel.setText(listBean.getTitle()); holder.tv_content.setText(listBean.getSubhead()); holder.tv_price.setText(listBean.getPrice()); holder.tv_num1.setText(listBean.getNum() + ""); //二级 holder.cbChild.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { listBean.setChecked(holder.cbChild.isChecked()); PriceAndCountEvent priceAndCountEvent = compute(); EventBus.getDefault().post(priceAndCountEvent); if (holder.cbChild.isChecked()) { if (isAllChildCbSelected(groupPosition)) { changGroupCbState(groupPosition, true); changeAllCbState(isAllGroupCbSelected()); } } else { changGroupCbState(groupPosition, false); changeAllCbState(isAllGroupCbSelected()); } notifyDataSetChanged(); } }); //加号 holder.iv_add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int num = listBean.getNum(); holder.tv_num1.setText(++num + ""); listBean.setNum(num); if (holder.cbChild.isChecked()) { PriceAndCountEvent priceAndCountEvent = compute(); EventBus.getDefault().post(priceAndCountEvent); } } }); //减号 holder.iv_del.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int num = listBean.getNum(); if (num == 1) { return; } holder.tv_num1.setText(--num + ""); listBean.setNum(num); if (holder.cbChild.isChecked()) { PriceAndCountEvent priceAndCountEvent = compute(); EventBus.getDefault().post(priceAndCountEvent); } } }); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; } class GroupViewHolder { CheckBox cbGroup; TextView tv_number; } class ChildViewHolder { SimpleDraweeView sdv; CheckBox cbChild; TextView tv_tel; TextView tv_content; TextView tv_price; ImageView iv_del; ImageView iv_add; TextView tv_num1; } /** * 改变全选的状态 * * @param flag */ private void changeAllCbState(boolean flag) { MessageEvent messageEvent = new MessageEvent(); messageEvent.setChecked(flag); EventBus.getDefault().post(messageEvent); } /** * 改变一级列表checkbox状态 * * @param groupPosition */ private void changGroupCbState(int groupPosition, boolean flag) { CarBean.DataBean dataBean = groupList.get(groupPosition); dataBean.setChecked(flag); } /** * 改变二级列表checkbox状态 * * @param groupPosition * @param flag */ private void changeChildCbState(int groupPosition, boolean flag) { List<CarBean.DataBean.ListBean> datasBeen = childList.get(groupPosition); for (int i = 0; i < datasBeen.size(); i++) { CarBean.DataBean.ListBean datasBean = datasBeen.get(i); datasBean.setChecked(flag); } } /** * 判断一级列表是否全部选中 * * @return */ private boolean isAllGroupCbSelected() { for (int i = 0; i < groupList.size(); i++) { CarBean.DataBean dataBean = groupList.get(i); if (!dataBean.isChecked()) { return false; } } return true; } /** * 判断二级列表是否全部选中 * * @param groupPosition * @return */ private boolean isAllChildCbSelected(int groupPosition) { List<CarBean.DataBean.ListBean> datasBeen = childList.get(groupPosition); for (int i = 0; i < datasBeen.size(); i++) { CarBean.DataBean.ListBean datasBean = datasBeen.get(i); if (!datasBean.isChecked()) { return false; } } return true; } /** * 计算列表中,选中的钱和数量 */ private PriceAndCountEvent compute() { int count = 0; double price = 0; for (int i = 0; i < childList.size(); i++) { List<CarBean.DataBean.ListBean> datasBeen = childList.get(i); for (int j = 0; j < datasBeen.size(); j++) { CarBean.DataBean.ListBean dataBean = datasBeen.get(j); if (dataBean.isChecked()) { price += dataBean.getNum() * Double.parseDouble(dataBean.getPrice()); count += dataBean.getNum(); } } } PriceAndCountEvent priceAndCountEvent = new PriceAndCountEvent(); priceAndCountEvent.setCount(count); priceAndCountEvent.setPrice(price + ""); return priceAndCountEvent; } /** * 设置全选、反选 * * @param flag */ public void changeAllListCbState(boolean flag) { for (int i = 0; i < groupList.size(); i++) { changGroupCbState(i, flag); changeChildCbState(i, flag); } EventBus.getDefault().post(compute()); notifyDataSetChanged(); } }