3
我在DrawerLayout中使用NavigationView,我不確定是否忘記了某些內容,但只是點擊時隱藏導航。導航視圖不調用onNavigationItemSelected方法
這是我的代碼:
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:
爲什麼每次使用'System.gc()'? –
我是一個begginer,我總是試着清理內存,我開始或結束和活動。 –
這是沒有必要的,只是'完成()'和系統將自己做所有 –