Android菜单没有触发点击

时间:2016-01-15 11:16:38

标签: android appbar

这是我的onNavigationItemSelected

@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" />

和&#34; @ menu / activity_home_drawer&#34;是这样的:

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

编辑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" />

1 个答案:

答案 0 :(得分:0)

您必须覆盖@Override public boolean onOptionsItemSelected(MenuItem item)功能,然后将商品ID与item.getItemId()

相匹配

希望这有帮助。

<强>更新 我抓到你了。你在谈论导航项目。导航抽屉实际上包含ListView,您必须在OnItemClickListener上实施ListView才能获取该项

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) {
});