setNavigationItemSelectedListener在Android中不起作用

问题描述

当我从导航菜单中选择Iten时,

setNavigationItemSelectedListener无法正常工作。当我设置setNavigationItemSelectedListener(this)时,程序不会调用 public boolean onNavigationItemSelected(@NonNull MenuItem项)此函数。我该如何解决enter image description here

    mToolbar = findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);
    mDrawerLayout = findViewById(R.id.drawer);

    mNavigationView = findViewById(R.id.navigationView);
    mNavigationView.setNavigationItemSelectedListener(this);

    mActionBarDrawerToggle = new ActionBarDrawerToggle
    (this,mDrawerLayout,mToolbar,R.string.Open,R.string.Close);
    mDrawerLayout.addDrawerListener(mActionBarDrawerToggle);
    mActionBarDrawerToggle.syncState();

xml

<androidx.drawerlayout.widget.DrawerLayout 
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/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".Pages.User.Dashboard"
    tools:openDrawer="start">


<com.google.android.material.navigation.NavigationView

    android:id="@+id/navigationView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/drawer_header"
    app:menu="@menu/drawer_menu"> 
 </com.google.android.material.navigation.NavigationView>

 <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="@color/black"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:titleTextColor="@color/white" />

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

  </LinearLayout>


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Home"
        android:textSize="30sp" />

 </RelativeLayout>

</androidx.drawerlayout.widget.DrawerLayout>

解决方法

这对我很有用,可以将视图展现在最前面

navigationView.setNavigationItemSelectedListener(this); navigationView.bringToFront();

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...