package com.example.shoppingcart.model;
import android.view.LayoutInflater;
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 com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.shoppingcart.R;
import com.example.shoppingcart.view.activity.AddAndRemove;
import java.util.List;
public class CartAdapter extends BaseExpandableListAdapter {
private List<CartBean.ResultBean> list;
public CartAdapter(List<CartBean.ResultBean> list) {
this.list = list;
}
@Override
public int getGroupCount() {
return list == null ? 0 : list.size();
}
@Override
public int getChildrenCount(int i) {
return list.get(i).getShoppingCartList() == null ? 0 : list.get(i).getShoppingCartList().size();
}
@Override
public Object getGroup(int i) {
return null;
}
@Override
public Object getChild(int i, int i1) {
return null;
}
@Override
public long getGroupId(int i) {
return 0;
}
@Override
public long getChildId(int i, int i1) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
final GroupViewHolder groupViewHolder;
if (view == null) {
view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.group_item, viewGroup, false);
groupViewHolder = new GroupViewHolder(view);
view.setTag(groupViewHolder);
} else {
groupViewHolder = (GroupViewHolder) view.getTag();
}
groupViewHolder.textview_shangjia.setText(list.get(i).getCategoryName());
//当前商家的货物列表
final List<CartBean.ResultBean.ShoppingCartListBean> shoppingCartList = list.get(i).getShoppingCartList();
// TODO: 2019/9/7 调用方法对所有的商品进行判断,如果有一个为false则商家为false
boolean getgroupischeck = getgroupischeck(shoppingCartList);
//对商家进行赋值
groupViewHolder.checkbox_shangjia.setChecked(getgroupischeck);
groupViewHolder.checkbox_shangjia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//点击后将原来的状态置返
// TODO: 2019/9/7 调用方法对所有的商品进行判断,如果有一个为false则商家为false
boolean getgroupischeck = getgroupischeck(shoppingCartList);
//点击取反
boolean b1 = !getgroupischeck;
//讲置返后的状态设置给当前商家下的所有商品
for (int j = 0; j < shoppingCartList.size(); j++) {
CartBean.ResultBean.ShoppingCartListBean shoppingCartListBean = shoppingCartList.get(j);
shoppingCartListBean.setIschecked(b1);
}
//调用接口与底部实现联动
onCartAdapterChangeListener.onGroupCheckedStatusChange(i);
notifyDataSetChanged();
}
});
return view;
}
@Override
public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
//获取到当前商家的商品集合
final List<CartBean.ResultBean.ShoppingCartListBean> shoppingCartList = list.get(i).getShoppingCartList();
ChildViewHolder childViewHolder;
if (view == null) {
view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.child_item, viewGroup, false);
childViewHolder = new ChildViewHolder(view);
view.setTag(childViewHolder);
} else {
childViewHolder = (ChildViewHolder) view.getTag();
}
//赋值
childViewHolder.product_price_tv.setText(shoppingCartList.get(i1).getPrice() + "");
childViewHolder.product_title_name_tv.setText(shoppingCartList.get(i1).getCommodityName() + "");
Glide.with(viewGroup.getContext())
.load(shoppingCartList.get(i1).getPic())
.error(R.mipmap.ic_launcher)
.apply(RequestOptions.circleCropTransform())
.into(childViewHolder.product_icon_iv);
childViewHolder.child_cb.setChecked(shoppingCartList.get(i1).isIschecked());
//商品的点击事件
childViewHolder.child_cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//修改当前商品checkbox的值
shoppingCartList.get(i1).setIschecked(!shoppingCartList.get(i1).isIschecked());
//刷新适配器重新计算商家的状态
notifyDataSetChanged();
//通知底部刷新价格
onCartAdapterChangeListener.onChildCheckedStatusChange(i, i1);
}
});
//改变加减器的值
childViewHolder.addandremove.setCount(shoppingCartList.get(i1).getCount());
//加减器的监听
childViewHolder.addandremove.setOnNumberChangeListener(new AddAndRemove.OnNumberChangeListener() {
@Override
public void changed(int num) {
//做了三件事 1、修改商品数据的数量 2、刷新适配器(重新修改view状态)3 通知底部状态栏改变
//修改商品数据的数量
shoppingCartList.get(i1).setCount(num);
//刷新适配器
notifyDataSetChanged();
//通知底部刷新价格
if (onCartAdapterChangeListener != null) {
onCartAdapterChangeListener.onChildNumberChange(i, i1);
}
}
});
return view;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return false;
}
public static
class GroupViewHolder {
public View rootView;
public CheckBox checkbox_shangjia;
public TextView textview_shangjia;
public GroupViewHolder(View rootView) {
this.rootView = rootView;
this.checkbox_shangjia = (CheckBox) rootView.findViewById(R.id.checkbox_shangjia);
this.textview_shangjia = (TextView) rootView.findViewById(R.id.textview_shangjia);
}
}
public static
class ChildViewHolder {
public View rootView;
public CheckBox child_cb;
public ImageView product_icon_iv;
public TextView product_title_name_tv;
public TextView product_price_tv;
public AddAndRemove addandremove;
public ChildViewHolder(View rootView) {
this.rootView = 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.addandremove = (AddAndRemove) rootView.findViewById(R.id.addandremove);
}
}
/**
* 获取当前状态
*/
private boolean getgroupischeck(List<CartBean.ResultBean.ShoppingCartListBean> shoppingCartList) {
//对所有的商品进行判断,如果有一个为false则商家为false
boolean groupIsChecked = true;
for (int j = 0; j < shoppingCartList.size(); j++) {
CartBean.ResultBean.ShoppingCartListBean shoppingCartListBean = shoppingCartList.get(j);
if (!shoppingCartListBean.isIschecked()) {
groupIsChecked = false;
break;
}
}
return groupIsChecked;
}
public interface OnCartAdapterChangeListener {
void onChildCheckedStatusChange(int groupPositon, int childPosition);
void onChildNumberChange(int groupPositon, int childPosition);
void onGroupCheckedStatusChange(int groupPositon);
}
OnCartAdapterChangeListener onCartAdapterChangeListener;
public void setOnCartAdapterChangeListener(OnCartAdapterChangeListener onCartAdapterChangeListener) {
this.onCartAdapterChangeListener = onCartAdapterChangeListener;
}
/**
* 计算所有商品是否全部被选中
*/
public boolean isAllChecked() {
boolean isAllChecked = true;
for (int i = 0; i < list.size(); i++) {
CartBean.ResultBean resultBean = list.get(i);
List<CartBean.ResultBean.ShoppingCartListBean> shoppingCartList = resultBean.getShoppingCartList();
for (int j = 0; j < shoppingCartList.size(); j++) {
boolean ischecked = shoppingCartList.get(j).isIschecked();
if (!ischecked) {
isAllChecked = false;
}
}
}
return isAllChecked;
}
/**
* 计算总价
*/
public double AllOfThePrice() {
double totalorice = 0.0;
for (int i = 0; i < list.size(); i++) {
List<CartBean.ResultBean.ShoppingCartListBean> shoppingCartList = list.get(i).getShoppingCartList();
for (int j = 0; j < shoppingCartList.size(); j++) {
boolean ischecked = shoppingCartList.get(j).isIschecked();
if (ischecked) {
totalorice += shoppingCartList.get(j).getPrice() * shoppingCartList.get(j).getCount();
}
}
}
return totalorice;
}
/**
* 计算总数
*/
public int AllOfTheCount() {
int totalorice = 0;
for (int i = 0; i < list.size(); i++) {
List<CartBean.ResultBean.ShoppingCartListBean> shoppingCartList = list.get(i).getShoppingCartList();
for (int j = 0; j < shoppingCartList.size(); j++) {
boolean ischecked = shoppingCartList.get(j).isIschecked();
if (ischecked) {
totalorice += shoppingCartList.get(j).getCount();
}
}
}
return totalorice;
}
/**
* 全选按钮改变所有商品的状态
*/
public void changeAllCheckBoxStatus(boolean is) {
for (int i = 0; i < list.size(); i++) {
List<CartBean.ResultBean.ShoppingCartListBean> shoppingCartList = list.get(i).getShoppingCartList();
for (int j = 0; j < shoppingCartList.size(); j++) {
shoppingCartList.get(j).setIschecked(is);
}
}
notifyDataSetInvalidated();
}
}
在activity中对底部和购物车的数据市实现联动
package com.example.shoppingcart.view.activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.shoppingcart.R;
import com.example.shoppingcart.model.CartAdapter;
import com.example.shoppingcart.model.CartBean;
import com.example.shoppingcart.presenter.CartPresenter;
import com.example.shoppingcart.view.interfaces.ContractClass;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CartActivity extends AppCompatActivity implements ContractClass.LoginBase, View.OnClickListener {
private CartPresenter cartPresenter;
private ExpandableListView expandablelistview;
private CheckBox checkbox_quanxuan;
private TextView textview;
private CartAdapter cartAdapter;
private ArrayList<CartBean.ResultBean> list;
private Button button_jiesuan;
private double anInt;
private int i;
private boolean allChecked;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shoppingcart_main);
initView();
cartPresenter = new CartPresenter();
cartPresenter.attachView(this);
Intent intent = getIntent();
String userid = intent.getStringExtra("userid");
String userid2 = intent.getStringExtra("userid2");
Map<String, String> headersMap = new HashMap<>();
headersMap.put("userId", userid);
headersMap.put("sessionId", userid2);
cartPresenter.Cart("http://172.17.8.100/", headersMap);
}
@Override
public void response(Object o) {
CartBean loginBean = (CartBean) o;
Log.i("TAG", "response: " + loginBean.getMessage());
if ("0000".equals(loginBean.getStatus())) {
List<CartBean.ResultBean> result = loginBean.getResult();
list.addAll(result);
for (int i = 0; i < list.size(); i++) {
expandablelistview.expandGroup(i);
}
cartAdapter.notifyDataSetChanged();
}
}
private void initView() {
expandablelistview = (ExpandableListView) findViewById(R.id.expandablelistview);
checkbox_quanxuan = (CheckBox) findViewById(R.id.checkbox_quanxuan);
textview = (TextView) findViewById(R.id.textview);
checkbox_quanxuan.setOnClickListener(this);
list = new ArrayList<>();
cartAdapter = new CartAdapter(list);
cartAdapter.setOnCartAdapterChangeListener(new CartAdapter.OnCartAdapterChangeListener() {
@Override
public void onChildCheckedStatusChange(int groupPositon, int childPosition) {
startChange();
}
@Override
public void onChildNumberChange(int groupPositon, int childPosition) {
startChange();
}
@Override
public void onGroupCheckedStatusChange(int groupPositon) {
startChange();
}
});
expandablelistview.setAdapter(cartAdapter);
button_jiesuan = (Button) findViewById(R.id.button_jiesuan);
button_jiesuan.setOnClickListener(this);
}
void startChange() {
i = cartAdapter.AllOfTheCount();
button_jiesuan.setText("数量:" + i);
anInt = cartAdapter.AllOfThePrice();
textview.setText("总价:" + anInt);
allChecked = cartAdapter.isAllChecked();
checkbox_quanxuan.setChecked(allChecked);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.checkbox_quanxuan:
//做了四件事
// 1、拿到旧状态、置反得到新状态
// 2、修改所有商品数据的状态
// 3、刷新适配 (修改view的状态)
// 4、刷新底部状态栏
boolean b = !allChecked;
allChecked = b;
cartAdapter.changeAllCheckBoxStatus(b);
startChange();
break;
case R.id.button_jiesuan:
break;
}
}
}