如何在单击图标时禁用底部导航视图中的动画?

时间:2017-11-04 09:34:43

标签: android android-animation bottomnavigationview

我正在使用我的应用程序的默认BottomView导航栏,它有4个按钮,并且它们有一个可怕的移动动画,并且似乎在compat lib中没有方法。禁用它。请帮忙。

P.s我不想使用第三方底部导航。

2 个答案:

答案 0 :(得分: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的新手,代码不是我写的,但我希望它可以帮助你

答案 1 :(得分:0)

对于新的设计支持库(即来自 28.0.0-alpha1 的),无需为底部导航添加帮助器类,而应在XML文件中进行定义:

希望有帮助。