简体   繁体   English

从 NavigationView 启动新活动

[英]Starting new Activity from NavigationView

I have tried to start new Activity from NavigationView with OnNavigationItemSelectedListener(), but it won't work.我试图用 OnNavigationItemSelectedListener() 从 NavigationView 开始新的 Activity,但它不起作用。 I also have a Bottom NavigationView which works fine.我还有一个可以正常工作的底部导航视图。 I also tried with if else statement.我也尝试过 if else 语句。 In my opinion the problem is somewhere adding the listener to the Navigatopnview,because, the BottomNavigationView gets me the log info.在我看来,问题出在将侦听器添加到 Navigatopnview 的某个地方,因为 BottomNavigationView 为我获取了日志信息。

This how I approach the problem.这就是我处理问题的方式。 I dont get any logs clicking on anything.我没有得到任何日志点击任何东西。

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;
        }
    });

I have this XML layout file.我有这个 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>

Also this menu XML还有这个菜单 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"/>

Move the NavigationView under the ConstraintLayout在 ConstraintLayout 下移动 NavigationView

Try this,尝试这个,

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM