在 Android 中的 FrameLayout 上添加片段

Add Fragment on FrameLayout in Android

我的 Activity 已扩展到 AppCompatActivity。我也成功地设置了一个抽屉。现在,当我在 Fragment 上进行交易时,基本上它使用 begintransaction。但我在 android.app.FragmentManager fragmentManager = getFragmentManager(); 上收到此错误消息 Unreachable statement 我也尝试了 android.support.v4.app 包它还说这已被弃用。

在我的抽屉导航中onClickListener

我有这个

`

mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                android.app.Fragment objFragment = null;

                menuItem.setChecked(true);
                switch (menuItem.getItemId()) {
                    case R.id.dhome:
                        objFragment = new panel();
                        mCurrentSelectedPosition = 0;
                        return true;
                    case R.id.dsetting:

                        mCurrentSelectedPosition = 1;
                        return true;
                    case R.id.dlogout:

                        mCurrentSelectedPosition = 2;
                        return true;
                    default:
                        return true;
                }

                // update the main content by replacing fragments
                if(objFragment != null) {
                    android.app.FragmentManager fragmentManager = getFragmentManager();
                    fragmentManager.beginTransaction()
                            .replace(R.id.container, objFragment)
                            .commit();
                }

            }
        });`

在我的布局中

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/nav_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
   >


    <FrameLayout android:id="@+id/container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#fff"/>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#d9d8d8"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer" />



</android.support.v4.widget.DrawerLayout>

使用这个:

@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
    switch (menuItem.getItemId()) {
        case R.id.dhome:
            objFragment = new panel();
            mCurrentSelectedPosition = 0;
            break;
        case R.id.dsetting:
            mCurrentSelectedPosition = 1;
            break;
        case R.id.dlogout:
            mCurrentSelectedPosition = 2;
            break;
    }

    // update the main content by replacing fragments
    if(objFragment != null) {
        android.app.FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                       .replace(R.id.container, objFragment)
                       .commit();
    }

    return true;
}

您之前的代码没有考虑用户如何与 actionbar 交互(单击任何项​​目),return true; 将始终在到达 片段事务之前执行代码。