adapter
package wanghuiqi.bawie.baweicommerceproject.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
import java.util.List;
import wanghuiqi.bawie.baweicommerceproject.R;
import wanghuiqi.bawie.baweicommerceproject.bean.CarBean;
import wanghuiqi.bawie.baweicommerceproject.net.MyAddSubView;
public class CarAdapter extends RecyclerView.Adapter<CarAdapter.Viewholder> {
private Context context;
private List<CarBean.ResultBean> carList;
public List<CarBean.ResultBean> getCarList() {
return carList;
}
public CarAdapter(Context context, List<CarBean.ResultBean> carList) {
this.context = context;
this.carList = carList;
}
@NonNull
@Override
public Viewholder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = View.inflate(context, R.layout.car_item, null);
Viewholder holder = new Viewholder(view);
return holder;
}
@Override
public void onBindViewHolder(@NonNull Viewholder viewholder, final int i) {
final CarBean.ResultBean resultBean = carList.get(i);
viewholder.car_commodityName.setText(resultBean.getCommodityName());
viewholder.car_pic.setImageURI(resultBean.getPic());
viewholder.car_price.setText(resultBean.getPrice() + "");
viewholder.car_add_sub.setNumber(resultBean.getCount());
//设置复选框是否选中
viewholder.car_box.setChecked(resultBean.isSelected());
//设置加减器
viewholder.car_add_sub.setOnNumberChangInterface(new MyAddSubView.onNumberChangInterface() {
@Override
public void onNumberChangListener(int num) {
if (mOnCarClickListener != null) {
mOnCarClickListener.onChangAddSub(i, num);
}
}
});
//设置复选框
viewholder.car_box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//判断复选框选中为true,否则为false
if (isChecked) {
resultBean.setSelected(true);
} else {
resultBean.setSelected(false);
}
isCarCheckAll();
}
});
}
//================购物车的逻辑代码===============
/*商品是否选中*/
public void isCarCheckAll() {
//循环出所有商品的选中状态
for (int i = 0; i < carList.size(); i++) {
//判断是否选中
boolean selected = carList.get(i).isSelected();
if (selected == false) {
mOnCarClickListener.onChangCheckBox(false);
return;
}
}
mOnCarClickListener.onChangCheckBox(true);
}
/*商品数量---加减器*/
public void carChangAddSub(int position, int number) {
CarBean.ResultBean resultBean = carList.get(position);
resultBean.setCount(number);
}
/*计算商品的价格*/
public float totalPrice() {
float totalprice = 0;
for (int i = 0; i < carList.size(); i++) {
CarBean.ResultBean resultBean = carList.get(i);
if (resultBean.isSelected() == true) {
int price = resultBean.getPrice();
int count = resultBean.getCount();
totalprice = price * count;
}
}
return totalprice;
}
/*计算商品数量*/
public int totalNum() {
int totalnum = 0;
for (int i = 0; i < carList.size(); i++) {
CarBean.ResultBean resultBean = carList.get(i);
if (resultBean.isSelected() == true) {
int count = resultBean.getCount();
totalnum += count;
}
}
return totalnum;
}
@Override
public int getItemCount() {
return carList.size();
}
public class Viewholder extends RecyclerView.ViewHolder {
private final CheckBox car_box;
private final SimpleDraweeView car_pic;
private final TextView car_commodityName;
private final TextView car_price;
private final MyAddSubView car_add_sub;
public Viewholder(@NonNull View itemView) {
super(itemView);
car_box = itemView.findViewById(R.id.car_box);
car_pic = itemView.findViewById(R.id.car_pic);
car_commodityName = itemView.findViewById(R.id.car_commodityName);
car_price = itemView.findViewById(R.id.car_price);
car_add_sub = itemView.findViewById(R.id.car_add_sub);
}
}
//创建接口
public interface onCarClickListener {
/*改变复选框*/
void onChangCheckBox(boolean flagBox);
/*加减器*/
void onChangAddSub(int position, int number);
}
//声明接口名
private onCarClickListener mOnCarClickListener;
//暴露方法
public void setOnCarClickListener(onCarClickListener oncarClickListener) {
mOnCarClickListener = oncarClickListener;
}
}
主页面
@Override
protected void initData() {
HttpUtilsManager.getInstance().doGet(UrlNet.URL_CAR, new HttpUtilsManager.HttpInterface() {
@Override
public void failed(Exception e) {
}
@Override
public void success(String data) {
CarBean carBean = new Gson().fromJson(data, CarBean.class);
List<CarBean.ResultBean> result = carBean.getResult();
carAdapter = new CarAdapter(getActivity(), result);
car_recy.setAdapter(carAdapter);
carCheckBoxAndAddSub(result);
}
});
}
private void carCheckBoxAndAddSub(List<CarBean.ResultBean> result) {
carAdapter.setOnCarClickListener(new CarAdapter.onCarClickListener() {
@Override
public void onChangCheckBox(boolean flagBox) {
//复选框全选状态
car_all_box.setChecked(flagBox);
//更新价格
carAllCheckedPrice();
carAllNumber();
}
@Override
public void onChangAddSub(int position, int number) {
carAdapter.carChangAddSub(position, number);
carAllCheckedPrice();
carAllNumber();
}
});
/*全选--复选框状态*/
car_all_box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//获取
List<CarBean.ResultBean> carList = carAdapter.getCarList();
for (int i = 0; i < carList.size(); i++) {
carList.get(i).setSelected(car_all_box.isChecked());
}
carAllCheckedPrice();
carAllNumber();
carAdapter.notifyDataSetChanged();
}
});
}
//计算总价格
private void carAllCheckedPrice() {
float price = carAdapter.totalPrice();
car_price_text.setText("" + price);
}
//计算总数量
private void carAllNumber() {
int num = carAdapter.totalNum();
car_settle_text.setText("去结算(" + num + ")");
}