全反选计算价格 总数

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //在需要接受事件的类中进行注册
        EventBus.getDefault().register(this);
        xrecy_view = findViewById(R.id.xrecy_view);
        checkall = findViewById(R.id.checkall);
        zongjia = findViewById(R.id.zongjia);
        shuliang = findViewById(R.id.shuliang);
        presenter = new Presenter();
        presenter.Attach(MainActivity.this);
        presenter.getPresData();
        checkall.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                CheckOrNot(isChecked);
            }


        });

    }
    @Override
    public void getView(Object data) {
        shopBean = (ShopBean) data;
         xrecy_view.setLayoutManager(new LinearLayoutManager(this));
        shopCarAdapter = new ShopCarAdapter(shopBean, this);
        xrecy_view.setAdapter(shopCarAdapter);
        shopCarAdapter.notifyDataSetChanged();
    }
    private void CheckOrNot(boolean isChecked) {

        List data = shopBean.getData();
        for (int i = 0; i < data.size(); i++) {
            ShopBean.DataBean dataBean = data.get(i);
            dataBean.setChecked(isChecked);
            List spus = dataBean.getSpus();
            for (int j = 0; j  data = shopBean.getData();
        int total=0;
        int totalCount=0;
        for (int i = 0; i  spus = dataBean.getSpus();
            for (int j = 0; j < spus.size(); j++) {
                ShopBean.DataBean.SpusBean spusBean = spus.get(j);
                if (spusBean.isChecked()){
                    int price = spusBean.getId();
                    int praise_num = spusBean.getPraise_num();
                   total+= price*praise_num;
                   totalCount+=praise_num;
                }
            }
        }
       zongjia.setText("总价:"+total);
        shuliang.setText("总数"+totalCount);
    }
    @Subscribe
public void onCaculeaEvent(CaculateEvent event){
    int parentPostion = event.getParentPostion();
    int selfPostion = event.getSelfPostion();
    List data = shopBean.getData();
    if (parentPostion!=-1){
        ShopBean.DataBean dataBean = data.get(parentPostion);
        List spus = dataBean.getSpus();
        if (selfPostion!=-1){
            ShopBean.DataBean.SpusBean spusBean = spus.get(selfPostion);
            spusBean.setPraise_num(event.getNewNub());
            if (spusBean.isChecked()){
                caculatePrice();
            }
        }
    }
}
@Subscribe
public void onGroupEvent(GroupEvent event){
    int groupPostion = event.getGroupPostion();
    List data = shopBean.getData();
    ShopBean.DataBean dataBean = data.get(groupPostion);
    dataBean.setChecked(event.isChecked());
    List spus = dataBean.getSpus();
    for (int i = 0; i < spus.size(); i++) {
        ShopBean.DataBean.SpusBean spusBean = spus.get(i);
        spusBean.setChecked(event.isChecked());
    }
shopCarAdapter.notifyDataSetChanged();
    caculatePrice();
}
@Subscribe
    public void onSingleEvent(SingleEvent event){
        List data = shopBean.getData();
        ShopBean.DataBean dataBean = data.get(event.getParentPosition());
        List spus = dataBean.getSpus();
        ShopBean.DataBean.SpusBean spusBean = spus.get(event.getSelfPosition());
        spusBean.setChecked(event.isChecked());
        shopCarAdapter.notifyDataSetChanged();
        caculatePrice();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}

你可能感兴趣的:(全反选计算价格 总数)