如何在更改完整 screen/above 底部导航时使用导航组件 navhostfragment

how to use Navigation component navhostfragment while altering full screen/above bottom Navigation

我正在使用带导航的单个 activity 多片段 component.how 我是否隐藏某些片段的底部导航栏?

我尝试了以下方法:

  1. 通过数据绑定控制底部导航栏的可见性。(错误)
  2. 在打开片段之前和在后台(buggy)上切换 bottomnavigation 可见性
  3. 制作2个宿主片段:1个全屏,1个底部导航绑定
  4. 制作 2 个导航图 ..

activity_main.xml:

<com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigation"
            android:visibility="@{viewModel.uiUtils.shouldShow ? View.VISIBLE:View.GONE}"/>

mainactivity.java:

    private void observeShouldShow() {
        mainViewModel.uiUtils.getShouldShow().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                ViewGroup.LayoutParams layoutParams = binding.bottomNavigation.getLayoutParams();
                if (mainViewModel.getUiUtils().getShouldShow().getValue()) {
                    binding.bottomNavigation.setVisibility(View.VISIBLE);

                    layoutParams.height = 170;
                    binding.bottomNavigation.setLayoutParams(layoutParams);
                } else {
                    layoutParams.height = 0;
                    binding.bottomNavigation.setLayoutParams(layoutParams);

                 binding.bottomNavigation.setVisibility(View.INVISIBLE);
                }
            }
        });

在全屏片段和普通片段之间切换时,底部导航栏会闪烁

官方文档推荐使用OnDestinationChangedListener来处理。 Look here.

我使用了 OnDestinationChangedListener,就像@Lavepe 回答的那样...抱歉很长时间没有检查这里是我的代码:

                if (destinationLabel.equals("FragmentX")) {
                    showBottomNav(true);
                    badgeBehaviour(false, false);}

ui函数:

private void showBottomNav(boolean b) {
    binding.bottomNavigation.setVisibility(b ? View.VISIBLE : View.GONE);
}

上面的视图是:

    <fragment
        android:id="@+id/navHostFragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/viewcartbadge"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_graph" />

最好的问候 希望你觉得有用