我在DrawerLayout中使用NavigationView,我不确定我是否忘记了某些内容,但是当我点击它时只是隐藏了导航。
这是我的代码:
MainActivity.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" />
menu_drawer.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:
答案 0 :(得分:0)
我解决了它,是布局中的orden。
如你所见,我把NavigationView放在第一位,当我点击NavigationView时我认为我点击了LinearLayout,或者我认为这就是发生的事情。
只需更改订单即可。