2017-07-18 57 views
0

我有一個導航菜單設置,但每當我從它選擇一個項目onNavigationItemSelected不會被調用。安卓導航菜單項目選定的監聽器沒有發射

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

導航菜單打開和關閉完美的罰款,有一次我選擇一個項目甚至關閉,但是不會出現兩個日誌或吐司,顯示onNavigationIte mSelected從未被調用過。

我懷疑它與一切事物(地圖和激動抽屜)在同一活動中,但我不確定。

回答

1

因爲您的NavigationView已被您的LinearLayoutfragment覆蓋。請把你的意見序列中activity_maps.xml這樣:

<DrawerLayout > 

    <LinearLayout /> 

    <fragment /> 

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

</DrawerLayout> 

而且不要忘了你的聽衆return true;