解决方案:
请尝试执行以下步骤以使其正常工作:
Step1:实施onNavigationItemSelected
如下所示:
.... extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
Step2:使用接口生成的方法为:
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.your_drawer_item_id:
....
....
case R.id.your_drawer_item_id:
....
....
}
}
Step3:在你的类中声明一个全局变量:
public NavigationView navigationView;
Step4:在你的onCreate()
将变量初始化为:
navigationView = (NavigationView) findViewById(R.id.your_nav_view);
Finally:设置导航视图:
navigationView.setNavigationItemSelectedListener(this);
就是这样。
希望它有效。
抽屉布局 xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_vendor_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_drawer_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/White"
android:fitsSystemWindows="true"
app:itemBackground="@android:color/white"
app:itemIconTint="@color/bottom_color"
app:itemTextColor="@color/bottom_color"
app:headerLayout="@layout/nav_header_vendor_drawer_layout"
app:menu="@menu/vendor_drawer_menu" >
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
app_bar_vendor_drawer_layout xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="logixtic.android.web.vd.driver.activities.DriverDrawerActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/MyMaterialTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/Project_Standard"
app:popupTheme="@style/MyMaterialTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:layout_above="@+id/bottom_navigation">
</FrameLayout>
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="@drawable/toolbar_dropshadow_above"
android:layout_above="@id/bottom_navigation"/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="@color/White"
app:itemIconTint="@color/bottom_color"
app:itemTextColor="@color/bottom_color"
app:menu="@menu/vendor_bottom_navigation"/>
</RelativeLayout>
Try it.