【问题标题】:How to hide bottom nav bar in fragment如何在片段中隐藏底部导航栏
【发布时间】:2022-04-26 02:12:03
【问题描述】:

我在主要活动中定义了底部导航栏。我在片段中有三个与底部导航栏链接的片段我有回收站视图所以我想在 RecyclerView 向下滚动时隐藏底部导航栏并在 RecyclerView 向上滚动时显示。 我的问题是如何访问片段中的底部导航栏,因为它是在 MainActivity 中定义的。

这是我的代码:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay"
    app:elevation="0dp"
    android:background="@color/colorPrimary"
    android:paddingBottom="7dp"
    android:fitsSystemWindows="true">


    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:layout_scrollFlags="scroll|enterAlways|snap">

        <Spinner
            android:layout_width="110dp"
            android:layout_height="50dp"
            android:id="@+id/chooseLocation"
            app:backgroundTint="@android:color/white"/>

    </android.support.v7.widget.Toolbar>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:id="@+id/search"
        android:paddingTop="6dp"
        android:paddingBottom="6dp"
        android:paddingRight="6dp"
        android:paddingLeft="12dp"
        android:hint="Search here"
        android:textColorHint="#9e9e9e"
        android:textColor="#000"
        tools:ignore="HardcodedText"
        android:background="@drawable/search_edit_text"
        android:paddingEnd="6dp"
        android:paddingStart="12dp"/>

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.design.widget.BottomNavigationView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/bottomBar"
    android:layout_gravity="bottom"
    app:menu="@menu/bottom_menu"
    android:background="#fff"
    app:itemIconTint="@drawable/nav_check"
    app:itemTextColor="@drawable/nav_check"/>

</android.support.design.widget.CoordinatorLayout>

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Tab1Fragment"
android:background="#fff">

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/purchasedBook"/>


</RelativeLayout>

这就是我的片段的定义方式,因为任何片段中都没有底部导航栏,所以我如何访问片段中的底部导航栏。

有人,请让我知道任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: android android-fragments


    【解决方案1】:

    要从片段中访问您的 BottomNavigationView,请使用以下代码:

    BottomNavigationView navBar = getActivity().findViewById(R.id.bottomBar);
    

    【讨论】:

    • 谢谢兄弟,效果很好,但是底部导航栏立即隐藏了如何才能顺利隐藏它。
    • 您是否按照@Brahma Datta 的建议实施了BottomNavigationViewBehavior
    • 您的两个解决方案都运行良好,但要访问我的片段中的底部导航栏,我正在使用您的解决方案,因为它非常简短。
    • Fragment 与 Android 紧密耦合
    • 您可以在某个片段内使其不可见,但当您单击返回时,它仍然不可见。怎么解决?
    【解决方案2】:
    navController.addOnDestinationChangedListener { _, destination, _ ->
       if(destination.id == R.id.full_screen_destination) {
           
           bottomNavigationView.visibility = View.GONE
       } else {
           
           bottomNavigationView.visibility = View.VISIBLE
       }
    }
    

    在主要活动中执行此操作。 这里R.id.full_screen_destination是导航片段中片段的id。

    【讨论】:

      【解决方案3】:

      由于fragment总是在一个activity中,你可以在fragment中调用getActivity()来访问activity中已经存在的对象。所以你可以这样做:

      活动

      public class MainActivity extends Activity {
      //...
         Toolbar toolbar;
      //...
         public Toolbar getNav() {
            return toolbar;
         }
      //...
      }
      

      片段

      //...
      if(getActivity() != null && getActivity instanceOf MainActivity)
          ((MainActivity)getActivity()).getNav().setVisiblity(View.GONE);
      //...
      

      【讨论】:

        【解决方案4】:

        试试这个,

        在Xml的BottomNavigationView中添加这一行

        app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"

        并使用 CoOrdinator Layout 实现此 BottomNavigation 行为,您可以使用滚动侦听器隐藏或显示视图。

        public class BottomNavigationViewBehavior extends CoordinatorLayout.Behavior<BottomNavigationView> {
        
        private int height;
        
        @Override
        public boolean onLayoutChild(CoordinatorLayout parent, BottomNavigationView child, int layoutDirection) {
            height = child.getHeight();
            return super.onLayoutChild(parent, child, layoutDirection);
        }
        
        @Override
        public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout,
                                       BottomNavigationView child, @NonNull 
                                       View directTargetChild, @NonNull View target,
                                       int axes, int type)
        {
            return axes == ViewCompat.SCROLL_AXIS_VERTICAL;
        }
        
        @Override
        public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull BottomNavigationView child,
                   @NonNull View target, int dxConsumed, int dyConsumed,
                   int dxUnconsumed, int dyUnconsumed, 
                    @ViewCompat.NestedScrollType int type)
        {
           if (dyConsumed > 0) {
               slideDown(child);
           } else if (dyConsumed < 0) {
               slideUp(child);
           }
        }
        
        private void slideUp(BottomNavigationView child) {
            child.clearAnimation();
            child.animate().translationY(0).setDuration(200);
        }
        
        private void slideDown(BottomNavigationView child) {
            child.clearAnimation();
            child.animate().translationY(height).setDuration(200);
        }
        

        }

        将此行代码添加到包含底部导航的 Activity 中

        bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav);
        CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) 
        bottomNavigationView .getLayoutParams();
        layoutParams.setBehavior(new BottomNavigationViewBehavior());
        

        试试这个,让我知道 Digvijay.Happy Coding。

        【讨论】:

        • ,如何在我的片段中访问这个底部导航栏
        • 你不是在MainActivity的底部导航栏实现了Activity被Fragments共享的地方吗? @Digvijay
        • 嘿,如果它有效,那么请接受我的回答并投票。随着它接触到更多的人@Digvijay
        • 底部导航栏立即隐藏如何才能使其顺利隐藏。
        • 对这些听众使用延迟概念。它可能会解决这个问题@Digvijay
        【解决方案5】:

        Fragment 有 onAttach() 方法,可以为您提供上下文。所以你必须使用创建活动实例,

        MainActivity mainActivity;
        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            mainActivity = (MainActivity)context;
        }
        

        现在使用 boolean 参数创建方法,隐藏和显示底部栏。

        public void visibilityOfBottom(boolean isScroll){
          if(isScroll){
        
          // hide bottom bar
        
          } else{
           // show bottom bar
          }
        
        }
        

        现在使用MainActivity上下文访问片段中的上述方法,

        mainActivity.visibilityOfBottom(false);
        

        【讨论】:

          【解决方案6】:

          科特林

          从片段访问导航视图

          val navBar: BottomNavigationView = activity!!.findViewById(R.id.bottomBar)
          

          【讨论】:

            【解决方案7】:

            我发现在 V6 中隐藏底部导航器的唯一方法 正在制作这样的结构 -

            Stack Nav {
               Bottom Nav
               Screen 1
               Screen 2
               Screen 3
            }
            

            【讨论】:

              【解决方案8】:

              添加

              app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
              

              底部导航视图

              app:layout_behavior="@string/appbar_scrolling_view_behavior"
              

              回收站视图

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2020-10-08
                • 2019-01-13
                相关资源
                最近更新 更多