我的应用程序中有一个带有不同活动的导航抽屉。一切顺利,但我有一个大问题。当我想更改导航抽屉中的活动时,该项目未被检查或检查错误。例如,我想进入活动B,当我打开导航抽屉未被选中或被选中另一个项目时。
这里有一些代码:
项目:
`
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_home"
android:title="@string/Button_home"/>
<group android:checkableBehavior="single"
android:id="@+id/anonimous_group">
<item
android:id="@+id/nav_registrati"
android:icon="@mipmap/ic_create_black_24dp"
android:title="@string/registrati"/>
<item
android:id="@+id/nav_login"
android:icon="@mipmap/ic_filter_tilt_shift_black_24dp"
android:title="@string/accedi"/>
</group>
<group android:checkableBehavior="single"
android:id="@+id/logged_group"
>
<item
android:id="@+id/nav_profilo"
android:icon="@drawable/id_profile"
android:title="@string/Profilo"/>
<item
android:id="@+id/nav_notifiche"
android:icon="@drawable/ic_notifications"
android:title="@string/Notifiche"/>
<item
android:id="@+id/nav_mie_recensioni"
android:icon="@drawable/ic_review"
android:title="@string/mie_recensioni"/>
<item
android:id="@+id/nav_persone_seguo"
android:icon="@drawable/ic_person"
android:title="@string/persone_seguo"/>
<item
android:id="@+id/nav_oggetti_seguo"
android:icon="@drawable/ic_favorite"
android:title="@string/oggetti_seguo"/>
<item
android:id="@+id/nav_inserisci_recensione"
android:icon="@drawable/ic_review"
android:title="@string/inserisci_recensione"/>
<item
android:id="@+id/nav_settings"
android:icon="@drawable/ic_build"
android:title="@string/action_settings"/>
<item
android:id="@+id/nav_esci"
android:icon="@mipmap/ic_exit_to_app_black_24dp"
android:title="@string/exit"/>
</group>
<item
android:id="@+id/nav_Assistenza"
android:icon="@drawable/ic_help"
android:title="@string/Assistenza"/>
和superActivity:
@Override
public boolean onNavigationItemSelected(MenuItem item) {
Log.i("Successo","SOno entrato"+item.getItemId());
// now set clicked menu item to checked
Intent intent;
switch(item.getItemId()) {
case R.id.nav_home:
intent = new Intent(ctx, A10_HomePage.class);
break;
case R.id.nav_registrati:
intent = new Intent(ctx, A1_LoginIniziale.class);
editor.putString("previous", "REG").apply();
break;
case R.id.nav_login:
intent = new Intent(ctx, A1_LoginIniziale.class);
editor.putString("previous", "LOGIN").apply();
break;
case R.id.nav_notifiche:
intent = new Intent(ctx, A14_Notifiche.class);
break;
case R.id.nav_mie_recensioni:
intent = new Intent(ctx, A23_MieRecensioni.class);
break;
case R.id.nav_oggetti_seguo:
intent = new Intent(ctx, A22_OggettiSeguiti.class);
break;
case R.id.nav_persone_seguo:
intent = new Intent(ctx, A19_PersoneSeguite.class);
break;
case R.id.nav_inserisci_recensione:
intent = new Intent(ctx, A15_InserisciRecensione.class);
break;
case R.id.nav_settings:
intent = new Intent(ctx, A6_Impostazioni.class);
break;
case R.id.nav_Assistenza:
intent = new Intent(ctx, A35_Assistenza.class);
break;
case R.id.nav_esci:
removeUser();
intent = new Intent(ctx, A1_LoginIniziale.class);
break;
case R.id.nav_profilo:
intent = new Intent(ctx, A7_ProfiloPrivato.class);
break;
default:
intent = new Intent(ctx, A10_HomePage.class);
break;
}
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
drawer.closeDrawer(GravityCompat.START);
return true;
}
布局:
<android.support.design.widget.NavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemIconTint="@color/drawer_item_text"
app:itemTextColor="@color/drawer_item_text"
app:headerLayout="@layout/nav_header"/>
答案 0 :(得分:0)
我也发现了这个问题,我用
解决了这个问题<强> onNavigationItemSelected(navView.getMenu()的getItem(R.id.nav_轮廓)); 强>
onNavigationItemSelected(navView.getMenu()。getItem(0)); 默认使用此选项进行第一项选择。
答案 1 :(得分:0)
添加此drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#FFFFFF" android:state_checked="true" />
<item android:color="#7D7D7F" android:state_checked="false" android:state_enabled="false" />
<item android:color="#7D7D7F" android:state_enabled="false" />
<item android:color="#7D7D7F" />
</selector>
然后将此drawable用于文本视图
<android.support.design.widget.NavigationView
xmlns:android="``http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemIconTint="@color/drawer_item_text"
app:itemTextColor="@color/navigation_text_color"
app:headerLayout="@layout/nav_header"
你也可以为图标做同样的事情