public class Fragment_One extends Fragment {
private CheckBox checkBox1;
private RecyclerView rec_view;
private Presenter presenter;
private TextView sum;
private ShopAdapter shopAdapter;
private List<ShowBean.DataEntity> list;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
checkBox1 = view.findViewById(R.id.checkbox1);
rec_view = view.findViewById(R.id.rec_view);
LinearLayoutManager linearLayoutManager =new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
rec_view.setLayoutManager(linearLayoutManager);
sum = view.findViewById(R.id.sum);
presenter = new Presenter(this);
presenter.getPress();
checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkAllOrNot(isChecked);
}
});
return view;
}
public void getViewShow(final String data) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Gson gson =new Gson();
ShowBean showBean = gson.fromJson(data, ShowBean.class);
list = showBean.getData();
list.remove(0);
shopAdapter = new ShopAdapter(getActivity(), list);
rec_view.setAdapter(shopAdapter);
}
});
}
private void checkAllOrNot(boolean isChecked) {
int prices = 0;
for (int i = 0; i <list.size() ; i++) {
ShowBean.DataEntity dataEntity = list.get(i);
dataEntity.setCheck(isChecked);
for (int j = 0; j < list.get(i).getList().size(); j++) {
list.get(i).getList().get(j).setCheck(isChecked);
if (checkBox1.isChecked()){
int num = list.get(i).getList().get(j).getNum();
double price = list.get(i).getList().get(j).getPrice();
prices+=num*price;
}
}
sum.setText("总价为:"+prices);
}
shopAdapter.notifyDataSetChanged();
}
}