导航抽屉不适用于导航组件

Navigation Drawer don't work with navigation Component

当用户点击菜单项时,导航抽屉无法将用户带到目的地。导航抽屉显示正确,但不起作用。有什么问题吗?

我有一个导航菜单:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        ...        
        <item
            android:id="@+id/feedbackFragment"
            android:icon="@drawable/ic_help"
            android:title="@string/title_help" />
    </group>

</menu>

和导航图:

  <fragment
      android:id="@+id/feedbackFragment"
      android:name="com.company.ru.ui.FeedBackFragment"
      android:label="fragmegment_feedback"
      tools:layout="@layout/fragment_feed_back" />

在我的 MainActivity 中有一个工具栏(全部复制):

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/color_white">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:navGraph="@navigation/main_graph" />
    </LinearLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nv"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/color_white"
        android:theme="@style/NavigationView"
        app:headerLayout="@layout/nav_header"
        app:itemIconPadding="@dimen/margin_16"
        app:itemIconTint="#FFD000"
        app:itemTextColor="@android:color/black"
        app:menu="@menu/navigation_menu" />
</androidx.drawerlayout.widget.DrawerLayout>

我这样初始化我的抽屉:

@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.AppTheme);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    controller = Navigation.findNavController(this, R.id.nav_host_fragment);

    initToolbar(getString(R.string.splash_screen_text));
    }

private void initToolbar(String title) {
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    ActionBar actionbar = getSupportActionBar();
    if (actionbar != null) {
        actionbar.setDisplayHomeAsUpEnabled(true);
        actionbar.setHomeAsUpIndicator(R.drawable.ic_dehaze);
        actionbar.setTitle(title);
    }
    toolbar.setTitleTextAppearance(this, R.style.RobotoBoldTextAppearance);
    initDrawer();
    }

private void initDrawer() {
    drawerLayout = findViewById(R.id.drawer_layout);

    Toolbar toolbar = findViewById(R.id.toolbar);
    NavigationUI.setupWithNavController(toolbar, controller, drawerLayout);
    }

我是跟着导游做的,但是很奇怪,在我看来一定没问题。

我不喜欢我不使用 NavigationView,我认为这不好。

根据 Add a Navigation Drawer documentation, you need to call setupWithNavController with your NavigationView if you want to hook it up to the NavController. In addition, you're using setSupportActionBar(). As per the Action Bar documentation,您不应使用 NavigationUI.setupWithNavController(toolbar, controller, drawerLayout),而应使用 setupActionBarWithNavController() 方法

private void initDrawer() {
    drawerLayout = findViewById(R.id.drawer_layout);

    // Set up the Action Bar with NavController
    NavigationUI.setupActionBarWithNavController(this, controller, drawerLayout);

    // Now hook up your NavigationView
    NavigationView navigationView = findViewById(R.id.nv);
    NavigationUI.setupWithNavController(navigationView, controller);
}

根据 Action Bar documentation:

使用 setSupportActionBar() 时,您还需要覆盖 onSupportNavigateUp()
@Override
public boolean onSupportNavigateUp() {
    return navController.navigateUp(drawerLayout) || super.onSupportNavigateUp();
}

我还应该注意 if (actionBar != null) 检查和其中的代码被导航覆盖 - 标题应该通过目的地上的 android:label 设置。