【问题标题】:Android Navigation Menu on item selected listener not firing项目选定侦听器上的 Android 导航菜单未触发
【发布时间】:2017-07-18 22:14:26
【问题描述】:

我设置了一个导航菜单,但是每当我从中选择一个项目时,都不会调用 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。

我怀疑这与处于同一活动中的所有内容(地图和导航抽屉)有关,但我不确定。

【问题讨论】:

    标签: java android google-maps navigation-drawer


    【解决方案1】:

    因为您的NavigationView 已被您的LinearLayoutfragment 覆盖。请把你的视图顺序这样放在activity_maps.xml中:

    <DrawerLayout >
    
        <LinearLayout />
    
        <fragment />
    
        <NavigationView /> <!-- on the top of views -->
    
    </DrawerLayout>
    

    并且不要忘记 return true; 在您的听众中。

    【讨论】:

    • 即使我已经 5 年没写过 android 代码了,但在我的情况下这有点奇怪,我的导航没有被覆盖,但我在导航视图下确实有约束布局,在将其向下移动后使其导航在约束下查看,如果您提供一些解释,我真的很感激。
    猜你喜欢
    • 2015-12-27
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多