adpater
package com.sn.shopcat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
/**
* date:2018/11/20
* author:易宸锋(dell)
* function:继承BaseExpandableListAdapter
* 1.基本方法做些设置
* 2.创建组Item的布局和ViewHolder及子Item的布局和ViewHolder
* 3.做一下基本的控件数据设置
*/
class MyAdapter extends BaseExpandableListAdapter {
private List<CartInfo.DataBean> mSellerData;
public MyAdapter(List<CartInfo.DataBean> sellerData) {
mSellerData = sellerData;
}
@Override//决定了有多少个组
public int getGroupCount() {
//三元运算符,提供程序运行效率
return mSellerData == null ? 0 : mSellerData.size();
}
@Override//一个组里面有多少个子条目
public int getChildrenCount(int groupPosition) {
return mSellerData.get(groupPosition).getList() == null ? 0 : mSellerData.get(groupPosition).getList().size();
}
//就是和ListView差不多了
@Override
public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
//先拿到Bean里组的数据,看hiJson
CartInfo.DataBean dataBean = mSellerData.get(groupPosition);
ParentViewHolder parentViewHolder;
if (convertView == null) {
convertView = View.inflate(parent.getContext(), R.layout.item_cart_parent, null);
parentViewHolder = new ParentViewHolder(convertView);
convertView.setTag(parentViewHolder);
} else {
parentViewHolder = (ParentViewHolder) convertView.getTag();
}
//商家名字
parentViewHolder.seller_name_tv.setText(dataBean.getSellerName());
//D.根据当前商家的所有商品,确定商家的CheckBox是否选中
boolean currentSellerAllProductSelected = isCurrentSellerAllProductSelected(groupPosition);
//D.根据这个boolean判断是否选中
parentViewHolder.seller_cb.setChecked(currentSellerAllProductSelected);
//D.设置点击CheckBox
parentViewHolder.seller_cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mOnCartListChangeListener !=null){
mOnCartListChangeListener.onSellerCheckedChange(groupPosition);
}
}
});
return convertView;
}
@Override//子条目的布局
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
//子条目所有的数据
CartInfo.DataBean dataBean = mSellerData.get(groupPosition);
List<CartInfo.DataBean.ListBean> list = dataBean.getList();
//拿到List集合里具体商品
CartInfo.DataBean.ListBean listBean = list.get(childPosition);
ChildViewHolder childViewHolder;
if (convertView == null) {
convertView = View.inflate(parent.getContext(), R.layout.item_cart_child, null);
childViewHolder = new ChildViewHolder(convertView);
convertView.setTag(childViewHolder);
} else {
childViewHolder = (ChildViewHolder) convertView.getTag();
}
//设置商品名字
childViewHolder.product_title_name_tv.setText(listBean.getTitle());
//设置商品单价
childViewHolder.product_price_tv.setText(listBean.getPrice() + "");
//设置复选框是否选中
childViewHolder.child_cb.setChecked(listBean.getSelected() == 1);
//设置组合式自定义控件内部的数量
childViewHolder.add_remove_view.setNumber(listBean.getNum());
//D.设置商品CheckBox的点击事件,通过接口回调,暴露给外面
childViewHolder.child_cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mOnCartListChangeListener != null){
mOnCartListChangeListener.onProductCheckedChange(groupPosition,childPosition);
}
}
});
//D.设置商品数量的点击事件,通过接口回调,暴露给外面
childViewHolder.add_remove_view.setOnNumberChangeListener(new MyAddSubView.OnNumberChangeListener() {
@Override
public void onNumberChange(int num) {
if (mOnCartListChangeListener !=null){
mOnCartListChangeListener.onProducNumberChange(groupPosition,childPosition,num);
}
}
});
return convertView;
}
///B.写一些购物车的特有的方法/
//B.判断当前商家所有商品是否被选中(一种是所有的都被选中,一种是没有所有的都选中)
public boolean isCurrentSellerAllProductSelected(int groupPosition) {
//拿到对应组的数据
CartInfo.DataBean dataBean = mSellerData.get(groupPosition);
//拿到商家的所有商品数据,是一个集合
List<CartInfo.DataBean.ListBean> list = dataBean.getList();
for (CartInfo.DataBean.ListBean listBean : list) {
//判断这个组的所有商品是否被选中,如果有一个没有被选中,那么商家就是未选中状态
if (listBean.getSelected() == 0) {
return false;
}
}
return true;
}
//B.底部有一个全选按钮的逻辑,也要判断,不过范围更大,是所有的商品是否被选中(一种是所有的都被选中,一种是没有所有的都选中)
public boolean isAllProductsSelected() {
for (int x = 0; x < mSellerData.size(); x++) {
CartInfo.DataBean dataBean = mSellerData.get(x);
List<CartInfo.DataBean.ListBean> list = dataBean.getList();
for (int j = 0; j < list.size(); j++) {
//判断这个组的所有商品是否被选中,如果有一个没有被选中,那么全选按钮就是未选中状态
if (list.get(j).getSelected() == 0){
return false;
}
}
}
return true;
}
//B.计算商品总的数量
public int calculateTotalNumber(){
int totalNumber = 0;
for(int x=0; x<mSellerData.size(); x++){
CartInfo.DataBean dataBean = mSellerData.get(x);
List<CartInfo.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();
totalNumber +=num;
}
}
}
return totalNumber;
}
//B.计算总价
public float calculateTotalPrice(){
float totalPrice = 0 ;
for(int x=0; x<mSellerData.size(); x++){
CartInfo.DataBean dataBean = mSellerData.get(x);
List<CartInfo.DataBean.ListBean> list = dataBean.getList();
for(int j=0; j< list.size(); j++){
//商品价格只算选中
if(list.get(j).getSelected() == 1){
//拿到了商品的数量
float price = list.get(j).getPrice();
int num = list.get(j).getNum();
totalPrice += price * num;
}
}
}
return totalPrice;
}
/C.根据用户的选择,改变选框里的状态///
//C.当商品组的全选框点击时,更新所有商品的状态
public void changeCurrentSellerAllProductsStatus(int groupPosition , boolean isSelected){
CartInfo.DataBean dataBean = mSellerData.get(groupPosition);
List<CartInfo.DataBean.ListBean> list = dataBean.getList();
for(int x=0; x<list.size(); x++){
CartInfo.DataBean.ListBean listBean = list.get(x);
listBean.setSelected(isSelected ? 1 : 0);
}
}
//C.当商家子条目的全选框选中时,更新其状态
public void changeCurrentProductStatus(int groupPosition , int childPositon){
CartInfo.DataBean dataBean = mSellerData.get(groupPosition);
List<CartInfo.DataBean.ListBean> list = dataBean.getList();
CartInfo.DataBean.ListBean listBean = list.get(childPositon);
listBean.setSelected(listBean.getSellerid() == 0 ? 1 : 0 );
}
//C.设置所有商品的状态
public void changeAllProductStatus(boolean selected){
for(int x=0; x<mSellerData.size() ; x++){
CartInfo.DataBean dataBean = mSellerData.get(x);
List<CartInfo.DataBean.ListBean> list = dataBean.getList();
for(int j=0; j<list.size(); j++){
list.get(j).setSelected(selected ? 1 : 0);
}
}
}
//C.当加减器被点击时,调用,改变里面当前商品的数量 参数1定位那个商家 参数2定位哪个商品 参数3定位改变具体的数量是多少
public void changeCurrentProductNumber(int groupPosition,int childPositon,int number){
CartInfo.DataBean dataBean = mSellerData.get(groupPosition);
List<CartInfo.DataBean.ListBean> list = dataBean.getList();
CartInfo.DataBean.ListBean listBean = list.get(childPositon);
listBean.setNum(number);
}
D.定义接口的固定形式//
//D.
public interface onCartListChangeListener{
/**
* 当商家的checkBox点击时回调
*/
void onSellerCheckedChange(int groupPosition);
/**
* 当点击子条目商品的CheckBox回调
*/
void onProductCheckedChange(int groupPosition ,int childPosition);
/**
* 当点击加减按钮的回调
*/
void onProducNumberChange(int groupPosition , int childPosition , int number);
}
//D.
onCartListChangeListener mOnCartListChangeListener;
//D.
public void setOnCartListChangeListener(onCartListChangeListener onCartListChangeListener){
mOnCartListChangeListener = onCartListChangeListener ;
}
创建的VIewHolder/
public static class ParentViewHolder {
public CheckBox seller_cb;
public TextView seller_name_tv;
public ParentViewHolder(View rootView) {
this.seller_cb = (CheckBox) rootView.findViewById(R.id.seller_cb);
this.seller_name_tv = (TextView) rootView.findViewById(R.id.seller_name_tv);
}
}
public static class ChildViewHolder {
public CheckBox child_cb;
public ImageView product_icon_iv;
public TextView product_title_name_tv;
public TextView product_price_tv;
public MyAddSubView add_remove_view;
public ChildViewHolder(View rootView) {
this.child_cb = (CheckBox) rootView.findViewById(R.id.child_cb);
this.product_icon_iv = (ImageView) rootView.findViewById(R.id.product_icon_iv);
this.product_title_name_tv = (TextView) rootView.findViewById(R.id.product_title_name_tv);
this.product_price_tv = (TextView) rootView.findViewById(R.id.product_price_tv);
this.add_remove_view = (MyAddSubView) rootView.findViewById(R.id.add_remove_view);
}
}
//BaseExpandableListAdapter必须要实习,但是没有作用的代码///
@Override//不管
public boolean hasStableIds() {
return false;
}
@Override//不管
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
@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;
}
}
main
package com.ali.cartdemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class MainActivity extends AppCompatActivity {
@BindView(R.id.el_cart)
ExpandableListView elCart;
@BindView(R.id.cb_cart_all_select)
CheckBox cbCartAllSelect;
@BindView(R.id.tv_cart_total_price)
TextView tvCartTotalPrice;
@BindView(R.id.btn_cart_pay)
Button btnCartPay;
private MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initData();
}
private void initData() {
String url = "http://www.zhaoapi.cn/product/getCarts";
Map<String, String> map = new HashMap<>();
map.put("uid", "71");
OkhtttpUtils.getInstance().doPost(url, map, new OkhtttpUtils.OkCallback() {
@Override
public void onFailure(Exception e) {
}
@Override
public void onResponse(String json) {
//解析Json数据
CartInfo cartInfo = new Gson().fromJson(json, CartInfo.class);
//如果字段是0的话,代表网络请求成功,一遍请求成功是200但是后台不专业
if ("0".equals(cartInfo.getCode())) {
List<CartInfo.DataBean> sellerData = cartInfo.getData();
//设置adapter
myAdapter = new MyAdapter(sellerData);
//B.设置ExpandableListView的监听事件
myAdapter.setOnCartListChangeListener(new MyAdapter.onCartListChangeListener() {
@Override//点击商家的CheckBox的回调
public void onSellerCheckedChange(int groupPosition) {
//商家被点击
boolean currentSellerAllProductSelected = myAdapter.isCurrentSellerAllProductSelected(groupPosition);
myAdapter.changeCurrentSellerAllProductsStatus(groupPosition, !currentSellerAllProductSelected);
myAdapter.notifyDataSetChanged();
//B.刷新底部数据
refreshSelectedAndTotalPriceAndTotalNumber();
}
//点击商品的CheckBox的回调
@Override
public void onProductCheckedChange(int groupPosition, int childPosition) {
//点击商品得checkbox
myAdapter.changeCurrentProductStatus(groupPosition,childPosition);
myAdapter.notifyDataSetChanged();
//B.刷新底部数据
refreshSelectedAndTotalPriceAndTotalNumber();
}
//点击加减按钮的回调
@Override
public void onProductNumberChange(int groupPosition, int childPosition, int number) {
//当加减被点击
myAdapter.changeCurrentProductNumber(groupPosition,childPosition,number);
myAdapter.notifyDataSetChanged();
//B.刷新底部数据
refreshSelectedAndTotalPriceAndTotalNumber();
}
});
elCart.setAdapter(myAdapter);
//展开二级列表
for (int i=0;i<sellerData.size();i++){
elCart.expandGroup(i);
}
//B.刷新checkbox状态和总价和总数量
refreshSelectedAndTotalPriceAndTotalNumber();
}
}
});
}
//B.刷新checkbox状态和总价和总数量
private void refreshSelectedAndTotalPriceAndTotalNumber() {
//去判断是否所有得商品都被选中
boolean allProductsSelected = myAdapter.isAllProductsSelected();
//设置给全选checkBox
cbCartAllSelect.setChecked(allProductsSelected);
//计算总价
float totalPrice = myAdapter.calculateTotalPrice();
tvCartTotalPrice.setText("总价 " + totalPrice);
//计算总数量
int totalNumber = myAdapter.calculateTotalNumber();
btnCartPay.setText("去结算(" + totalNumber + ")");
}
@OnClick(R.id.cb_cart_all_select)
public void onViewClicked() {
//底部全选按钮
//时候所有得商品都被选中
boolean allProductsSelected = myAdapter.isAllProductsSelected();
myAdapter.changeAllProductsStatus(!allProductsSelected);
myAdapter.notifyDataSetChanged();
//刷新底部数据
refreshSelectedAndTotalPriceAndTotalNumber();
}
}