How to call setContentView in a fragment

2019-06-13 18:15发布

I know similar questions have been asked before but I have searched and couldn't find a solution.

I am trying to setup barcodescanner as described here

public class MainFragment extends Fragment implements View.OnClickListener{

    private ZXingScannerView scannerView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_warrant, container, false);

        ImageButton scanBar = (ImageButton) view.findViewById(R.id.scan_barcode);
        scanBar.setOnClickListener(this);

        Button checkImei = (Button) view.findViewById(R.id.btn_check);
        checkImei.setOnClickListener(this);

        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.scan_barcode:
                scannerView = new ZXingScannerView(getActivity());
                scannerView.setResultHandler(new ZXingScannerResultHandler());
//                setContentView(scannerView);
                scannerView.startCamera();
                break;
            case R.id.btn_check:
                Toast.makeText(getActivity(), "Button", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    class ZXingScannerResultHandler implements ZXingScannerView.ResultHandler {

        @Override
        public void handleResult(Result result) {
            String resultCode = result.getText();
            Toast.makeText(getActivity(), resultCode, Toast.LENGTH_LONG).show();
//            setContentView(R.layout.activity_start);
            scannerView.stopCamera();
        }
    }
}

Works in Activity but moving it in a fragment I get error with the setContentView. Is there an alternative to setContentView or may a different library that uses fragments?

1条回答
啃猪蹄的小仙女
2楼-- · 2019-06-13 18:44

This is the link of,
Official documentation

About how you can implement ZXingScanner into Fragment.

Cheers!

查看更多
登录 后发表回答