【问题标题】:How do I disable the animation in Bottom Navigation View when icons are clicked?单击图标时如何禁用底部导航视图中的动画?
【发布时间】:2019-04-09 14:18:44
【问题描述】:
我正在为我的应用程序使用默认的 BottomView 导航栏,该导航栏有 4 个按钮,并且它们的移动动画很糟糕,而且在兼容库中似乎没有方法。禁用它。请帮忙。
P.s 我不想使用第三方底部导航。
【问题讨论】:
标签:
android
android-animation
bottomnavigationview
【解决方案1】:
//Create a new class Bottom Navigation Bar helper and then implement it in the buttons classes
public class BottomNavigationViewHelper {
@SuppressLint("RestrictedApi")
public static void disableShiftMode(BottomNavigationView view) {
BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
try {
Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
shiftingMode.setAccessible(true);
shiftingMode.setBoolean(menuView, false);
shiftingMode.setAccessible(false);
for (int i = 0; i < menuView.getChildCount(); i++) {
BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
//noinspection RestrictedApi
item.setShiftingMode(false);
// set once again checked value, so view will be updated
//noinspection RestrictedApi
item.setChecked(item.getItemData().isChecked());
}
} catch (NoSuchFieldException e) {
Log.e("BNVHelper", "Unable to get shift mode field", e);
} catch (IllegalAccessException e) {
Log.e("BNVHelper", "Unable to change value of shift mode", e);
}
}
//and then just use this code in every button class
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity);
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavViewBar);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
BottomNavigationViewHelper.enableNavigation(mContext, bottomNavigationView);
Menu menu=bottomNavigationView.getMenu();
MenuItem menuItem=menu.getItem(ACTIVITY_NUM);
menuItem.setChecked(true);
}
这将使按钮性能良好。
我是android新手,代码不是我写的,希望对你有帮助