购物车

 bean里面商品状态方法

  //商品选择状态

            private boolean isCheck = false;

            public boolean isCheck() {
                return isCheck;
            }

            public void setCheck(boolean check) {
                isCheck = check;
            }

 自定义加减

package com.bw.com.lianximx;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.bw.com.lianximx.R;
import com.bw.com.lianximx.adapter.ShopAdapter;
import com.bw.com.lianximx.bean.CarBean;

import java.util.List;

public class CastView extends RelativeLayout implements View.OnClickListener {
    Context mContext;
    private EditText mEditCar;
    ShopAdapter context;
    List<CarBean.DataBean.ListBean> list;

    private long num;
    private int position;

    public CastView(Context context) {
        super(context);
        init(context);
    }

    public CastView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }


    public CastView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init(Context context) {
        this.mContext = context;
        View view = View.inflate(context, R.layout.jia_jian, null);
        ImageView addIamge = (ImageView) view.findViewById(R.id.add_car);
        ImageView jianIamge = (ImageView) view.findViewById(R.id.jian_car);
        mEditCar = view.findViewById(R.id.edit_shop_car);
        addIamge.setOnClickListener(this);
        jianIamge.setOnClickListener(this);
        addView(view);

        mEditCar.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                num = Integer.parseInt(s.toString());
                //TODO:改变数量
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.add_car:
                //改变数量,设置数量,改变对象内容,回调,局部刷新
                num++;
                mEditCar.setText(num + "");
                list.get(position).setNum(String.valueOf(num));
                mCallBackListener.callBack();
                break;
            case R.id.jian_car:
                if (num > 1) {
                    num--;
                } else {
                    Toast.makeText(mContext, "商品数量不能小于1", Toast.LENGTH_LONG).show();
                }
                mEditCar.setText(num + "");
                list.get(position).setNum(String.valueOf(num));
                mCallBackListener.callBack();
                break;
            default:
                break;

        }
    }


    public void setData(ShopAdapter context, List<CarBean.DataBean.ListBean> list, int i) {
        this.list = list;
        this.context = context;
        position = i;
        num = Integer.parseInt(list.get(i).getNum());
        mEditCar.setText(this.num + "");
    }

    private CallBackListener mCallBackListener;

    public void setOnCallBack(CallBackListener listener) {
        this.mCallBackListener = listener;
    }

    public interface CallBackListener {
        void callBack();
    }
}

 商品的adapter

package com.bw.com.lianximx.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.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bw.com.lianximx.CastView;
import com.bw.com.lianximx.R;
import com.bw.com.lianximx.bean.CarBean;

import java.util.List;

