我已经使用 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/