Android BottomSheetBehavior setPeekHeight(int peekHeight, boolean animate) 不会对高度过渡进行动画处理

标签 android android-layout animation kotlin bottom-sheet

我的 BottomSheetBehavior 布局如下所示:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical"
    app:behavior_hideable="false"
    app:behavior_peekHeight="76dp"
    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

    <LinearLayout
        android:id="@+id/bottomSheetPeekLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/bottomSheetEditText"
            android:layout_width="match_parent"
            android:layout_height="44dp"
            android:layout_margin="16dp"/>

        <include
            android:id="@+id/bottomSheetDynamicHeightLayout"
            layout="@layout/layout_bottom_sheet_dynamic_height"
            android:visibility="gone" />
    </LinearLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/bottomSheetRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

嵌入到 Activity 的 CoordinatorLayout 内:

<LinearLayout
    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"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:background="@color/white">

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/closeIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="12dp"
            android:layout_marginTop="4dp"
            android:padding="12dp"
            android:src="@drawable/ic_close_point_chooser" />

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:textAllCaps="true"
            android:textColor="#3f454e"
            android:textSize="14sp" />

    </FrameLayout>

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.fragment.app.FragmentContainerView
            android:id="@+id/mapFragmentContainerView"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:focusableInTouchMode="true" />

        <include
            android:id="@+id/bottomSheetLayout"
            layout="@layout/bottom_sheet_layout"
            android:paddingTop="44dp" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</LinearLayout>

BottomSheet 的 Peek 高度应该根据 bottomSheetDynamicHeightLayout 的高度进行更改,bottomSheetDynamicHeightLayout 是显示某些数据的约束布局。我观察到此布局的高度变化,并以这种方式更新 BottomSheetBehavior:

bottomSheetPeekLayout.addOnLayoutChangeListener { v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom ->
            bottomSheetBehavior.setPeekHeight(bottomSheetPeekLayout.height, true)
}

因此,当我的 View 高度发生变化时,BottomSheet 窥视高度会使用 setPeekHeight(int peekHeight, boolean animate) 进行更改method,根据文档,应该动画高度变化。

然而,这不会发生 - 窥视高度已更新, Bottom Sheet 更改了窥视尺寸,但没有动画 - 它只是改变了屏幕尺寸。当我之前使用此方法手动更新高度(不在 onLayoutChangedListener 中)时,动画效果很好。

我有什么遗漏的吗?这是某种错误,还是期望的行为? animate 参数不应该始终为过渡设置动画吗?怎样才能达到预期的效果?

最佳答案

我遇到了类似的问题,并且我不确定使用以下方法的直接解决方案:

setPeekHeight(int peekHeight, boolean animate)

但是,您可以使用转换管理器添加转换,如下所示:

TransitionManager.beginDelayedTransition(bottomSheet);
bottomSheetBehavior.setPeekHeight(20);

如果您想更改翻译的持续时间,请使用以下命令:

ChangeBounds transition = new ChangeBounds();
transition.setDuration(200L);
TransitionManager.beginDelayedTransition(bottomSheet, transition);
bottomSheetBehavior.setPeekHeight(20);

基于以下答案:

  1. animateLayoutChanges="true" in BottomSheetView showing unexpected behaviour

  2. Is it possible to set transition speed/time in transition everywhere on android

关于Android BottomSheetBehavior setPeekHeight(int peekHeight, boolean animate) 不会对高度过渡进行动画处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60779005/

相关文章:

android - 在Android中实现类似扑克牌的控件布局(附原型(prototype)图)

css - 移动按钮推送内容

java - 我应该升级JAVA版本Mac OS X来进行android开发吗

java - 创建具有大量选项的位掩码

android - 如何从 Android 设备上的后台服务使屏幕闪烁/闪烁?

android - 想在单个 TextView 中有多种文本显示样式

javascript - 向 svg 中的随机元素添加和删除类

iphone - 在启动动画之前修改 iPhone 动画容器 View

android - 在android中合并适配器

java - 如何使用 Intellij 将 Android Gradle 项目从 Windows 导入 Linux,而不会出现 keystore 问题