public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.ShopViewholder> {

    List<CarBean.DataBean.ListBean> listBeans;
    Context context;

    public ShopAdapter(List<CarBean.DataBean.ListBean> listBeans, Context context) {
        this.listBeans = listBeans;
        this.context = context;
    }

    @NonNull
    @Override
    public ShopViewholder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view=View.inflate(context, R.layout.shop_item,null);
        ShopViewholder shopViewholder=new ShopViewholder(view);
        return shopViewholder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ShopViewholder shopViewholder, final int i) {
        shopViewholder.textn.setText(listBeans.get(i).getTitle());
        shopViewholder.textr.setText(listBeans.get(i).getBargainPrice());
        Glide.with(context).load(listBeans.get(i).getImages().split("\\|")[0]
                .replace("https", "http")).into(shopViewholder.image);
        shopViewholder.chv.setChecked(listBeans.get(i).isCheck());
        shopViewholder.chv.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //首先要改变自己的状态
                listBeans.get(i).setCheck(isChecked);
                if (shopCallBackListener!=null){
                    shopCallBackListener.callBack();
                }
            }
        });
        shopViewholder.caview.setData(ShopAdapter.this,listBeans,i);
        shopViewholder.caview.setOnCallBack(new CastView.CallBackListener() {
            @Override
            public void callBack() {
                if (shopCallBackListener!=null){
                    shopCallBackListener.callBack();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return listBeans.size();
    }


    public class ShopViewholder extends RecyclerView.ViewHolder {

        private final CheckBox chv;
        private final TextView textn,textr;
        private final ImageView image;
        private final CastView caview;

        public ShopViewholder(@NonNull View itemView) {
            super(itemView);
            chv = itemView.findViewById(R.id.shopcheck);
            textn = itemView.findViewById(R.id.shoptext);
            textr = itemView.findViewById(R.id.shoptext2);
            image = itemView.findViewById(R.id.shopimag);
            caview = itemView.findViewById(R.id.casrview);
        }
    }

    //子商品全选反选
    public void selectOrRemoveAll(boolean isSelectAll){
        for (CarBean.DataBean.ListBean listBean : listBeans){
            listBean.setCheck(isSelectAll);
        }
        notifyDataSetChanged();
    }


    ShopCallBackListener shopCallBackListener;
    public void setListener(ShopCallBackListener shopCallBackListener){
        this.shopCallBackListener = shopCallBackListener;
    }
    public interface ShopCallBackListener {
        void callBack();
    }
}

 商家的adapter

package com.bw.com.lianximx.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.bw.com.lianximx.R;
import com.bw.com.lianximx.bean.CarBean;

import java.util.List;

public class Myadapter extends RecyclerView.Adapter<Myadapter.MyViewHolder> {
    Context context;
    List<CarBean.DataBean> list;
    ShopCallBackListener mshopCallBackListener;
    public Myadapter(Context context, List<CarBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view=View.inflate(context,R.layout.shopitem,null);
        MyViewHolder myViewHolder=new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, final int i) {
            myViewHolder.text.setText(list.get(i).getSellerName());
            final LinearLayoutManager linearLayoutManager=new LinearLayoutManager(context,OrientationHelper.VERTICAL,false);
            myViewHolder.recy_shop.setLayoutManager(linearLayoutManager);
             final List<CarBean.DataBean.ListBean> listBeans = this.list.get(i).getList();
              final ShopAdapter adapter=new ShopAdapter(listBeans,context);
              myViewHolder.recy_shop.setAdapter(adapter);
              //默认选中一个
              myViewHolder.checkbox.setChecked(list.get(i).isCheck());



              adapter.setListener(new ShopAdapter.ShopCallBackListener() {
                  @Override
                  public void callBack() {
                      if (mshopCallBackListener!=null){
                            mshopCallBackListener.callBack(list);
                      }
                      List<CarBean.DataBean.ListBean> beans = Myadapter.this.list.get(i).getList();
                      boolean isAllChecked = true;
                      for (CarBean.DataBean.ListBean bean :beans){
                          if (!bean.isCheck()){
                              //只要有一个商品未选中,标志位设置成false,并且跳出循环
                              isAllChecked = false;
                              break;
                          }
                      }
                      //刷新商家的状态
                      myViewHolder.checkbox.setChecked(isAllChecked);
                      Myadapter.this.list.get(i).setCheck(isAllChecked);
                  }
              });
        //checkbox的点击事件
        //目的是改变旗下所有商品的选中状态
          myViewHolder.checkbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list.get(i).setCheck(myViewHolder.checkbox.isChecked());
                //调用产品adapter的方法,用来全选和反选
                adapter.selectOrRemoveAll(myViewHolder.checkbox.isChecked());
            }
        });


    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    public void setList(List<CarBean.DataBean> list){
        this.list = list;
        notifyDataSetChanged();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private final TextView text;
        private final RecyclerView recy_shop;
        private final CheckBox checkbox;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            text = itemView.findViewById(R.id.shop_layout);
            recy_shop = itemView.findViewById(R.id.shop_recy);
            checkbox = itemView.findViewById(R.id.shop_checkbox);
        }
    }

    public void setLintener(ShopCallBackListener mshopCallBackListener){
        this.mshopCallBackListener = mshopCallBackListener;
    }

    public interface ShopCallBackListener{
        void callBack(List<CarBean.DataBean> list);
    }
}

 展示页面

