gpt4 book ai didi

android - 导航 View 未调用 onNavigationItemSelected 方法

转载 作者:太空狗 更新时间:2023-10-29 14:51:59 25 4
gpt4 key购买 nike

我在 DrawerLayout 中使用 NavigationView,我不确定我是否忘记了什么,但只是在我单击它时隐藏了 Navigation。

这是我的代码:

主要 Activity .java:

  private void instantiateDrawerLayout() {
Toolbar mToolbar = (Toolbar) generalContent.findViewById(R.id.toolbar);
mToolbar.setBackgroundColor(getResources().getColor(R.color.colorMainActivity));
setSupportActionBar(mToolbar);
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setItemIconTintList(ColorStateList.valueOf(getResources().getColor(R.color.colorMainActivity)));
navigationView.setNavigationItemSelectedListener(this);
Log.d("instantiateDrawerLayout", "called");
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, mDrawer, mToolbar, R.string.drawer_open, R.string.drawer_close);
mDrawer.setDrawerListener(drawerToggle);
drawerToggle.syncState();
...
}

@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
Log.d("onNavigationItem", "called");
final int itemId = menuItem.getItemId();
Intent intent;
switch (itemId) {
case R.id.navigation_item_1:
hideDrawer();
break;
case R.id.navigation_item_2:
intent = new Intent(this, FirstActivity.class);
startActivity(intent);
this.finish();
break;
case R.id.navigation_item_3:
intent = new Intent(this, SecondActivity.class);
startActivity(intent);
this.finish();
break;
case R.id.navigation_item_4:
intent = new Intent(this, ThirdActivity.class);
startActivity(intent);
this.finish();
break;
}
return false;
}

activity_general.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackground">

<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_header"
app:itemIconTint="@color/colorAccent"
app:menu="@menu/menu_drawer" />

<LinearLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:orientation="vertical" />

菜单抽屉.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!--All the items added without any grouping-->

<item
android:id="@+id/navigation_item_1"
android:icon="@drawable/ic_number_1"
android:title="@string/navigation_item_1" />
<item
android:id="@+id/navigation_item_2"
android:icon="@drawable/ic_number_2"
android:title="@string/navigation_item_2" />

<item
android:id="@+id/navigation_item_3"
android:icon="@drawable/ic_number_3"
android:title="@string/navigation_item_3" />
<item
android:id="@+id/navigation_item_4"
android:icon="@drawable/ic_number_4"
android:title="@string/navigation_item_4" />

</menu>

和我的 Logcat:

enter image description here

最佳答案

我解决了,是为了Layout中的orden。

如您所见,我将 NavigationView 放在第一位,这导致当我点击 NavigationView 时认为我点击了 LinearLayout,或者我认为这就是发生的事情。

只需更改顺序即可。

关于android - 导航 View 未调用 onNavigationItemSelected 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34539287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com