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

时间:2017-07-18 22:14:26

标签: java android google-maps navigation-drawer

我设置了一个导航菜单,但每当我从中选择一个项目时,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" />

导航菜单打开和关闭完全正常,甚至在我选择项目后关闭,但是两个日志或吐司都没有出现,表明从未调用过onNavigationItemSelected。

我怀疑这与所有事情(地图和nagivation抽屉)在同一活动中有关,但我不确定。

1 个答案:

答案 0 :(得分:3)

因为NavigationViewLinearLayout涵盖了fragment。请以这种方式将您的观看顺序放在activity_maps.xml中:

<DrawerLayout >

    <LinearLayout />

    <fragment />

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

</DrawerLayout>

请勿忘记 return true;