package com.bw.com.lianximx;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.bw.com.lianximx.adapter.Myadapter;
import com.bw.com.lianximx.bean.CarBean;
import com.bw.com.lianximx.mvp.presenter.HomePresenterInter;
import com.bw.com.lianximx.mvp.view.IHomeView;
import com.google.gson.Gson;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements IHomeView {

    @BindView(R.id.recy_layout)
    RecyclerView recyLayout;
    @BindView(R.id.mian_price)
    TextView mianPrice;
    @BindView(R.id.mainjisuan)
    Button mainjisuan;
    @BindView(R.id.main_check)
    CheckBox chkbox;
    private RecyclerView recy_layout;
    private List<CarBean.DataBean> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        recy_layout = findViewById(R.id.recy_layout);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this, OrientationHelper.VERTICAL, false);
        recy_layout.setLayoutManager(linearLayoutManager);
        HomePresenterInter homePresenterInter = new HomePresenterInter(MainActivity.this);
        homePresenterInter.getPresenter(71);

    }

    @Override
    public void getDataView(String dataView) {
        Gson gson = new Gson();
        CarBean bean = gson.fromJson(dataView, CarBean.class);
        list = bean.getData();
        final Myadapter myadapter = new Myadapter(MainActivity.this, list);
        recy_layout.setAdapter(myadapter);
        if (list != null) {
            list.remove(0);
            myadapter.setList(list);
        }
        myadapter.setLintener(new Myadapter.ShopCallBackListener() {
            @Override
            public void callBack(List<CarBean.DataBean> list) {
                //在这里重新遍历已经改状态后的数据,
                // 这里不能break跳出,因为还需要计算后面点击商品的价格和数目,所以必须跑完整个循环
                double totalprice = 0;
                //勾选商品的数量,不是该商品购买的数量
                int num = 0;
                //所有商品总数,和上面的数量做比对,如果两者相等,则说明全选
                int totalNum = 0;
                for (int a = 0;a<list.size();a++){
                    //获取商家里的商品
                    List<CarBean.DataBean.ListBean> listAll = list.get(a).getList();
                    for (int i = 0;i<listAll.size();i++){
                        totalNum = totalNum + Integer.valueOf(listAll.get(i).getNum());
                        if (listAll.get(i).isCheck()){
                            totalprice = totalprice + (Integer.valueOf(listAll.get(i).getNum())*Double.valueOf(listAll.get(i).getPrice())) ;
                            num = num +Integer.valueOf(listAll.get(i).getNum());
                        }
                    }
                }
                if (num<totalNum){
                    //不是全部选中
                   chkbox.setChecked(false);
                }else{
                    chkbox.setChecked(true);
                }

                mianPrice.setText("合计:"+totalprice);
                mainjisuan.setText("去结算:"+num);
            }
        });

        chkbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkSeller(chkbox.isChecked());
                myadapter.notifyDataSetChanged();
            }
        });
    }

    //定义一个方法
    private void checkSeller(boolean bool) {
        double totalprice = 0;
        int num = 0;
        for (int a = 0; a < list.size(); a++) {
            //遍历商家改变状态
            CarBean.DataBean dataBean = list.get(a);
            dataBean.setCheck(bool);
            //商品
            List<CarBean.DataBean.ListBean> listBeans = this.list.get(a).getList();
            for (int i = 0; i < listBeans.size(); i++) {
                listBeans.get(i).setCheck(bool);
                totalprice = totalprice + (Integer.valueOf(listBeans.get(i).getNum()) * Double.valueOf(listBeans.get(i).getPrice()));
                num = num + Integer.valueOf(listBeans.get(i).getNum());
            }
        }
        if (bool) {
            mianPrice.setText("合计" + totalprice);
            mainjisuan.setText("去(" + num + ")");
        } else {
            mianPrice.setText("0.00");
            mainjisuan.setText("0");
        }
    }


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值