首页 > 解决方案 > MotionLayout keyTrigger 不能正常工作?

问题描述

我有 MotionLayout xml 文件

<androidx.constraintlayout.motion.widget.MotionLayout
    android:id="@+id/motionLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#1e376b"
    app:layoutDescription="@xml/motion_layout_header_scene">

    <ImageView
        android:id="@+id/background"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:scaleType="centerCrop"
        app:srcCompat="@drawable/icon_schiller"
        tools:ignore="MissingConstraints" />


</androidx.constraintlayout.motion.widget.MotionLayout>

<androidx.core.widget.NestedScrollView
    android:id="@+id/scrollable"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text=“@string/long_text />
</androidx.core.widget.NestedScrollView>

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:clickable="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:srcCompat="@android:drawable/ic_input_add" />

这是我的场景文件

<Transition
    motion:constraintSetEnd="@+id/end"
    motion:constraintSetStart="@+id/start"
    motion:duration="250"
    motion:motionInterpolator="linear">

    <OnSwipe
        motion:dragDirection="dragUp"
        motion:touchAnchorId="@+id/motionLayout"
        motion:touchAnchorSide="bottom" />

    <KeyFrameSet>

        <KeyTrigger
            motion:framePosition="20"
            motion:motionTarget="@id/fab"
            motion:onPositiveCross="hide" />

        <KeyTrigger
            motion:framePosition="40"
            motion:motionTarget="@id/fab"
            motion:onNegativeCross="show" />
    </KeyFrameSet>
</Transition>

<ConstraintSet android:id="@+id/start">
    <Constraint
        android:id="@id/motionLayout"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        motion:layout_constraintTop_toTopOf="parent" />

    <Constraint
        android:id="@id/scrollable"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        motion:layout_constraintBottom_toBottomOf="parent"
        motion:layout_constraintTop_toBottomOf="@+id/motionLayout" />
</ConstraintSet>

<ConstraintSet android:id="@+id/end">
    <Constraint
        android:id="@id/motionLayout"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        motion:layout_constraintTop_toTopOf="parent"
        motion:progress="1" />

    <Constraint
        android:id="@id/scrollable"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        motion:layout_constraintBottom_toBottomOf="parent"
        motion:layout_constraintTop_toBottomOf="@+id/motionLayout" />
</ConstraintSet>

但是您可以在视频中看到 fab 存在问题,向上滚动时它会消失,然后当我向下滚动时它不会再次出现。 https://drive.google.com/file/d/1-V5EAdKd0EDfYOKE0ElgfEOSOoUUZrCK/view?usp=sharing

标签: androidandroid-motionlayout

解决方案


谷歌问题跟踪器中目前存在一个错误:https ://issuetracker.google.com/issues/135938944 。


推荐阅读