这个新的 Android studio 3.5 Navigation Drawer 示例代码如何 运行

How does this new Android studio 3.5 Navigation Drawer sample code run

我在学习 Android studio 并学习了一些有关导航抽屉的知识。 后来我更新了我的 Android Studio 到 3.5 版本 现在我看到旧代码不见了。 我看到的新代码,因为我是一个新学习者,我完全困惑这个新代码如何适用于片段, 是的,我尝试过搜索,但 none 成功了 有人可以帮我解决以下代码吗?

    public class HomeActivity extends AppCompatActivity {

     private AppBarConfiguration mAppBarConfiguration;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);



    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    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)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.home, menu);
    return true;
}

@Override
public boolean onSupportNavigateUp() {
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    return NavigationUI.navigateUp(navController, mAppBarConfiguration)
            || super.onSupportNavigateUp();
}
}

我想在点击菜单后打开相关片段。

来自文件 xml >>activity_main.xml 查看默认顺序

<com.google.android.material.navigation.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" />

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

更改为以下顺序:

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

<com.google.android.material.navigation.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" />

在java中使用

      navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
            @Override
            public void onDestinationChanged(@NonNull NavController controller, @NonNull
                    NavDestination destination, @Nullable Bundle arguments) {
                if (destination.getId() == R.id.nav_home) {
                    Toast.makeText(MainActivity.this, "home", Toast.LENGTH_LONG).show();
                }
                if (destination.getId() == R.id.nav_gallery) {
                    Toast.makeText(MainActivity.this, "nav_gallery", Toast.LENGTH_LONG).show();
                }
                if (destination.getId() == R.id.nav_slideshow) {
                    Toast.makeText(MainActivity.this, "nav_slideshow", Toast.LENGTH_LONG).show();
                }
                if (destination.getId() == R.id.nav_tools) {
                    Toast.makeText(MainActivity.this, "nav_tools", Toast.LENGTH_LONG).show();
                }
                if (destination.getId() == R.id.nav_share) {
                    Toast.makeText(MainActivity.this, "nav_share", Toast.LENGTH_LONG).show();
                }
                if (destination.getId() == R.id.nav_send) {
                    Toast.makeText(MainActivity.this, "nav_send", Toast.LENGTH_LONG).show();
                }
            }
        });