java - Bottom Sheet 展开时最小化带有动画的底部导航栏

标签 java android kotlin bottomnavigationview bottom-sheet

我有一个底部导航栏和其上方的 Bottom Sheet 。 Bottom Sheet 可以扩展以填充整个屏幕,我想添加一个动画,在 Bottom Sheet 扩展时隐藏底部导航栏。下面是我的布局的屏幕截图,展开的图像显示了它当前的样子,所需的图像显示了我想要的样子。我查看了 Bottom Sheet 文档,但找不到任何有用的内容。

折叠:/image/lrqjA.png

扩展:/image/rr2K8.png

所需:/image/P8a1W.png

下面是我的布局

<androidx.constraintlayout.widget.ConstraintLayout 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/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/navigation_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:backgroundTint="?attr/colorSurface"
        android:elevation="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:menu="@menu/bottom_nav_menu">
    </com.google.android.material.bottomnavigation.BottomNavigationView>

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:cameraZoom="13.8"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:context=".MainActivity" />

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/navigation_bar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:elevation="16dp" >

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

        <LinearLayout
            android:id="@+id/floating_action_buttons"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:clipToPadding="false"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="16dp"
            app:layout_behavior="jonathan.gpsapp.FloatingButtonBehavior">

            <com.google.android.material.floatingactionbutton.FloatingActionButton
                android:id="@+id/center_map"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:clickable="true"
                android:src="@drawable/ic_center"
                app:backgroundTint="?attr/colorSurface"
                app:fabSize="mini"
                app:tint="?attr/colorPrimary" />

            <com.google.android.material.floatingactionbutton.FloatingActionButton
                android:id="@+id/record"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:src="@drawable/ic_record"
                app:backgroundTint="?attr/colorPrimary"
                app:fabSize="auto"
                app:tint="#f2f2f2" />
        </LinearLayout>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</androidx.constraintlayout.widget.ConstraintLayout>```

最佳答案

您需要使用addBottomSheetCallback来监听BottomSheet的滑动位置,通过这个值您可以顺利隐藏BottomNavigation:

private int navigationHeight;

bottomSheet.addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
              
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {
               if (navigationHeight == 0)
                     navigationHeight = navigation.getHeight(); //the height of the navigationView
               float slideY = navigationHeight - navigationHeight * (1 - slideOffset);
               navigation.setTranslationY(slideY);//Translate the navigatinView
            }
        });

注意:为了获得良好的结果,您必须移动协调器布局上的底部导航

关于java - Bottom Sheet 展开时最小化带有动画的底部导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66754484/

相关文章:

java - 编写计算闰年的代码时遇到困难

java - 如何将 Vaadin 8 Grid 的高度设置为未定义?

java - 访问列表中的不同元素

Android 如何调整 EditText 内文本的大小

java - Android:以事务方式将大量文件移动到 SD 卡?

android - 如何设置 Kotlin 版本

android - Unresolved reference : kotlinx - Kotlin 1-0-0-rc-1036

java - 使用 Java 自动化 Web 操作

javascript - WebView 的 loadUrl ("javascript:...") 不工作

kotlin - 如何捕获传递给模拟函数的参数并返回它?