从BottomNavigationView android中删除动画/转移模式

32 android bottomnavigationview android-bottomnav

我正在构建一个我有BottomNavigationView的应用程序.一切正常,直到我进入活动.

导航是这样的:

在此输入图像描述

问题是它有这个默认动画,因此它每次都会推动活动元素高于其余元素.

另一个例子:

在此输入图像描述

所以我的问题是如何摆脱这个默认动画,当我在它们之间切换时,每个项目都是对齐的?

我的代码:

public class MainActivity extends AppCompatActivity {
    private BottomNavigationView bottomNavigationView;
    private Fragment fragment;
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setupBottomBar();
    }

    private void setupBottomBar() {
        bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottomBar);
        fragmentManager = getSupportFragmentManager();
        fragment = new CardDeckFragment();
        transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.activity_main, fragment).commit();
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.action_card_deck:{
                        Toast.makeText(MainActivity.this, "Card Deck Selected", Toast.LENGTH_SHORT).show();
                        fragment = new CardDeckFragment();
                        break;
                    }
                    case R.id.action_favorites:{
                        Toast.makeText(MainActivity.this, "Favorites Selected", Toast.LENGTH_SHORT).show();
                        fragment = new FavoritesFragment();
                        break;
                    }
                    case R.id.action_favorites_another:{
                        Toast.makeText(MainActivity.this, "Image Selected", Toast.LENGTH_SHORT).show();
                        fragment = new ImageFragment();
                        break;
                    }
                    case R.id.action_profile:{
                        Toast.makeText(MainActivity.this, "Profile Selected", Toast.LENGTH_SHORT).show();
                        fragment = new ProfileFragment();
                        break;
                    }
                    case R.id.action_menu:{
                        Toast.makeText(MainActivity.this, "Menu Selected", Toast.LENGTH_SHORT).show();
                        fragment = new MenuFragment();
                        break;
                    }
                }

                transaction = fragmentManager.beginTransaction();
                transaction.replace(R.id.activity_main, fragment).commit();
                return true;
            }
        });



    }
}
Run Code Online (Sandbox Code Playgroud)

和我的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.realtimegaming.androidnative.testproject.MainActivity">

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottomBar"
        android:layout_alignParentBottom="true"
        android:background="@color/brown"
        android:layout_gravity="bottom"
        android:gravity="bottom"
        android:layout_marginTop="?attr/actionBarSize"
        app:itemBackground="@color/colorPrimary"
        app:menu="@menu/bottom_navigation_main"
        app:itemIconTint="@color/white"
        android:fitsSystemWindows="true"
        android:animateLayoutChanges="false"
        android:splitMotionEvents="false"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

小智 74

好吧,我找到了一种方法,以防它帮助其他人.因此,默认情况下,当BottomNavigationView超过3个项目时,添加shiftingmode = true.

此时您无法通过现有API更改它,并且禁用shift模式的唯一方法是使用反射.

所以我们可以使用这个助手摆脱这个:

class BottomNavigationViewHelper {

    static void removeShiftMode(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);
                // set once again checked value, so view will be updated
                item.setChecked(item.getItemData().isChecked());
            }
        } catch (NoSuchFieldException e) {
            Log.e("ERROR NO SUCH FIELD", "Unable to get shift mode field");
        } catch (IllegalAccessException e) {
            Log.e("ERROR ILLEGAL ALG", "Unable to change value of shift mode");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后像这样使用它:

BottomNavigationView bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottomBar);
BottomNavigationViewHelper.removeShiftMode(bottomNavigationView);
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助有同样问题的人!

请记住,每次更改BottomNavigationView中的菜单项时,都需要执行此方法.

UPDATE

根据不同的stackoverflow问题,您还需要更新proguard配置文件(例如proguard-rules.pro),上面的代码使用反射,如果proguard混淆了mShiftingMode字段,则无法工作.

-keepclassmembers class android.support.design.internal.BottomNavigationMenuView { 
    boolean mShiftingMode; 
}
Run Code Online (Sandbox Code Playgroud)

  • 上帝,Android开发很疯狂...... (87认同)
  • 在支持库28.0.0-alpha3中,setShiftingMode`被重命名为`setShifting` (3认同)
  • 为什么不能只在xml中保留一个属性来实现这个目的 (3认同)
  • 这不适用于最新的设计支持库 (2认同)
  • 不幸的是,它不适用于 import ```com.google.android.material.bottomnavigation.BottomNavigationView;``` (2认同)