簡體   English   中英

選定的偵聽器上的Android導航菜單未觸發

[英]Android Navigation Menu on item selected listener not firing

我設置了導航菜單,但是每當我從中選擇一個項目時,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。

我懷疑這與同一活動中的所有內容(地圖和導航抽屜)有關,但我不確定。

因為您的NavigationViewLinearLayoutfragment覆蓋了。 請通過以下方式將視圖序列放入activity_maps.xml中:

<DrawerLayout >

    <LinearLayout />

    <fragment />

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

</DrawerLayout>

並且不要忘記 return true; 在您的聽眾中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM