How to disable BottomNavigationView shift mode?

后端 未结 21 2248
Happy的楠姐
Happy的楠姐 2020-11-22 15:55

BottomNavigationView doesn\'t show menu\'s title that are inactive.

How to show titles of all menu elements in bottomNavigationBar? The problem is that in my case s

21条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 16:38

    To completely remove animations:

    If you also want to get rid of that annoying little top margin animation, you need more reflection code. Here's the complete solution that removes any animation:

    @SuppressLint("RestrictedApi")
    private 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);
                item.setShiftingMode(false);
    
                Field shiftAmount = item.getClass().getDeclaredField("mShiftAmount");
                shiftAmount.setAccessible(true);
                shiftAmount.setInt(item, 0);
                shiftAmount.setAccessible(false);
    
                item.setChecked(item.getItemData().isChecked());
            }
        } catch (NoSuchFieldException e) {
            Timber.e(e, "Unable to get fields");
        } catch (IllegalAccessException e) {
            Timber.e(e, "Unable to change values");
        }
    }
    

    And make sure to add that to your proguard configuration file:

    -keepclassmembers class android.support.design.internal.BottomNavigationMenuView { 
        boolean mShiftingMode; 
    }
    -keepclassmembers class android.support.design.internal.BottomNavigationItemView { 
        int mShiftAmount;
    }
    

提交回复
热议问题