将Activity转换为Fragments时,我可以使用什么来代替setContentView

时间:2017-06-05 08:49:07

标签: android android-fragments fragment setcontentview

我正在尝试将Activity转换为片段,如下所示:

活动

public class MainActivity extends AppCompatActivity {
    private ZXingScannerView scannerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
    }

    public void scanBarcode(View view) {
        scannerView = new ZXingScannerView(HomeActivity.this);
        scannerView.setResultHandler(new ZXingScannerResultHandler());
        setContentView(scannerView);
        scannerView.startCamera();
    }

    private class ZXingScannerResultHandler implements ZXingScannerView.ResultHandler {

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

片段

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);

        Button scanBar = (Button) view.findViewById(R.id.scan_barcode);
        scanBar.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;
        }
    }

    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();
        }
    }
}

warranty_fragment

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.kainet.barcode.MainActivity">

    <Button
        android:id="@+id/scan_barcode"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:text="Scan"/>

</RelativeLayout>

正如您所见,buttonClick调用scannerView并在捕获条形码时返回到视图,这是使用setContentView实现的。但似乎片段不支持setContentView。有什么替代方案或如何使此代码生效?

0 个答案:

没有答案