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

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

导航菜单可以很好地打开和关闭,甚至在我选择一个项目后关闭,但是两个日志或 toast 都没有出现,表明从未调用过 onNavigationItemSelected。

我怀疑它与同一 Activity 中的所有内容( map 和抽屉导航)有关,但我不确定。

最佳答案

因为你的 NavigationView 被你的 LinearLayoutfragment 覆盖了。请按以下方式将您的 View 顺序放入 activity_maps.xml:

<DrawerLayout >

    <LinearLayout />

    <fragment />

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

</DrawerLayout>

不要忘记 return true; 在你的监听器中。

关于java - 项目选定监听器上的 Android 导航菜单未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45178003/

相关文章:

javascript - 如何在输入地址时发送新的地理定位请求并刷新 Google map ?

java - Google Places API - 保存 place_id 并违反条款和条件

android - android 中的谷歌地图 v2 中的多色折线

java - 检测选项卡布局上的哪个位置

java - Eclipse 文本字段中的输入类型

java - 在JavaFX的FXML中,如何声明自定义事件的处理程序?

java - Android sqlite游标查询

android - 基于 firebase 的移动应用数据服务器设计

java - 提供者递归问题

java string.contains 在 switch 语句中