在Scroll上隐藏/显示bottomNavigationView

Kar*_*nga 42 android android-layout android-fragments bottomnavigationview

我必须在向上滚动时隐藏底部导航视图并在向下滚动时显示.如何实现这一点?我的布局是这样的

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_above="@+id/navigation"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="5dp">

        <FrameLayout
            android:id="@+id/container1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
          />


    </LinearLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="?android:attr/windowBackground"
        app:layout_scrollFlags="scroll|enterAlways|snap"
        app:menu="@menu/dashboard_slider_menu" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我附上了视图的截图.请检查一下.

在此输入图像描述

Abh*_*ngh 92

UPDATE

更新到最新的支持库版本28.0.0,只需添加一个属性即可higher version.

<BottomNavigationView
 ....
 ....
 app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"/>
Run Code Online (Sandbox Code Playgroud)

注意: - 您的XML应遵循旧答案中下面给出的XML结构.

旧答案(仍然有效)

您需要一个辅助类来执行此操作.此解决方案的工作方式与Google Material Design Guide相同.

创建一个类 BottomNavigationView

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

要使用此行为,您需要使用cooradinator布局...

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.kliff.digitaldwarka.activity.MainActivity">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinator_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/myAppBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:descendantFocusability="beforeDescendants"
            android:focusableInTouchMode="true"
            android:theme="@style/AppTheme.AppBarOverlay"
            app:elevation="0dp">

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

        <!---your RecyclerView/Fragment Container Layout-->
        <FrameLayout
             android:id="@+id/container"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             app:layout_behavior="@string/appbar_scrolling_view_behavior" />


         <android.support.design.widget.BottomNavigationView
             android:id="@+id/bottom_nav"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_gravity="bottom"
             app:itemBackground="@color/white"
             app:menu="@menu/bottom_nav_menu" />

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

      <!---NavigationView-->
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

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

mBottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav);
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) mBottomNavigationView.getLayoutParams();
    layoutParams.setBehavior(new BottomNavigationViewBehavior());
Run Code Online (Sandbox Code Playgroud)

  • Google就是这样的应用程序,他们指定了所有这些不错的Material View动画标准,但我找不到官方指导.Zero SDK支持这些指南,因此您必须自己制作并且只能在SO .. +上找到 (4认同)
  • @AbhishekSingh你如何处理这个案例,当回收者视图不滚动导致一切都适合屏幕,但最底层的单元格被底部导航视图覆盖.然后,底部导航将永远不会隐藏/消失,从而导致覆盖最后一行项目的问题. (2认同)
  • 我们如何解释显示在底部导航上方的小吃店? (2认同)
  • 经过大约半天的苦苦挣扎,我只需添加该行layout_behaviour就可以使它正常工作。问题是我正在尝试嵌套布局的不同组合,因此这是像我这样使用ConstraintLayout的用户的提示:诀窍是在约束布局中使用协调器布局。希望能帮助到你 :) (2认同)

小智 12

试试这个,

 mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                if (dy > 0 && bottom_navigation.isShown()) {
                    bottom_navigation.setVisibility(View.GONE);
                } else if (dy < 0 ) {
                    bottom_navigation.setVisibility(View.VISIBLE);

                }
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

                super.onScrollStateChanged(recyclerView, newState);
            }
        });
Run Code Online (Sandbox Code Playgroud)

向上滚动图像: -

点击这里滚动图像

向下滚动图像:

单击此处向下滚动图像


sun*_*rer 8

最新库更新后更新的答案:

BottomNavigationView现在只需在布局中使用一个标志即可隐藏滚动!从版本28.0.0-alpha1或材质/ androidX开始1.0.0-alpha1.

我使用后一种方法更新了我的项目,因为该版本现在是一个稳定的候选版本.更新:使用完全发布的版本"1.0.0"!

调用新的开箱即用行为HideBottomViewOnScrollBehavior.将其设置BottomNavigationViewapp:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"最新文档中所述.

这是一个完整的例子:

<com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:labelVisibilityMode="selected"
        app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
        android:layout_gravity="bottom"
        app:layout_insetEdge="bottom"
        app:menu="@menu/navigation" />
Run Code Online (Sandbox Code Playgroud)

由于与工具栏上滚动的隐藏,你必须确保内容是,支持最新的滚动类RecyclerViewNestedScrollView.

这可确保所有工作正如设计规格中的动画所示

PS:这labelVisibilityMode是另一个很酷的补充,你可以免费获得更新的麻烦,这在设计规范中有详细描述.

  • 如果在一个标签中我向上滚动条消失(如预期的那样),当按下返回并跳转到另一个标签屏幕时 - 标签仍然隐藏,如何显示它? (2认同)

Lef*_*fty 5

  1. 将项目更新为Androidx,即重构>>迁移至androidx(最低Android Studio版本3.4)
  2. 使用默认的底部导航菜单xml文件,将父约束布局替换为协调器布局
  3. 添加以下行app:layout_behavior =“ com.google.android.material.behavior.HideBottomViewOnScrollBehavior”

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".dashboards.Admin_dashboard_main">

    <include layout="@layout/toolbar" />
    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/main_area"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_margin="0dp"
        android:padding="0dp">

        <!-- Fragments Container -->
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            tools:context="MainActivity"
            tools:showIn="@layout/activity_tenant_dashboard"
            android:id="@+id/fragment_container">

        </FrameLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>
    <!-- Bottom Navigation View -->

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        android:layout_gravity="bottom"
        app:menu="@menu/menu_admin_dashboard_main"
        app:layout_behavior="com.google.android.material.behavior.HideBottomViewOnScrollBehavior"
        />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)