有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用导航组件navhostfragment更改全屏/上方底部导航

我使用的是带有导航组件的单活动多片段。如何隐藏一些片段的底部导航栏

我尝试了以下方法:

  1. 通过数据绑定控制底部导航栏的可见性。(童车)
  2. 在打开碎片之前和在后背上切换底部导航可见性(小车)
  3. 制作2个主机片段:1个全屏,1个底部导航绑定
  4. 制作2张导航图

主要活动。xml:

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

主要活动。爪哇:

    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);
                }
            }
        });

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


共 (2) 个答案

  1. # 1 楼答案

    我使用了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" />
    

    致意 希望你觉得有用

  2. # 2 楼答案

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