2017-07-18 2 views
0

J'ai un menu de navigation configuré, mais chaque fois que je sélectionne un élément, onNavigationItemSelected n'est pas appelé.Menu de navigation Android sur l'élément sélectionné auditeur ne se déclenche pas

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

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

Le menu de navigation ouvre et ferme parfaitement bien, et ferme, même une fois que je sélectionne un élément, mais ni les deux journaux ni le toast n'apparaissent, indiquant que le onNavigationIte mSelected n'a jamais été appelé.

Je soupçonne que cela a quelque chose à voir avec tout (les cartes et le tiroir de nagivation) étant dans la même activité mais je ne suis pas sûr.

Répondre

1

Parce que votre NavigationView était couvert par vos LinearLayout et fragment. S'il vous plaît mettre la séquence de vos vues activity_maps.xml ainsi:

<DrawerLayout > 

    <LinearLayout /> 

    <fragment /> 

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

</DrawerLayout> 

Et NE PAS OUBLIERreturn true; dans votre écouteur.