BigDemo----V

CaptureActivity

public class CaptureActivity extends AppCompatActivity implements QRCodeView.Delegate {

    private QRCodeView mQRCodeView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_capture);
        mQRCodeView = findViewById(R.id.zbarview);
        mQRCodeView.setDelegate(this);
    }
    
    @Override
    public void onScanQRCodeSuccess(String result) {
        vibrate();
        Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
        mQRCodeView.startSpot();
    }
    
    @Override
    public void onScanQRCodeOpenCameraError() {
        Toast.makeText(this, "错误", Toast.LENGTH_SHORT).show();
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        mQRCodeView.startCamera();//打开相机
        mQRCodeView.showScanRect();//显示扫描框
        mQRCodeView.startSpot();//开始识别二维码
    }

    @Override
    protected void onStop() {
        mQRCodeView.stopCamera();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        mQRCodeView.onDestroy();
        super.onDestroy();
    }

    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }
}

Fragment1

public class Fragment1 extends Fragment implements DataCall {

    private ImageView sys;
    private GridView gridView;
    private Banner banner;
    private RecyclerView recyclerView;
    private SearchView searchView;
    private GridPresenter gridpresenter;
    private GridAdapter mAdapter;
    private GoodsPresenter goodspresenter;
    private List<GoodsBean.DataBean> list;
    private GoodsPresenter goodsPresenter;
    private String sname = "手机";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment1, container, false);
        //初始化控件
        sys = view.findViewById(R.id.sys);
        searchView = (SearchView) view.findViewById(R.id.search);
        gridView = view.findViewById(R.id.gridview);
        banner = view.findViewById(R.id.banner);
        recyclerView = view.findViewById(R.id.recy_show);

        goodsPresenter = new GoodsPresenter(this);

        gridpresenter = new GridPresenter(this);
        goodspresenter = new GoodsPresenter(this);
        goodsPresenter.showGoods(1, sname);

        gridpresenter.showGoods("http://www.zhaoapi.cn/product/getCatagory");

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //***********************轮播图************
        //ok网络请求
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url("http://api.tianapi.com/meinv/?key=2a0024d1f7f558e09936f697580f1643&num=5")
                .build();
        Call call = okHttpClient.newCall(request);
        //用okHttp里面的call对象打点调用  异步请求数据的抽象方法
        call.enqueue(new Callback() {
            //建个集合  用来存放图片的url地址
            private List<String> picUrlList;
            //此集合是bean解析过来的集合
            private List<IconBean.NewslistBean> list;

            @Override
            public void onFailure(Call call, IOException e) {
                //访问网络失败的方法(自动生成的)
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //访问网络成功的方法(自动生成的)
                //这是bena里边的数据
                String json = response.body().string();
                //开始Gson解析
                Gson gson = new Gson();
                IconBean myBean = gson.fromJson(json, IconBean.class);
                //拿到bean类里的集合
                list = myBean.getNewslist();
                //设此局此集合专门用来存放图片url地址的
                picUrlList = new ArrayList<>();
                Log.d("AAAA", "onResponse: +++++++++"+list.size());
                for (int i = 0; i < list.size(); i++) {
                    //循环吧图片地址添加到String泛型的集合里
                    picUrlList.add(list.get(i).getPicUrl());
                }
                //子线程不能更新UI
                //所以【必须】开启返回主线程的方法
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        banner.setImages(picUrlList).setImageLoader(new GildeApplication()).start();
                    }
                });
            }
        });

        //*******************************搜索**********************
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {

                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                goodsPresenter.showGoods(1, s);
                sname = s;
                return false;
            }
        });

        //*****************扫一扫***************
        sys.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), CaptureActivity.class);
                startActivity(intent);
            }
        });



    }

    @Override
    public void ShowSuccess(GoodsBean goodsBean) {
        list = goodsBean.getData();
        Log.d("aaaaaaaa", "ShowSuccess: ++++++++++" + list);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        GoodsAdapter goodsAdapter = new GoodsAdapter(list, getActivity());
        recyclerView.setAdapter(goodsAdapter);
    }
    //*******************GridVIew展示****************
    @Override
    public void ShowGridSuccess(GrBean grBean) {
        List<GrBean.DataBean> list = grBean.getData();
        mAdapter = new GridAdapter();
        mAdapter.setContext(getActivity());
        mAdapter.setmList(list);
        gridView.setNumColumns(3);
        gridView.setAdapter(mAdapter);
    }

    @Override
    public void ShowShopSuccess(List<Shop.DataBean> dataBean) {

    }

    @Override
    public void ShowError(String error) {

    }

    @Override
    public Context context() {
        return getActivity();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        goodsPresenter.onDestroy();
    }
}

Fragment2

public class Fragment2 extends android.support.v4.app.Fragment implements DataCall {

