我使用BottomNavigationView,但是当我单击后退按钮时,菜单图标不会更改(但是片段会更改)。例如,我在主菜单上,并且通过触摸设置菜单被移到该菜单,但是当我单击“后退”按钮时,我被移到了主片段,但是未选择主图标,并且它仍然设置图标。
我已经使用了以下库。
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
以及XML:
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.bottomnavigation.BottomNavigationView
app:labelVisibilityMode="labeled"
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layoutDirection="rtl"
android:background="@color/bgBottomNavigation"
android:foreground="?attr/selectableItemBackground"
app:itemBackground="@color/bgBottomNavigation"
app:layout_constraintBottom_toBottomOf="@+id/constraint"
app:menu="@menu/bottom_navigation"
app:itemIconTint="@drawable/bnv_tab_item_foreground"
app:itemTextColor="@drawable/bnv_tab_item_foreground"
tools:ignore="MissingConstraints" />
并且在菜单中:
<?xml version="1.0" encoding="utf-8"?>
<item
android:enabled="true"
android:id="@+id/navigation_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/title_home" />
<item
android:enabled="true"
android:id="@+id/navigation_cart"
android:icon="@drawable/ic_shopping_cart_black_24dp"
android:title="@string/title_cart" />
<item
android:enabled="true"
android:id="@+id/navigation_bookmark"
android:icon="@drawable/ic_bookmark_black_24dp"
android:title="@string/title_bookmark" />
<item
android:enabled="true"
android:id="@+id/navigation_profile"
android:icon="@drawable/ic_settings_black_24dp"
android:title="@string/title_profile" />
在Java中:
bottomNavigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
bottomNavigationView.setSelectedItemId(R.id.navigation_home);
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment fragment;
actionId = item.getItemId();
switch (item.getItemId()) {
case R.id.navigation_home:
fragment = new HomeFragment();
loadFragment(fragment);
return true;
case R.id.navigation_cart:
fragment = new CartFragment();
loadFragment(fragment);
return true;
case R.id.navigation_bookmark:
fragment = new BookmarkFragment();
loadFragment(fragment);
return true;
case R.id.navigation_profile:
fragment = new SettingsFragment();
loadFragment(fragment);
return true;
}
return false;
}
};
和loadFragment():
private void loadFragment(Fragment fragment) {
// load fragment
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
[内部方法loadFragment
中,您已经调用了transaction.addToBackStack(null)
,这意味着当您按返回顶部时,Fragment
将弹出,并且菜单仍保持当前选择。
如果在回叫时需要finish
,则应删除呼叫transaction.addToBackStack(null)
如果您想保留此行为,并且必须更新菜单,请将此代码放入活动中
@Override
public void onAttachFragment(@NonNull Fragment fragment) {
super.onAttachFragment(fragment);
if (fragment instanceof HomeFragment) {
bottomNavigationView.setSelectedItemId(R.id.navigation_home);
} else if (fragment instanceof CartFragment) {
bottomNavigationView.setSelectedItemId(R.id.navigation_cart);
} // others your fragments
}