Android 从NavigationView启动新活动

Android 从NavigationView启动新活动,android,Android,我已尝试使用OnNavigationItemSelectedListener()从NavigationView启动新活动,但无效。我还有一个底部导航视图,它工作得很好。我还尝试了if-else语句。在我看来,问题在于将侦听器添加到NavigationView的某个地方,因为BottomNavigationView会获取日志信息 这就是我处理这个问题的方法。我没有任何日志点击任何东西 navigationView.setNavigationItemSelectedListener(new Navi

我已尝试使用OnNavigationItemSelectedListener()从NavigationView启动新活动,但无效。我还有一个底部导航视图,它工作得很好。我还尝试了if-else语句。在我看来,问题在于将侦听器添加到NavigationView的某个地方,因为BottomNavigationView会获取日志信息

这就是我处理这个问题的方法。我没有任何日志点击任何东西

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            Log.i(TAG, "onNavigationItemSelected: " + item.getItemId());
            switch (item.getItemId()) {

                case R.id.nav_recent:
                    startActivity(new Intent(DashboardActivity.this, RecentActivity.class));
                    return true;

                case R.id.nav_offline:
                    Intent intentoffline = new Intent(DashboardActivity.this, OfflineActivity.class);
                    startActivity(intentoffline);
                    return true;

                case R.id.nav_trash:
                    Intent intenttrash = new Intent(DashboardActivity.this, TrashActivity.class);
                    startActivity(intenttrash);
                    return true;

                case R.id.nav_settings:
                    Intent intentsettings = new Intent(DashboardActivity.this, SettingsActivity.class);
                    startActivity(intentsettings);
                    return true;

                case R.id.nav_help:
                    Intent intenthelp = new Intent(DashboardActivity.this, HelpActivity.class);
                    startActivity(intenthelp);
                    return true;
            }
            drawerLayout.closeDrawer(GravityCompat.START);
            Log.i(TAG, "onNavigationItemSelected: nothing clicked");
            return false;
        }
    });
我有这个XML布局文件

<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:windowActionBar="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
android:id="@+id/drawerlayout"
tools:context=".Activities.DashboardActivity"
tools:ignore="InvalidId">


<com.google.android.material.navigation.NavigationView
    android:id="@+id/navigationview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/navigation_header"
    app:menu="@menu/navigation_menu" />

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:background="#383838"
        android:elevation="4dp"
        android:theme="?attr/actionBarTheme"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <SearchView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:layout_editor_absoluteX="16dp" />

    </androidx.appcompat.widget.Toolbar>

    <!-- Fragments Container -->

    <include
        layout="@layout/content_main"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottomnavigationview"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomnavigationview"
        android:layout_width="0dp"
        android:layout_height="55dp"
        android:background="#383838"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_menu" />


</androidx.constraintlayout.widget.ConstraintLayout>

此外,此菜单还包含XML

<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:id="@+id/app_bar_switch"
    android:title="@string/darkmode"
    app:actionLayout="@layout/switch_item"
    app:showAsAction="always"
    android:icon="@drawable/baseline_nights_stay_white_48dp"/>
<item
    android:id="@+id/nav_recent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:title="@string/recent"
    android:icon="@drawable/baseline_av_timer_white_48dp"/>

<item
    android:id="@+id/nav_offline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:title="@string/offline"
    android:icon="@drawable/baseline_offline_pin_white_48dp"/>

<item
    android:id="@+id/nav_trash"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:title="@string/trash"
    android:icon="@drawable/baseline_delete_white_48dp"/>

<item
    android:id="@+id/nav_settings"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:title="@string/settings"
    android:icon="@drawable/baseline_settings_white_48dp"/>

<item
    android:id="@+id/nav_help"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:title="@string/help"
    android:icon="@drawable/baseline_help_white_48dp"/>


将NavigationView移动到ConstraintLayout下

试试这个

<androidx.drawerlayout.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:windowActionBar="false"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start"
    android:id="@+id/drawerlayout"
    tools:ignore="InvalidId">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:background="#383838"
            android:elevation="4dp"
            android:theme="?attr/actionBarTheme"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <SearchView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:layout_editor_absoluteX="16dp" />

        </androidx.appcompat.widget.Toolbar>

        <!-- Fragments Container -->

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomnavigationview"
            android:layout_width="0dp"
            android:layout_height="55dp"
            android:background="#383838"
            android:visibility="visible"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:menu="@menu/navigation_menu" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigationview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/navigation_menu" />
</androidx.drawerlayout.widget.DrawerLayout>

将NavigationView移动到ConstraintLayout下

试试这个

<androidx.drawerlayout.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:windowActionBar="false"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start"
    android:id="@+id/drawerlayout"
    tools:ignore="InvalidId">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:background="#383838"
            android:elevation="4dp"
            android:theme="?attr/actionBarTheme"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <SearchView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:layout_editor_absoluteX="16dp" />

        </androidx.appcompat.widget.Toolbar>

        <!-- Fragments Container -->

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomnavigationview"
            android:layout_width="0dp"
            android:layout_height="55dp"
            android:background="#383838"
            android:visibility="visible"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:menu="@menu/navigation_menu" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/navigationview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/navigation_menu" />
</androidx.drawerlayout.widget.DrawerLayout>


谢谢,这很有帮助,现在我的NavigationView可以正常工作了。所有这些现在都默认显示在屏幕上,我无法切换。谢谢,这很有帮助,现在我的NavigationView工作了。所有这些现在都默认显示在屏幕上,我无法切换。