问题描述
当我从导航菜单中选择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();