简体   繁体   English

选定的侦听器上的Android导航菜单未触发

[英]Android Navigation Menu on item selected listener not firing

I have a navigation menu set up but whenever I select an item from it the onNavigationItemSelected does not get called. 我设置了导航菜单,但是每当我从中选择一个项目时,onNavigationItemSelected都不会被调用。

MapsActivity.java MapsActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);

    initializeMap();

    googleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this, this)
            .addConnectionCallbacks(this)
            .addApi(LocationServices.API)
            .build();

    navigationMenu = (NavigationView) findViewById(R.id.navigation_menu);

    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);

    mDrawerLayout.addDrawerListener(mToggle);
    mToggle.syncState();

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    toast = Toast.makeText(this, "test", Toast.LENGTH_LONG);

    navigationMenu.setNavigationItemSelectedListener(this);
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    toast.show();
    Log.v("NAV", "Navigation item selected");
    Log.v("ITEM", item.getItemId() + "");
    switch (item.getItemId()){
        case R.id.nav_restaurant: {
            //do stuff
        }
        case R.id.nav_pharmacy: {
            //do stuff
        }
        case R.id.nav_bank: {
            //do stuff
        }
    }
    return false;
}

activity_maps.xml activity_maps.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout">

<android.support.design.widget.NavigationView
    android:layout_width="200dp"
    android:layout_height="match_parent"
    app:menu="@menu/navigation_menu"
    android:id="@+id/navigation_menu"
    android:layout_gravity="start"
    app:headerLayout="@layout/navigation_header">

</android.support.design.widget.NavigationView>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Main Layout"
        android:layout_gravity="center"
        android:textAlignment="center"
        android:textSize="30dp"/>

</LinearLayout>

<fragment
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.damia.placefinder.activities.MapsActivity" />

The navigation menu opens and closes perfectly fine, and even closes once I select an item, but neither the two logs or the toast appears, showing that the onNavigationItemSelected was never called. 导航菜单可以很好地打开和关闭,甚至在我选择一个项目后也可以关闭,但是两个日志或Toast都不会出现,这表明从未调用过onNavigationItemSelected。

I suspect it has something to do with everything (the maps and nagivation drawer) being in the same activity but I'm not sure. 我怀疑这与同一活动中的所有内容(地图和导航抽屉)有关,但我不确定。

Because your NavigationView was covered by your LinearLayout and fragment . 因为您的NavigationViewLinearLayoutfragment覆盖了。 Please put the sequence of your views in activity_maps.xml in this way: 请通过以下方式将视图序列放入activity_maps.xml中:

<DrawerLayout >

    <LinearLayout />

    <fragment />

    <NavigationView /> <!-- on the top of views -->

</DrawerLayout>

And DO NOT FORGET return true; 并且不要忘记 return true; in your listener. 在您的听众中。

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

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