在做电商项目的时候,肯定绕不过购物车这一项,我是使用二级列表来实现的展示。
一级的itemLayout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <CheckBox android:id="@+id/group_checkBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:focusable="false" /> <TextView android:id="@+id/group_textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:layout_marginLeft="10sp" /> </LinearLayout>
二级的itemLayout
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingBottom="24dp" > <CheckBox android:id="@+id/child_checkBox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:focusable="false" android:layout_marginBottom="10dp" android:layout_below="@+id/child_goods_name" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <TextView android:id="@+id/child_goods_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:text="112" android:layout_marginTop="24dp" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/child_goods_img" android:layout_toEndOf="@+id/child_goods_img" /> <ImageView android:id="@+id/child_goods_img" android:layout_width="100dp" android:layout_height="100dp" android:layout_marginLeft="11dp" android:layout_marginStart="11dp" app:srcCompat="@mipmap/ic_launcher" android:layout_alignTop="@+id/child_goods_name" android:layout_toRightOf="@+id/child_checkBox" android:layout_toEndOf="@+id/child_checkBox" /> <TextView android:id="@+id/child_goods_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/child_goods_img" android:layout_toEndOf="@+id/child_goods_img" android:layout_toRightOf="@+id/child_goods_img" android:textColor="#FF6600" android:textSize="16sp" android:text="TextView" /> <TextView android:id="@+id/child_goods_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/child_goods_price" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_marginRight="5dp" android:text="TextView" /> </RelativeLayout> </RelativeLayout>
二级列表ExpandableListview适配器
public class ExpandableAdapter extends BaseExpandableListAdapter { private Context context; private CartBean cartBean; public ExpandableAdapter(Context context, CartBean cartBean) { this.context = context; this.cartBean = cartBean; } @Override public int getGroupCount() { return cartBean.getData().size(); } @Override public int getChildrenCount(int groupPosition) { return cartBean.getData().get(groupPosition).getList().size(); } @Override public Object getGroup(int groupPosition) { return cartBean.getData().get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return cartBean.getData().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 true; } @Override public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { final ViewHolder holder; if (convertView == null) { convertView = View.inflate(context, R.layout.group_item, null); holder = new ViewHolder(); holder.checkBox = (CheckBox) convertView.findViewById(R.id.group_checkBox); holder.textView = (TextView) convertView.findViewById(R.id.group_textView); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.textView.setText(cartBean.getData().get(groupPosition).getSellerName()); holder.checkBox.setChecked(cartBean.getData().get(groupPosition).isGroupIsChecked()); // 给多选添加点击事件 holder.checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 先将点击的那个bean里的数据改变 if (holder.checkBox.isChecked() == true) { cartBean.getData().get(groupPosition).setGroupIsChecked(true); for (int i = 0; i < cartBean.getData().get(groupPosition).getList().size(); i++) { cartBean.getData().get(groupPosition).getList().get(i).setChildIsChecked(true); } notifyDataSetChanged(); } else { cartBean.getData().get(groupPosition).setGroupIsChecked(false); for (int i = 0; i < cartBean.getData().get(groupPosition).getList().size(); i++) { cartBean.getData().get(groupPosition).getList().get(i).setChildIsChecked(false); } notifyDataSetChanged(); } panduan(); } }); return convertView; } @Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final ViewHolder holder; if (convertView == null) { convertView = View.inflate(context, R.layout.child_item, null); holder = new ViewHolder(); holder.checkBox = (CheckBox) convertView.findViewById(R.id.child_checkBox); holder.name = (TextView) convertView.findViewById(R.id.child_goods_name); holder.img = (ImageView) convertView.findViewById(R.id.child_goods_img); holder.price = (TextView) convertView.findViewById(R.id.child_goods_price); holder.num = (TextView) convertView.findViewById(R.id.child_goods_num); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.name.setText(cartBean.getData().get(groupPosition).getList().get(childPosition).getTitle()); holder.price.setText("¥"+cartBean.getData().get(groupPosition).getList().get(childPosition).getPrice()); holder.num.setText("x"+cartBean.getData().get(groupPosition).getList().get(childPosition).getNum()); holder.checkBox.setChecked(cartBean.getData().get(groupPosition).getList().get(childPosition).isChildIsChecked()); String[] split = cartBean.getData().get(groupPosition).getList().get(childPosition).getImages().split("!"); Picasso.with(context).load(split[0]).into(holder.img); holder.checkBox.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (holder.checkBox.isChecked() == true) { cartBean.getData().get(groupPosition).getList().get(childPosition).setChildIsChecked(true); // 查询当前一级目录下的所有框是否选中 boolean groupCheck = true; for (int i = 0; i < cartBean.getData().get(groupPosition).getList().size(); i++) { if (!cartBean.getData().get(groupPosition).getList().get(i).isChildIsChecked()) { groupCheck = false; } } // 如果选中给一级的框赋值 if (groupCheck) { cartBean.getData().get(groupPosition).setGroupIsChecked(true); notifyDataSetChanged(); } else { cartBean.getData().get(groupPosition).setGroupIsChecked(false); notifyDataSetChanged(); } } else { cartBean.getData().get(groupPosition).getList().get(childPosition).setChildIsChecked(false); boolean groupCheck = true; for (int i = 0; i < cartBean.getData().get(groupPosition).getList().size(); i++) { if (!cartBean.getData().get(groupPosition).getList().get(i).isChildIsChecked()) { groupCheck = false; } } if (groupCheck) { cartBean.getData().get(groupPosition).setGroupIsChecked(true); notifyDataSetChanged(); } else { cartBean.getData().get(groupPosition).setGroupIsChecked(false); notifyDataSetChanged(); } } panduan(); } }); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } class ViewHolder { CheckBox checkBox; TextView textView,name,price,num; ImageView img; } private void panduan(){ // 然后判断是否所有的组都被选中 boolean allGroup = true; // 如果都被选中那么给全选赋值 for (int i = 0; i < cartBean.getData().size(); i++) { if (!cartBean.getData().get(i).isGroupIsChecked()) { allGroup = false; } } // 然后计算一下选中数量 int num = 0; int price = 0; for (int i = 0; i < cartBean.getData().size(); i++) { for (int j = 0; j < cartBean.getData().get(i).getList().size(); j++) { if (cartBean.getData().get(i).getList().get(j).isChildIsChecked()) { num++; price = price + (cartBean.getData().get(i).getList().get(j).getPrice())*(cartBean.getData().get(i).getList().get(j).getNum()); } } } EventBus.getDefault().post(new ReturnCart(allGroup,num,price)); }
Fragment中的onCreate方法中将EventBus注册
/ 注册EventBus EventBus.getDefault().register(this); 从EventBus中获取数据//通过注解的方式指定接收消息的线程 @Subscribe(threadMode = ThreadMode.MAIN) public void messageEvent(ReturnCart returnCart) { all.setChecked(returnCart.isAll()); jiesuan.setText("结算("+returnCart.getNum()+")"); price.setText("¥"+returnCart.getPrice()); }
全选的点击事件
all.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(all.isChecked()){ isno(true,cartBean); }else { isno(false,cartBean); } } });
private void isno(boolean ischecked,CartBean cartBean){ for (int i = 0;i < cartBean.getData().size();i++){ cartBean.getData().get(i).setGroupIsChecked(ischecked); for (int j = 0;j<cartBean.getData().get(i).getList().size();j++){ cartBean.getData().get(i).getList().get(j).setChildIsChecked(ischecked); } } adapter.notifyDataSetChanged(); jisuan(cartBean); } private void jisuan(CartBean cartBean){ // 然后计算一下选中数量 int num = 0; int price = 0; for (int i = 0; i < cartBean.getData().size(); i++) { for (int j = 0; j < cartBean.getData().get(i).getList().size(); j++) { if (cartBean.getData().get(i).getList().get(j).isChildIsChecked()) { num++; price = price + (cartBean.getData().get(i).getList().get(j).getPrice())*(cartBean.getData().get(i).getList().get(j).getNum()); } } } jiesuan.setText("结算("+num+")"); this.price.setText("¥"+price); }