public class MainActivity extends AppCompatActivity implements CartContart.CartView,YijiCallback {
private void initData() {
mQuan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mQuan.isChecked()){
for (CartBean.DataBean dataBean : list) {
dataBean.cartischecked = true;
for (CartBean.DataBean.ListBean listBean : dataBean.list) {
listBean.productischecked = true;
}
}
}else {
for (CartBean.DataBean dataBean : list) {
dataBean.cartischecked = false;
for (CartBean.DataBean.ListBean listBean : dataBean.list) {
listBean.productischecked = false;
}
}
}
cartAdapter.notifyDataSetChanged();
getprice();
}
});
}
private void getprice(){
double price = 0;
for (CartBean.DataBean dataBean : list) {
for (CartBean.DataBean.ListBean listBean : dataBean.list) {
if (listBean.productischecked){
price += listBean.price*listBean.shuliang;
}
}
}
mTvPrice.setText("¥:"+price);
}
@Override
public void cartsuccess(CartBean cartBean) {
list = new ArrayList<>();
list = cartBean.data;
cartAdapter = new CartAdapter(this);
cartAdapter.setYijiCallback(this);
cartAdapter.setList(cartBean.data);
mRecy.setAdapter(cartAdapter);
}
@Override
public void notifyCart() {
getprice();
}
}
public class CartAdapter extends RecyclerView.Adapter<CartAdapter.Myvh> implements ErjiCallback {
private YijiCallback yijiCallback;
public void setYijiCallback(YijiCallback yijiCallback) {
this.yijiCallback = yijiCallback;
}
@Override
public void onBindViewHolder(@NonNull final Myvh holder, final int position) {
productAdapter.setErjiCallback(this);
holder.mCheckCart.setChecked(list.get(position).cartischecked);
for (CartBean.DataBean.ListBean listBean : list.get(position).list) {
listBean.pos = position;
}
holder.mCheckCart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.get(position).cartischecked = holder.mCheckCart.isChecked();
for (CartBean.DataBean.ListBean listBean : list.get(position).list) {
listBean.productischecked = list.get(position).cartischecked;
}
notifyDataSetChanged();
if (yijiCallback!=null){
yijiCallback.notifyCart();
}
}
});
}
@Override
public void notifyCartItem(boolean isChecked, int position) {
list.get(position).cartischecked = isChecked;
notifyDataSetChanged();
if (yijiCallback!=null){
yijiCallback.notifyCart();
}
}
@Override
public void notifyNum() {
if (yijiCallback!=null){
yijiCallback.notifyCart();
}
}
}
public interface ErjiCallback {
void notifyCartItem(boolean isChecked,int position);
void notifyNum();
}
public interface YijiCallback {
void notifyCart();
}
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.Myvh> {
private ErjiCallback erjiCallback;
public void setErjiCallback(ErjiCallback erjiCallback) {
this.erjiCallback = erjiCallback;
}
@Override
public void onBindViewHolder(@NonNull final Myvh holder, final int position) {
holder.mAddview.seted_num(list.get(position).shuliang);
holder.mCheckProduct.setChecked(list.get(position).productischecked);
holder.mAddview.setAddminusCallback(new Addview.AddminusCallback() {
@Override
public void click(int num) {
list.get(position).shuliang = num;
if (erjiCallback!=null){
erjiCallback.notifyNum();
}
}
});
holder.mCheckProduct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!holder.mCheckProduct.isChecked()){//如果没有选中
list.get(position).productischecked=false;
if (erjiCallback!=null){
erjiCallback.notifyCartItem(false,list.get(position).pos);
}
}else {
list.get(position).productischecked=true;
for (CartBean.DataBean.ListBean listBean : list) {
if (!listBean.productischecked){
if (erjiCallback!=null){
erjiCallback.notifyCartItem(false,list.get(position).pos);
}
break;
}
if (erjiCallback!=null){
erjiCallback.notifyCartItem(true,list.get(position).pos);
}
}
}
}
});
}
public class CartBean {
public String code;
public String msg;
public List<DataBean> data;
public static class DataBean {
public String sellerName;
public List<ListBean> list;
public boolean cartischecked;
public static class ListBean {
public String images;
public int num;
public int price;
public int pscid;
public String title;
public boolean productischecked;
public int pos;//子条目全部选中 父条目也选中
public int shuliang = 1; //商品数量
}
}
}
public class AddMinus extends LinearLayout {
private void init(final Context context) {
Addview addview = (Addview) LayoutInflater.from(context).
inflate(R.layout.addandjian, this, true);
mTvJian = addview.findViewById(R.id.tv_jian);
mEditNum = addview.findViewById(R.id.edit_num);
mTvJia = addview.findViewById(R.id.tv_jia);
mTvJian.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
num--;
if (num==0){
num=1;
Toast.makeText(context, "不能再减了", Toast.LENGTH_SHORT).show();
}
mEditNum.setText(num+"");
if (addminusCallback!=null){
addminusCallback.click(num);
}
}
});
mTvJia.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
num++;
mEditNum.setText(num+"");
if (addminusCallback!=null){
addminusCallback.click(num);
}
}
});
}
public void seted_num(int num){
mEditNum.setText(num+"");
}
private AddminusCallback addminusCallback;
public void setAddminusCallback(AddminusCallback addminusCallback) {
this.addminusCallback = addminusCallback;
}
public interface AddminusCallback{
void click(int num);
}
}