android - 为什么 MotionLayout 的折叠动画不能通过 NestedScrollView 自动完成?

标签 android android-constraintlayout android-coordinatorlayout android-nestedscrollview android-motionlayout

我已经使用 MotionLayout 而不是 Coordinator 创建了自定义折叠 View ,除了动画的自动完成或在协调器中我们可以称之为捕捉之外一切正常,就像当我们滚动和停止滚动时,折叠 View 将自动捕捉顶部或底部.但是如果我在 MotionLayout 中使用 NestedScrollView 就不会发生这种情况。我已经创建了示例来演示这个。我还添加了 app:onTouchUp="autoComplete" 但它不起作用。

motion_layout.xml

<androidx.constraintlayout.motion.widget.MotionLayout 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"
app:layoutDescription="@xml/motion_scene">

<View
    android:id="@+id/collapsible"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/red" />

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <View
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:background="#EEE" />

    ///.......///

    </LinearLayout>

</androidx.core.widget.NestedScrollView>

motion_scene.xml

<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <Transition
        app:constraintSetEnd="@id/end"
        app:constraintSetStart="@id/start"
        app:duration="1000">

        <OnSwipe
            app:dragDirection="dragUp"
            app:onTouchUp="autoComplete"
            app:touchAnchorId="@id/nested_scroll_view"
            app:touchAnchorSide="top" />

    </Transition>

    <ConstraintSet android:id="@+id/start">

        <Constraint
            android:id="@+id/collapsible"
            android:layout_width="0dp"
            android:layout_height="300dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Constraint
            android:id="@id/nested_scroll_view"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/collapsible" />

    </ConstraintSet>

    <ConstraintSet
        android:id="@+id/end"
        app:deriveConstraintsFrom="@id/start">

        <Constraint
            android:id="@+id/collapsible"
            android:layout_width="0dp"
            android:layout_height="100dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Constraint
            android:id="@id/nested_scroll_view"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/collapsible" />

    </ConstraintSet>

</MotionScene>

最佳答案

只需添加到 NestedScrollView > LinearLayout

android:descendantFocusability="blocksDescendants"

关于android - 为什么 MotionLayout 的折叠动画不能通过 NestedScrollView 自动完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58231419/

相关文章:

android - Flutter - SQFlite - 类型 'String' 不是类型 'double' 的子类型

android - 约束布局高度占用可用空间

android - 协调器布局和运动布局有什么区别?

android - 带有 Snackbar 和透明导航栏的 FloatingActionButton

java - 显示一个又一个对话框,直到许可证检查通过

android - 动画搜索栏进度

android - 当键盘出现在 ConstraintLayout 中时如何向上移动 View

java - 如何在 ConstraintLayout 类中使用 android 扩展 XML 布局?

android - 防止 CollapsingToolbar 在 Lollipop 的状态栏下继续

安卓横向。模拟器没 react