public class MainActivity extends AppCompatActivity implements Contract.Iview, View.OnClickListener {
private TextView title;
private RecyclerView recycler_view;
private Presenter presenter;
private CheckBox box_all;
private TextView text_price;
private Button btn_count;
private ShowAdapter showAdapter;
private List<DataBean> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
initView();
//调用p层
presenter = new Presenter();
presenter.attch(this);
presenter.showUrl(Api_hou.URL);
box_all.setOnClickListener(this);
title.setText("购物车");
}
private void initView() {
title = findViewById(R.id.text_title);
recycler_view = findViewById(R.id.recycler_view);
box_all = findViewById(R.id.box_all);
text_price = findViewById(R.id.text_price);
btn_count = findViewById(R.id.btn_count);
}
@Override
public void getRequest(Object data) {
Gson gson = new Gson();
ShowBean showBean = gson.fromJson(String.valueOf(data), ShowBean.class);
List<DataBean> dataBeans = showBean.getData();
showAdapter = new ShowAdapter(dataBeans, MainActivity.this);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recycler_view.setLayoutManager(linearLayoutManager);
recycler_view.setAdapter(showAdapter);
showAdapter.setLisenter(new ShowAdapter.ShopShowCallBack() {
@Override
public void callBack(List<DataBean> list) {
double totalPrice = 0;
int num = 0;
int totalNum = 0;
for (int i = 0; i < list.size(); i++) {
List<ListBean> listtotal = list.get(i).getList();
for (int a = 0; a < listtotal.size(); a++) {
totalNum = totalNum - listtotal.get(a).getNum();
////取选中的状态
if (listtotal.get(a).isCheck()) {
totalPrice = totalPrice + (listtotal.get(a).getPrice() * listtotal.get(a).getNum());
num = num +listtotal.get(a).getNum();
}
}
}
if (num < totalNum) {
box_all.setChecked(false);
} else {
box_all.setChecked(true);
}
text_price.setText("合计:" + totalPrice);
btn_count.setText("去结算(" + num + ")");
}
});
if (data instanceof ShowBean){
ShowBean showBean1= (ShowBean) data;
list = showBean1.getData();
if (list !=null){
list.remove(0);
showAdapter.setList(list);
}
}
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.box_all:
checkedSell(box_all.isChecked());
showAdapter.notifyDataSetChanged();
break;
}
}
private void checkedSell(boolean checked) {
double priceAll=0;
int numAll=0;
for (int i = 0; i <list.size(); i++) {
DataBean dataBean = list.get(i);
dataBean.setChecked(checked);
List<ListBean> listAll =list.get(i).getList();
for (int a=0;a<listAll.size();a++){
listAll.get(a).setCheck(checked);
priceAll=priceAll+(listAll.get(a).getPrice()*listAll.get(a).getNum());
numAll=numAll+listAll.get(a).getNum();
}
}
if (checked){
text_price.setText("合计:"+priceAll);
btn_count.setText("去结算("+numAll+")");
}else{
text_price.setText("合计:0.00");
btn_count.setText("去结算(0)");
}
}
//修改选中状态,获取价格和数量
@Override
protected void onDestroy() {
super.onDestroy();
presenter.detch();
}
}