Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将片段添加到NavigationDrawer活动_Java_Android_Android Fragments_Navigation Drawer - Fatal编程技术网

Java 将片段添加到NavigationDrawer活动

Java 将片段添加到NavigationDrawer活动,java,android,android-fragments,navigation-drawer,Java,Android,Android Fragments,Navigation Drawer,我在Android Studio上创建了一个带有NavigationDrawer活动的项目。 它包含导航列表 我想在单击导航列表时启动一个片段活动。 就像我从导航栏点击导入一样,这将打开新的图层 我的密码是 活动主体 <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res

我在Android Studio上创建了一个带有
NavigationDrawer活动的项目。
它包含导航列表

我想在单击导航列表时启动一个片段活动。 就像我从导航栏点击导入一样,这将打开新的图层

我的密码是

活动主体

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <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:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

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

如何做到这一点?我是碎片方面的新手。

只要这样称呼你的碎片:

 if (id == R.id.nav_camara) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.main_fragment, New YourPreDefindeFragment());
        ft.addToBackStack("tag_back");
        ft.commit();
    } else if (id == R.id.nav_gallery) {

    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }
并在XML中的用户
下添加此代码

    <FrameLayout
        android:id="@+id/main_fragment"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"

        android:orientation="vertical" />

您只需创建新的片段类,该类扩展片段及其各自的XML文件,并在MainActivity中调用它们 例如


在android studio 3.5及以上版本中,这是默认设置

您将注意到默认情况下生成了不同的片段。显示单击导航项目的时间

mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
            R.id.nav_tools, R.id.nav_share, R.id.nav_send,R.id.nav_accounts)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);
您还可以将侦听器添加到项目单击,并添加另一个事件,如“打开新活动”

  navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
        @Override
        public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {

            int menuId = destination.getId();

            switch (menuId){
                case R.id.nav_gallery:
                    Toast.makeText(MainActivity.this,"You tapped gallery",Toast.LENGTH_LONG).show();
                    fab.hide();

                    break;
                    default:
                        fab.show();
                        break;
            }

        }
    });
您可以查看本教程以了解更多详细信息。

我正在努力。我应该删除
?我想是的,你必须删除。你可以使用Android Studio生成的
content\u main
布局,而不是添加
:你可以给它的布局指定一个id(即c\u main),然后用:
ft.replace(R.id.c\u main,new YourFragment())修改上面的代码。因此,您不应该删除
。它对我有用
    <FrameLayout
        android:id="@+id/main_fragment"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"

        android:orientation="vertical" />
if (id == R.id.nav_camara) {
    Fragment fragment = null;
      fragment = new newFragment();
} else if (id == R.id.nav_gallery) {

} else if (id == R.id.nav_slideshow) {

} else if (id == R.id.nav_manage) {

} else if (id == R.id.nav_share) {

} else if (id == R.id.nav_send) {

}

 if (fragment != null) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.content_frame, fragment);
        ft.commit();
    }

    // set the toolbar title
    if (getSupportActionBar() != null) {
        getSupportActionBar().setTitle(title);
    }
mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow,
            R.id.nav_tools, R.id.nav_share, R.id.nav_send,R.id.nav_accounts)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);
  navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
        @Override
        public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {

            int menuId = destination.getId();

            switch (menuId){
                case R.id.nav_gallery:
                    Toast.makeText(MainActivity.this,"You tapped gallery",Toast.LENGTH_LONG).show();
                    fab.hide();

                    break;
                    default:
                        fab.show();
                        break;
            }

        }
    });