    private XRecyclerView xRecyclerView;
    private ImageView sys;
    private SearchView searchView;
    private ImageView btn_menu;
    private String sname = "手机";
    private int page = 1;
    private GoodsPresenter goodsPresenter;
    private List<GoodsBean.DataBean> list;
    private int type = 1;
    private GoodsAdapter goodsAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment2, container, false);
        //初始化控件
        sys = view.findViewById(R.id.sys);
        searchView = (SearchView) view.findViewById(R.id.search);
        xRecyclerView = view.findViewById(R.id.recy_show);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        xRecyclerView.setLayoutManager(linearLayoutManager);

        btn_menu = view.findViewById(R.id.btn_menu1);
        goodsPresenter = new GoodsPresenter(this);
        initData();
        //刷新加载
       initRefresh();
        goodsPresenter.showGoods(1, sname);

        return view;
    }

    private void initData() {

        goodsPresenter = new GoodsPresenter(this);
        goodsPresenter.showGoods(1, "笔记本");
        btn_menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (type == 1) {
                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager
                            (getActivity());
                    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                    xRecyclerView.setLayoutManager(linearLayoutManager);
                    goodsAdapter.notifyDataSetChanged();
                    type = 2;
                } else {
                    GridLayoutManager gridLayoutManager = new GridLayoutManager(
                            getActivity(), 2);
                    xRecyclerView.setLayoutManager(gridLayoutManager);
                    goodsAdapter.notifyDataSetChanged();
                    type = 1;
                }
            }
        });
    }

    private void initRefresh() {
        //*************刷新加载*********
        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                //更新UI
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        goodsPresenter.showGoods(page, sname);
                        xRecyclerView.refreshComplete();
                        Toast.makeText(getActivity(), "刷新完成", Toast.LENGTH_SHORT).show();
                    }
                }, 2000);
            }

            @Override
            public void onLoadMore() {
                //更新UI
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page++;
                        goodsPresenter.showGoods(page, sname);
                        xRecyclerView.loadMoreComplete();
                        Toast.makeText(getActivity(), "加载完成", Toast.LENGTH_SHORT).show();
                    }
                }, 2000);
            }
        });
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        //*******************************搜索**********************
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {

                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                goodsPresenter.showGoods(1, s);
                sname = s;
                return false;
            }
        });
        //*****************扫一扫***************
        sys.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getActivity(), CaptureActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void ShowSuccess(GoodsBean goodsBean) {
        list = goodsBean.getData();

        goodsAdapter = new GoodsAdapter(list, getActivity());
        xRecyclerView.setAdapter(goodsAdapter);
    }

    @Override
    public void ShowGridSuccess(GrBean grBean) {

    }

    @Override
    public void ShowShopSuccess(List<Shop.DataBean> dataBean) {

    }

    @Override
    public void ShowError(String error) {

    }

    @Override
    public Context context() {
        return getActivity();
    }
}

Fragment3

public class Fragment3 extends Fragment implements View.OnClickListener, DataCall {

    private ExpandableListView ex_listView;
    private CheckBox all_box;
    private TextView all_price;
    private Button jiesuan_btn;
    private ShopAdapter shopAdapter;
    private String url = "http://www.zhaoapi.cn/product/getCarts?uid=71";
    private ShopPresenter shopPresenter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment3, container, false);
        //初始化控件
        ex_listView = view.findViewById(R.id.ex_listView);
        all_box = view.findViewById(R.id.all_box);
        all_price = view.findViewById(R.id.all_price);
        jiesuan_btn = view.findViewById(R.id.jiesuan_btn);
        //B.获取数据
        shopPresenter = new ShopPresenter(this);
        shopPresenter.showShop(url);
        all_box.setOnClickListener(this);
        return view;
    }
    //-------底部全选框的点击事件--------
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.all_box:
                boolean allSelected = shopAdapter.isAllSelected();
                shopAdapter.changAllCheckBox(!allSelected);
                shopAdapter.notifyDataSetChanged();
                //刷新底部的数据显示
                refreshAllShop();
                break;
        }
    }
    //--------刷新底部---------
    private void refreshAllShop() {
        //判断是否所有商品都被选中
        boolean allSelected = shopAdapter.isAllSelected();
        //设置给checkbox
        all_box.setChecked(allSelected);
        //计算总计
        float price = shopAdapter.TotalPrice();
        all_price.setText("总计" + price);
        //计算数量
        int number = shopAdapter.TotalNumber();
        jiesuan_btn.setText("去结算(" + number + ")");

    }

    @Override
    public void ShowSuccess(GoodsBean goodsBean) {

    }

    @Override
    public void ShowGridSuccess(GrBean grBean) {

    }

    @Override
    public void ShowShopSuccess(List<Shop.DataBean> dataBean) {
//B1.获取适配器
        shopAdapter = new ShopAdapter(dataBean);
        //B2.对适配器设置监听(加减器,组,子条目复选框改变)
        shopAdapter.setOnCartListChangeListener(new ShopAdapter.onCartListChangeListener() {
            //------B2.1.组的复选框被点击------
            @Override
            public void onParentCheckedChange(int groupPosition) {
                boolean parentAllSelect = shopAdapter.isParentAllSelect(groupPosition);
                shopAdapter.changeSellerAllProduct(groupPosition, !parentAllSelect);
                shopAdapter.notifyDataSetChanged();
                //刷新底部
                refreshAllShop();

            }

            //-------B2.2.子条目的复选框被点击-------
            @Override
            public void onChildCheckedChange(int groupPosition, int childPosition) {
                shopAdapter.changeChild(groupPosition, childPosition);
                shopAdapter.notifyDataSetChanged();
                refreshAllShop();
            }

            //-------B2.3.加减器被点击------
            @Override
            public void onAddSubNumberChange(int groupPosition, int childPosition, int number) {
                shopAdapter.changProductNumber(groupPosition, childPosition, number);
                shopAdapter.notifyDataSetChanged();
                refreshAllShop();
            }
        });
        //B3.设置adapter对象
        ex_listView.setAdapter(shopAdapter);
        //B4.展开二级列表
        for (int i = 0; i < dataBean.size(); i++) {
            ex_listView.expandGroup(i);
        }
    }

    @Override
    public void ShowError(String error) {
        Toast.makeText(getActivity(), "66666    +." + error, Toast.LENGTH_SHORT).show();
    }

    @Override
    public Context context() {
        return getActivity();
    }
}

猜你喜欢

转载自blog.csdn.net/LG_lxb/article/details/85135706
V
C V