购物车主页面

来源:互联网 发布:程序员一句话工作总结 编辑:程序博客网 时间:2024/04/28 22:48
package wld.bawei.com.myjindong.Fragment;import android.content.Context;import android.content.SharedPreferences;import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.widget.CheckBox;import android.widget.ExpandableListView;import android.widget.TextView;import android.widget.Toast;import java.text.DecimalFormat;import java.util.List;import wld.bawei.com.myjindong.Adapter.CarExpandAdapter;import wld.bawei.com.myjindong.Bean.Query_bean;import wld.bawei.com.myjindong.QueryMvp.QueryPresenter;import wld.bawei.com.myjindong.QueryMvp.QueryView;import wld.bawei.com.myjindong.R;/** * Created by 大金鑫 on 2017/12/5. */public class Shopping extends Fragment implements QueryView {    private View viewshopping;    private ExpandableListView carexpand;    private QueryPresenter queryPresenter;    private TextView carts;    private double totalPrice = 0.0;    private double numPrice = 0.0;    private TextView totalShowPrice;    private TextView btnAmount;    private CheckBox chooseAll;    private TextView btnEditor;    //记录商品总数量    private int totalNum = 0;    private int numNum = 0;    private boolean flag=false;    private CarExpandAdapter carExpandAdapter;    private Button btn_commodity_delete;    private TextView btnback;    List<Query_bean.DataBean> arr;    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        viewshopping = View.inflate(getContext(), R.layout.shopping, null);        return viewshopping;    }    @Override    public void onActivityCreated(@Nullable Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        carexpand = viewshopping.findViewById(R.id.carexpand);        carexpand.setGroupIndicator(null);        carts = viewshopping.findViewById(R.id.carts);        totalShowPrice = viewshopping.findViewById(R.id.totalPrice);        btnAmount = viewshopping.findViewById(R.id.btnAmount);        chooseAll = viewshopping.findViewById(R.id.chooseAll);        btnEditor = viewshopping.findViewById(R.id.btnEditor);        btnback = (TextView)viewshopping.findViewById(R.id.btnBack);        btn_commodity_delete = viewshopping.findViewById(R.id.btn_commodity_delete);        //判断×号是否显示        btnEditor.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if(btnEditor.getText().toString().trim().equals("编辑")){                    if(!(carExpandAdapter==null)){                         CarExpandAdapter.flog=true;                        carExpandAdapter.notifyDataSetChanged();                    }                    btnEditor.setText("完成");                }else {                    if(!(carExpandAdapter==null)) {                        CarExpandAdapter.flog=false;                        carExpandAdapter.notifyDataSetChanged();                    }                    btnEditor.setText("编辑");                }            }        });        chooseAll.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                if(((CheckBox)view).isChecked()){                    //全选 checkbo 是选中状态                    isChooseAll(((CheckBox)view).isChecked());                }else{                    //全部不选                    isChooseAll(((CheckBox)view).isChecked());                }                js();            }        });    }    @Override    public void onFailure(String error) {        Toast.makeText(getContext(),error,Toast.LENGTH_LONG).show();    }    @Override    public void onSuccess(final Query_bean query_bean) {        if(!(query_bean==null)){            arr = query_bean.getData();            SharedPreferences myshared = getActivity().getSharedPreferences("Myshared", Context.MODE_PRIVATE);            int uid = myshared.getInt("uid", 1);            if(!(uid==1)){                List<Query_bean.DataBean> data = query_bean.getData();                carExpandAdapter = new CarExpandAdapter(data, getActivity(),this);                carexpand.setAdapter(carExpandAdapter);                //默认打开全部二级列表                int groupCount = carexpand.getCount();                for (int i=0; i<groupCount; i++)                {                    carexpand.expandGroup(i);                }                isChooseAll(true);                js();            }        }else{            carts.setVisibility(View.VISIBLE);            carexpand.setVisibility(View.GONE);            totalShowPrice.setText("0.0¥");            btnAmount.setText("结算 : ("+0+")");             Toast.makeText(getContext(),"购物车空空如也,快去购买吧!",Toast.LENGTH_SHORT).show();        }    }    @Override    public void onResume() {        super.onResume();        SharedPreferences myshared = getActivity().getSharedPreferences("Myshared", Context.MODE_PRIVATE);        int uid = myshared.getInt("uid", 1);        if(!(uid==1)){            queryPresenter = new QueryPresenter(this);            queryPresenter.vertify(uid+"");            carexpand.setVisibility(View.VISIBLE);            carts.setVisibility(View.GONE);        }else{            if(arr!=null){                arr.clear();            }            totalShowPrice.setText("0.0¥");            btnAmount.setText("结算 : ("+0+")");            carts.setVisibility(View.VISIBLE);            carexpand.setVisibility(View.GONE);        }    }    //全选判断    private void isChooseAll(boolean isChooseAll){        for (Query_bean.DataBean listBean : arr){            if(isChooseAll){                listBean.setFlog(true);                for(Query_bean.DataBean.ListBean list:listBean.getList()){                    list.setChoosed(true);                }            }else{                listBean.setFlog(false);                for(Query_bean.DataBean.ListBean list:listBean.getList()){                    list.setChoosed(false);                }            }        }        //刷新适配器        carExpandAdapter.notifyDataSetChanged();    }    public void Gaibian(boolean f){        chooseAll.setChecked(f);    }    public void js(){        double p = 0;        int s = 0;//选中商品数量        for(int i=0;i<arr.size();i++){            for(int j=0;j<arr.get(i).getList().size();j++){                if(arr.get(i).getList().get(j).isChoosed){                    double price = arr.get(i).getList().get(j).getBargainPrice();                    int num = arr.get(i).getList().get(j).getNum();                    p+= price * num;                    s+=num;                }            }        }        DecimalFormat df = new DecimalFormat("#####0.00");        String str = df.format(p);        totalShowPrice.setText("总价:"+str+"¥");        btnAmount.setText("结算 : ("+s+")");    }}