单击导航抽屉的项目不会打开片段

Clicking on item of Navigation Drawer doesn’t open fragments

我想使用 Android Studio (v. 3.5) 的默认导航抽屉 Activity。创建此默认 activity(新项目 --> 导航抽屉 Activity)后,我启动了此模板。如果我单击导航菜单的其中一个图标(例如“图库”),NavHost 的当前片段不会改变。 据我了解以下部分:https://developer.android.com/guide/navigation/navigation-ui#Tie-navdrawer 如果菜单项的 ID 与目的地的 ID 匹配,NavController 应该导航到所选目的地,但这不起作用。

经过一整天的研究,我在 Whosebug 上发现了以下问题:NavigationView click event 非常相似但没有真正回答。下一个有趣的一点是,如果我使用按钮导航 Activity 这个模板似乎可以工作,我无法真正弄清楚导航抽屉 Activity 模板的区别。我还发现了其他几种适用于旧 Templets 的解决方案。 我不明白为什么有一个似乎不起作用的标准模板,为什么也没有通过 https://developer.android.com/ 提供的适当示例和教程。

mobile_navigation.xml(导航图):

<?xml version="1.0" encoding="utf-8"?>
<navigation 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/mobile_navigation"
    app:startDestination="@+id/nav_home">
    ... >

    ...

    <fragment
        android:id="@+id/nav_tools"
        android:name="ktu.workout_planner.ui.tools.ToolsFragment"
        android:label="@string/menu_tools"
        tools:layout="@layout/fragment_tools" />
</navigation>

activity_main_drawer.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <group android:checkableBehavior="single">
...
        <item
            android:id="@+id/nav_tools"
            android:icon="@drawable/ic_menu_manage"
            android:title="@string/menu_tools" />
    </group>

如您所见,项目的 ID 与片段的 ID 匹配。

MainActivity的onCreate函数:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        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)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    }

我是不是漏掉了什么,为什么点击导航抽屉的项目没有打开相应的片段?

Mike M. 的回答(见评论):

There's an issue in 3.5 that's causing XML to be rearranged improperly. In the activity_main layout, make sure that the <NavigationView> is listed last within the <DrawerLayout>. That is, move it to after the <include> there, if you still have the default setup. Then, have a look at to see how to stop Android Studio from reordering your View s like that.

我有同样的问题。我只是将 navigationview 放在 mainActivity.xml 文件的最后,然后首先将 include 放在 drawerlayout 中,然后它工作正常。

我遇到了同样的问题,我四处寻找,但直到我将包裹在工具栏和片段容器中的布局从更改为

之前,似乎没有任何建议起作用