【问题标题】:Why MotionLayout collapsing animation not auto completed with NestedScrollView?为什么 MotionLayout 折叠动画不使用 NestedScrollView 自动完成?
【发布时间】:2019-10-04 07:01:29
【问题描述】:

我已经使用 MotionLayout 而不是 Coordinator 创建了自定义折叠视图,除了动画的自动完成或在协调器中,我们可以将其称为捕捉,例如当我们滚动并停止滚动时,折叠视图将自动捕捉顶部或底部.但如果我在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>

【问题讨论】:

  • 嗨!你找到解决办法了吗?
  • @OlegNestyuk 不,仍然没有找到任何东西。

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


【解决方案1】:

只需添加到 NestedScrollView > LinearLayout

android:descendantFocusability="blocksDescendants"

【讨论】:

  • 这应该是公认的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-29
  • 1970-01-01
  • 2015-09-16
  • 2020-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多