首先布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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="#990000ff"
android:gravity="center"
android:text="购物车"
android:textColor="#ff3660"
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="@color/antiquewhite"
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>
然后是该fragment
public class ShopFragment extends Fragment implements IcartView {
private String uid;
@BindView(R.id.elv)
ExpandableListView elv;
@BindView(R.id.checkbox2)
CheckBox checkbox2;
@BindView(R.id.tv_price)
TextView tvPrice;
@BindView(R.id.tv_num)
TextView tvNum;
Unbinder unbinder;
private MyAdapter myAdapter;
private CartPresenter cartPresenter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_shop, container, false);
unbinder = ButterKnife.bind(this, view);
EventBus.getDefault().register(this);
//
uid = (String) SharedPreferencesUtils.getParam(getActivity(), "uid", "");
cartPresenter = new CartPresenter(this);
if (!TextUtils.isEmpty(uid)) {
cartPresenter.getData(uid);
}
checkbox2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myAdapter.changeAllListCbState(checkbox2.isChecked());
}
});
return view;
}
@Override
public Context context() {
return getContext();
}
@Override
public void onSuccess(ShopBean shopBean) {
List<ShopBean.DataBean> data = shopBean.getData();
List<List<ShopBean.DataBean.ListBean>> list = new ArrayList<List<ShopBean.DataBean.ListBean>>();
for (int i = 0; i < data.size(); i++) {
List<ShopBean.DataBean.ListBean> list1 = data.get(i).getList();
list.add(list1);
}
myAdapter = new MyAdapter(getContext(), data, list);
elv.setAdapter(myAdapter);
elv.setGroupIndicator(null);
//默认让其全部展开
for (int i = 0; i < data.size(); i++) {
elv.expandGroup(i);
}
}
@Override
public void onFaild(String error) {
}
@Override
public void delCart(DeleteCartBean deleteCartBean) {
if (deleteCartBean.getCode().equals("0")) {
cartPresenter.getData(uid);
}
}
@Subscribe
public void onMessageEvent(MessageEvent event){
checkbox2.setChecked(event.isChecked());
}
@Subscribe
public void onMessageEvent(PriceAndCountEvent event){
tvNum.setText("结算(" + event.getCount() + ")");
tvPrice.setText(event.getPrice()+"");
}
//销毁方法
@Override
public void onDestroy() {
super.onDestroy();
//eventBus销毁
EventBus.getDefault().unregister(this);
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
}
adapter
public class MyAdapter extends BaseExpandableListAdapter { Context context; List<ShopBean.DataBean> group; List<List<ShopBean.DataBean.ListBean>> child; private final LayoutInflater inflater; public MyAdapter(Context context, List<ShopBean.DataBean> data, List<List<ShopBean.DataBean.ListBean>> list) { this.group = data; this.child = list; this.context = context; 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) { holder = new GroupViewHolder(); view = inflater.inflate(R.layout.group_item, null); holder.cbGroup = view.findViewById(R.id.seller_cb); holder.tv_number = view.findViewById(R.id.seller_name_tv); view.setTag(holder); } else { view = convertView; holder = (GroupViewHolder) view.getTag(); } final ShopBean.DataBean dataBean = group.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 view; } @Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { View view; final ChildViewHolder holder; if (convertView == null) { holder = new ChildViewHolder(); view = inflater.inflate(R.layout.child_item, null); holder.cbChild = view.findViewById(R.id.cb_child); holder.tv_tel = view.findViewById(R.id.tv_tel); holder.tv_content = view.findViewById(R.id.my_image_view); holder.tv_time = view.findViewById(R.id.tv_time); holder.tv_del = view.findViewById(R.id.tv_del); holder.iv_add = view.findViewById(R.id.iv_add); holder.iv_del = view.findViewById(R.id.iv_del); holder.tv_price = view.findViewById(R.id.tv_pri); holder.tv_num = view.findViewById(R.id.tv_num); view.setTag(holder); } else { view = convertView; holder = (ChildViewHolder) view.getTag(); } final ShopBean.DataBean.ListBean datasBean = child.get(groupPosition).get(childPosition); Uri uri = null; String[] split = datasBean.getImages().split("\\|"); for (int i = 0; i <split.length ; i++) { uri= Uri.parse(split[i]); } holder.tv_content.setImageURI(uri); holder.cbChild.setChecked(datasBean.isChecked()); holder.tv_tel.setText(datasBean.getTitle()); //holder.tv_content.setText(datasBean.getSubhead()); holder.tv_time.setText(datasBean.getCreatetime()); holder.tv_price.setText("价格" + datasBean.getPrice()); holder.tv_num.setText(datasBean.getNum() + ""); //二级checkbox holder.cbChild.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //设置该条目对象里的checked属性值 datasBean.setChecked(holder.cbChild.isChecked()); PriceAndCountEvent priceAndCountEvent = compute(); EventBus.getDefault().post(priceAndCountEvent); if (holder.cbChild.isChecked()) { //当前checkbox是选中状态 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 = datasBean.getNum(); holder.tv_num.setText(++num + ""); datasBean.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 = datasBean.getNum(); if (num == 1) { return; } holder.tv_num.setText(--num + ""); datasBean.setNum(num); if (holder.cbChild.isChecked()) { PriceAndCountEvent priceAndCountEvent = compute(); EventBus.getDefault().post(priceAndCountEvent); } } }); //删除 holder.tv_del.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { List<ShopBean.DataBean.ListBean> datasBeen = child.get(groupPosition); ShopBean.DataBean.ListBean remove = datasBeen.remove(childPosition); if (datasBeen.size() == 0) { child.remove(groupPosition); group.remove(groupPosition); } EventBus.getDefault().post(compute()); notifyDataSetChanged(); } }); return view; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } class GroupViewHolder { CheckBox cbGroup; TextView tv_number; } class ChildViewHolder { CheckBox cbChild; TextView tv_tel; SimpleDraweeView tv_content; TextView tv_time; TextView tv_price; ImageView tv_del; ImageView iv_del; ImageView iv_add; TextView tv_num; } /** * 改变全选的状态 * * @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) { ShopBean.DataBean dataBean = group.get(groupPosition); dataBean.setChecked(flag); } /** * 改变二级列表checkbox状态 * * @param groupPosition * @param flag */ private void changeChildCbState(int groupPosition, boolean flag) { List<ShopBean.DataBean.ListBean> datasBeen = child.get(groupPosition); for (int i = 0; i < datasBeen.size(); i++) { ShopBean.DataBean.ListBean datasBean = datasBeen.get(i); datasBean.setChecked(flag); } } /** * 判断一级列表是否全部选中 * * @return */ private boolean isAllGroupCbSelected() { for (int i = 0; i < group.size(); i++) { ShopBean.DataBean dataBean = group.get(i); if (!dataBean.isChecked()) { return false; } } return true; } /** * 判断二级列表是否全部选中 * * @param groupPosition * @return */ private boolean isAllChildCbSelected(int groupPosition) { List<ShopBean.DataBean.ListBean> datasBeen = child.get(groupPosition); for (int i = 0; i < datasBeen.size(); i++) { ShopBean.DataBean.ListBean datasBean = datasBeen.get(i); if (!datasBean.isChecked()) { return false; } } return true; } /** * 计算列表中,选中的钱和数量 */ private PriceAndCountEvent compute() { int count = 0; int price = 0; for (int i = 0; i < child.size(); i++) { List<ShopBean.DataBean.ListBean> datasBeen = child.get(i); for (int j = 0; j < datasBeen.size(); j++) { ShopBean.DataBean.ListBean datasBean = datasBeen.get(j); if (datasBean.isChecked()) { price += datasBean.getNum() * datasBean.getPrice(); count += datasBean.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 < group.size(); i++) { changGroupCbState(i, flag); changeChildCbState(i, flag); } EventBus.getDefault().post(compute()); notifyDataSetChanged(); } }