2016-01-15 21 views
0

這是我onNavigationItemSelected在Android的菜單不觸發點擊

@SuppressWarnings("StatementWithEmptyBody") 
@Override 
public boolean onNavigationItemSelected(MenuItem item) { 
    // Handle navigation view item clicks here. 
    int id = item.getItemId(); 

    if (id == R.id.nav_home) { 
     Intent intent = new Intent(HomeActivity.this, HomeActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
     HomeActivity.this.startActivity(intent); 
     return true; 
    } else if (id == R.id.nav_extrato) { 
     //Nothing yet 
    } else if (id == R.id.nav_bloqueio) { 
     //Nothing yet 
    } else if (id == R.id.nav_rede) { 
     //Nothing yet 
    } else if (id == R.id.nav_logout) { 
     signOut(); 
     return true; 
    } 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    drawer.closeDrawer(GravityCompat.START); 
    return true; 
} 

這是我的菜單:http://imgur.com/f2Fpa7C

我在函數的開始一個斷點,只停留在斷點當我點擊在主頁按鈕上。

編輯:我的菜單不是一個列表視圖。這裏是我的菜單:

<android.support.design.widget.NavigationView 
    android:id="@+id/nav_view" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:fitsSystemWindows="true" 
    android:background="@color/background" 
    app:menu="@menu/activity_home_drawer" /> 

和 「@菜單/ activity_home_drawer」 是這樣的:

<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:id="@+id/nav_home" 
     android:icon="@drawable/home" 
     android:title="Home" /> 
    <item 
     android:id="@+id/nav_extrato" 
     android:icon="@drawable/extrato" 
     android:title="Extrato" /> 
    <item 
     android:id="@+id/nav_bloqueio" 
     android:icon="@drawable/bloqueio" 
     android:title="Bloqueio" /> 
    <item 
     android:id="@+id/nav_rede" 
     android:icon="@drawable/rede" 
     android:title="Rede Credenciada" /> 
    <item 
     android:id="@+id/nav_logout" 
     android:icon="@drawable/exit" 
     android:title="Logout" /> 

EDIT 2

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.home, menu); 
    return true; 
} 

首頁:

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto"> 
<item 
    android:id="@+id/act_logout" 
    android:orderInCategory="100" 
    android:title="" 
    android:icon="@drawable/blank" 
    app:showAsAction="always" /> 

+0

你已經看到你的代碼,你做了什麼?您正在打開當前打開的同一活動,而且沒有動畫。如何注意任何變化 –

回答

0

您必須覆蓋@Override public boolean onOptionsItemSelected(MenuItem item)功能,然後用item.getItemId()

希望這有助於匹配的項目ID。

更新 我抓到你了。您正在討論導航項目。抽屜式導航欄實際上包含ListView,你必須落實ListViewOnItemClickListener來獲取項目

ListView listViewDrawer = (ListView) findViewById(R.id.listview_leftdrawer); 
listViewDrawer.setOnItemClickListener(new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { 
}); 
+0

是的,我這樣做,但是當我點擊任何不是第一個(家庭)的按鈕時不會觸發onOptionsItemSelected函數 –

+0

請顯示'onCreateOptionsMenu',你是否使用任何自定義佈局對於菜單 –

+0

'onOptionsItemSelected'在您從'onCreateOptionsMene'創建菜單並對菜單進行缺省調用時調用。請參閱更新的答案 –