【发布时间】:2020-06-03 13:09:30
【问题描述】:
我使用 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();
}
【问题讨论】:
-
我很久没有和
BottomNavigation合作了,但我记得如果你只是改变Fragment这并不意味着图标选择也会改变。您必须取消选择已选择的图标并选择应该选择的图标。这就是我一直这样做的方式。
标签: android fragment androidx bottomnavigationview onbackpressed