【问题标题】:how to use Navigation component navhostfragment while altering full screen/above bottom Navigation如何在更改全屏/上方导航时使用导航组件 navhostfragment
【发布时间】:2019-09-17 07:03:15
【问题描述】:

我正在使用带有导航组件的单个活动多片段。如何隐藏某些片段的底部导航栏?

我尝试了以下方法:

  1. 通过数据绑定控制底部导航栏的可见性。(buggy)
  2. 在打开片段之前切换底部导航可见性和在后台(越野车)
  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);
                }
            }
        });

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

【问题讨论】:

  • 您好,请问您是如何管理底部导航栏的可见性的?

标签: java android android-architecture-components


【解决方案1】:

官方文档建议使用OnDestinationChangedListener 来处理这个问题。 Look here.

【讨论】:

    【解决方案2】:

    我使用了 OnDestinationChangedListener,正如@Lavepe 所回答的那样......对不起,很长时间没有在这里检查这是我的代码:

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

    用户界面功能:

    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" />
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-02
      • 1970-01-01
      • 2021-12-01
      • 2021-07-21
      • 1970-01-01
      相关资源
      最近更新 更多