//自定义加减器
public class MyAddView extends LinearLayout implements View.OnClickListener { private TextView removeTV; private TextView addTV; private TextView numTV; private int number = 1; public MyAddView(Context context) { super(context); } public MyAddView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); View view = inflate(context, R.layout.add_remove_view_layout, this); removeTV = view.findViewById(R.id.tv_remove); addTV = view.findViewById(R.id.tv_add); numTV = view.findViewById(R.id.tv_num); removeTV.setOnClickListener(this); addTV.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.tv_remove: if (number > 1) { --number; numTV.setText(number + ""); if (setOnNumberChange != null) { setOnNumberChange.numChangeListener(number); } else { Toast.makeText(getContext(), "不能少啦", Toast.LENGTH_SHORT).show(); } } break; case R.id.tv_add: ++number; numTV.setText(number + ""); if (setOnNumberChange != null) { setOnNumberChange.numChangeListener(number); } break; } } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; numTV.setText(number + ""); } public void setSetOnNumberChange(setOnNumberChangeListener setOnNumberChange) { this.setOnNumberChange = setOnNumberChange; } private setOnNumberChangeListener setOnNumberChange; public interface setOnNumberChangeListener { void numChangeListener(int num); } }
//价减器xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="80dp" android:layout_height="35dp" android:layout_gravity="center_vertical" android:background="#000" android:gravity="center_vertical" android:padding="2dp"> <TextView android:id="@+id/tv_remove" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="#fff" android:gravity="center" android:text="-" android:textSize="16dp" /> <TextView android:id="@+id/tv_num" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginLeft="2dp" android:layout_weight="1" android:background="#fff" android:gravity="center" android:text="1" android:textSize="16dp" /> <TextView android:id="@+id/tv_add" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginLeft="2dp" android:layout_weight="1" android:background="#fff" android:gravity="center" android:text="+" android:textSize="16dp" /> </LinearLayout>
//Main
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private static final String TAG = "MainActivity+++++++++++"; private ExpandableListView mMainEalv; private CheckBox mCbCardSelect; /** * 总价:¥1200.00 */ private TextView mTvPrice; /** * 去结算(0) */ private Button mBtnNum; private String url = "网络请求的接口"; private MyAdapter myAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); } private void initView() { mMainEalv = findViewById(R.id.main_ealv); mCbCardSelect = findViewById(R.id.cb_card_select); mTvPrice = findViewById(R.id.tv_price); mBtnNum = findViewById(R.id.btn_num); mCbCardSelect.setOnClickListener(this); } private void initData() { OkHttpUtil okHttpUtil = OkHttpUtil.getInListener(); okHttpUtil.doGet(url, new OkHttpUtil.OkHttpCallBack() { @Override public void Success(String json) { Log.d(TAG, "Success: " + json); Gson gson = new Gson(); final CartBean cartBean = gson.fromJson(json, CartBean.class); List<CartBean.DataBean> data = cartBean.getData(); myAdapter = new MyAdapter(data); myAdapter.setOnCartListChangeListener(new MyAdapter.onCartListChangeListener() { @Override public void onSellerCheckedChange(int groupPosition) { //商家被点击 boolean curllentSellerAllSelected = myAdapter.isCurllentSellerAllSelected(groupPosition); myAdapter.ChangeSellerStatus(groupPosition, !curllentSellerAllSelected); //刷新底部数据 myAdapter.notifyDataSetChanged(); CheckedChangeAndPriceAndNum(); } @Override public void onProductCheckedChange(int groupPosition, int childPosition) { //点击商品的checkbox myAdapter.changeProductStatus(groupPosition,childPosition); //刷新底部数据 myAdapter.notifyDataSetChanged(); CheckedChangeAndPriceAndNum(); } @Override public void onProductNumberChange(int groupPosition, int childPosition, int number) { myAdapter.changeProductNum(groupPosition,childPosition,number); //刷新底部数据 myAdapter.notifyDataSetChanged(); CheckedChangeAndPriceAndNum(); } }); //隐藏小箭头 mMainEalv.setGroupIndicator(null); mMainEalv.setAdapter(myAdapter); //展开所有商品列表 for (int i = 0; i < data.size(); i++) { mMainEalv.expandGroup(i); } //刷新底部数据 CheckedChangeAndPriceAndNum(); } @Override public void Error(Exception e) { Log.d(TAG, "Error: " + e); } }); } //刷新checkbox状态和总价数量 private void CheckedChangeAndPriceAndNum() { //去判断是否所有商品都被选中 boolean allProductSelected = myAdapter.isAllProductSelected(); //赋给全选checkbox mCbCardSelect.setChecked(allProductSelected); //计算总价 double price = myAdapter.ChangePrice(); mTvPrice.setText("总价¥:" + price); //计算总数量 int num = myAdapter.ChangeNum(); mBtnNum.setText("去结算(" + num + ")"); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.cb_card_select: //去判断是否所有商品都被选中 boolean allProductSelected = myAdapter.isAllProductSelected(); //是否改变所有checkbox的状态 myAdapter.isAllProductStatus(!allProductSelected); myAdapter.notifyDataSetChanged(); //刷新底部数据 CheckedChangeAndPriceAndNum(); break; } } }
//Main xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <ExpandableListView android:id="@+id/main_ealv" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="9" /> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:background="#F0FFF0" android:layout_weight="1"> <CheckBox android:id="@+id/cb_card_select" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginLeft="5dp" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginTop="18dp" android:text="全选" android:textSize="16dp" /> <TextView android:id="@+id/tv_price" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:layout_marginLeft="10dp" android:layout_marginTop="14dp" android:text="总价:¥1200.00" android:textColor="#f00" android:textSize="18dp" /> <Button android:id="@+id/btn_num" android:layout_width="100dp" android:layout_height="match_parent" android:text="去结算(0)" /> </LinearLayout> </LinearLayout>
//ExpandableListAdapter
public class MyAdapter extends BaseExpandableListAdapter { private List<CartBean.DataBean> data; public MyAdapter(List<CartBean.DataBean> data) { this.data = data; } @Override public int getGroupCount() { return data == null ? 0 : data.size(); } @Override public int getChildrenCount(int groupPosition) { return data.get(groupPosition).getList() == null ? 0 : data.get(groupPosition).getList().size(); } @Override public Object getGroup(int groupPosition) { return null; } @Override public Object getChild(int groupPosition, int childPosition) { return null; } @Override public long getGroupId(int groupPosition) { return 0; } @Override public long getChildId(int groupPosition, int childPosition) { return 0; } @Override public boolean hasStableIds() { return false; } @Override public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { ViewHolder1 viewHolder1; if (convertView == null) { convertView = View.inflate(parent.getContext(), R.layout.item_cart_parent, null); viewHolder1 = new ViewHolder1(convertView); convertView.setTag(viewHolder1); } else { viewHolder1 = (ViewHolder1) convertView.getTag(); } viewHolder1.tvParent.setText(data.get(groupPosition).getSellerName()); //根据商品确定商家的checkbox是否被选中 boolean curllentSellerAllSelected = isCurllentSellerAllSelected(groupPosition); viewHolder1.cbParent.setChecked(curllentSellerAllSelected); viewHolder1.cbParent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //点击商家的checkbox if (onCartListChangeListener != null) { onCartListChangeListener.onSellerCheckedChange(groupPosition); } } }); return convertView; } //当前商家的商品是否全被被选中 public boolean isCurllentSellerAllSelected(int groupPosition) { CartBean.DataBean dataBean = data.get(groupPosition); List<CartBean.DataBean.ListBean> list = dataBean.getList(); for (CartBean.DataBean.ListBean listBean : list) { //只要有一个未选中商家就未选中 if (listBean.getSelected() == 0) { return false; } } return true; } @Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { CartBean.DataBean dataBean = data.get(groupPosition); List<CartBean.DataBean.ListBean> list = dataBean.getList(); CartBean.DataBean.ListBean listBean = list.get(childPosition); ViewHolder2 viewHolder2; if (convertView == null) { convertView = View.inflate(parent.getContext(), R.layout.item_cart_child, null); viewHolder2 = new ViewHolder2(convertView); convertView.setTag(viewHolder2); } else { viewHolder2 = (ViewHolder2) convertView.getTag(); } viewHolder2.tvTitleChild.setText(listBean.getTitle()); viewHolder2.tvPriceChild.setText("¥:" + listBean.getPrice() + ""); String images = listBean.getImages(); String[] pic = images.split("\\|"); ImageLoader.getInstance().displayImage(pic[0], viewHolder2.ivPicChild, MyApp.getOptions()); //商品checkbox的状态 viewHolder2.cbChild.setChecked(listBean.getSelected() == 1); viewHolder2.cbChild.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //点击商品的checkbxo if (onCartListChangeListener != null) { onCartListChangeListener.onProductCheckedChange(groupPosition, childPosition); } } }); viewHolder2.myChild.setNumber(listBean.getNum()); viewHolder2.myChild.setSetOnNumberChange(new MyAddView.setOnNumberChangeListener() { @Override public void numChangeListener(int num) { //拿到最小的数量 if (onCartListChangeListener != null) { onCartListChangeListener.onProductNumberChange(groupPosition, childPosition, num); } } }); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } //所有商品是否被选中 public boolean isAllProductSelected() { for (int i = 0; i < data.size(); i++) { CartBean.DataBean dataBean = data.get(i); List<CartBean.DataBean.ListBean> list = dataBean.getList(); for (int j = 0; j < list.size(); j++) { if (list.get(j).getSelected() == 0) { return false; } } } return true; } //计算总价 public double ChangePrice() { double totalPrice = 0; for (int i = 0; i < data.size(); i++) { CartBean.DataBean dataBean = data.get(i); List<CartBean.DataBean.ListBean> list = dataBean.getList(); for (int j = 0; j < list.size(); j++) { //判断是否被选中 if (list.get(j).getSelected() == 1) { double price = list.get(j).getPrice(); int num = list.get(j).getNum(); totalPrice += price * num; } } } return totalPrice; } //计算总数量 public int ChangeNum() { int tatolNum = 0; for (int i = 0; i < data.size(); i++) { CartBean.DataBean dataBean = data.get(i); List<CartBean.DataBean.ListBean> list = dataBean.getList(); for (int j = 0; j < list.size(); j++) { if (list.get(j).getSelected() == 1) { int num = list.get(j).getNum(); tatolNum += num; } } } return tatolNum; } public void isAllProductStatus(boolean b) { for (int i = 0; i < data.size(); i++) { CartBean.DataBean dataBean = data.get(i); List<CartBean.DataBean.ListBean> list = dataBean.getList(); for (int j = 0; j < list.size(); j++) { list.get(j).setSelected(b ? 1 : 0); } } } //当商家的checkbox被点击的时候回调用,设置当前商家所有的商品状态 public void ChangeSellerStatus(int groupPosition, boolean ischecked) { CartBean.DataBean dataBean = data.get(groupPosition); List<CartBean.DataBean.ListBean> list = dataBean.getList(); for (int i = 0; i < list.size(); i++) { CartBean.DataBean.ListBean listBean = list.get(i); listBean.setSelected(ischecked ? 1 : 0); } } //当商品的checkbox被点击,改变当前的商品状态 public void changeProductStatus(int groupPosition, int childPosition) { CartBean.DataBean dataBean = data.get(groupPosition); List<CartBean.DataBean.ListBean> list = dataBean.getList(); CartBean.DataBean.ListBean listBean = list.get(childPosition); listBean.setSelected(listBean.getSelected() == 0 ? 1 : 0); } //当加减器被点击,改变num的数量 public void changeProductNum(int groupPosition, int childPosition, int number) { CartBean.DataBean dataBean = data.get(groupPosition); List<CartBean.DataBean.ListBean> list = dataBean.getList(); CartBean.DataBean.ListBean listBean = list.get(childPosition); listBean.setNum(number); } static class ViewHolder1 { @BindView(R.id.cb_parent) CheckBox cbParent; @BindView(R.id.tv_parent) TextView tvParent; ViewHolder1(View view) { ButterKnife.bind(this, view); } } static class ViewHolder2 { @BindView(R.id.cb_child) CheckBox cbChild; @BindView(R.id.iv_pic_child) ImageView ivPicChild; @BindView(R.id.tv_title_child) TextView tvTitleChild; @BindView(R.id.tv_price_child) TextView tvPriceChild; @BindView(R.id.my_child) MyAddView myChild; ViewHolder2(View view) { ButterKnife.bind(this, view); } } public void setOnCartListChangeListener(MyAdapter.onCartListChangeListener onCartListChangeListener) { this.onCartListChangeListener = onCartListChangeListener; } onCartListChangeListener onCartListChangeListener; public interface onCartListChangeListener { void onSellerCheckedChange(int groupPosition); void onProductCheckedChange(int groupPosition, int childPosition); void onProductNumberChange(int groupPosition, int childPosition, int number); } }
//父布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="60dp" android:gravity="center_vertical"> <CheckBox android:id="@+id/cb_parent" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_parent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:textSize="22dp" android:text="123456789" android:textColor="#f00" /> </LinearLayout>
//子布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="120dp" android:layout_marginLeft="10dp" android:gravity="center_vertical"> <CheckBox android:id="@+id/cb_child" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/iv_pic_child" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginLeft="20dp" android:scaleType="fitXY" android:src="@mipmap/ic_launcher" /> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <TextView android:id="@+id/tv_title_child" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="end" android:maxLines="4" android:layout_marginLeft="10dp" android:text="123456566" /> <TextView android:id="@+id/tv_price_child" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="22dp" android:textColor="#f00" android:layout_marginLeft="10dp" android:text="¥:0.0" /> </LinearLayout> <com.bwie.cartdemo.MyAddView android:id="@+id/my_child" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dp" /> </LinearLayout>