android - OnSwipe dragUp 不适用于 MotionLayout 中的子视图
问题描述
我想使用 MotionLayout 在我的活动中实现底部工作表。
运动场景:
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:motion="http://schemas.android.com/apk/res-auto">
<Transition
motion:constraintSetEnd="@+id/end"
motion:constraintSetStart="@id/start"
motion:duration="2000">
<OnSwipe
motion:dragDirection="dragUp"
motion:touchAnchorId="@id/bottomSheet"
motion:touchRegionId="@id/bottomSheet" />
</Transition>
<ConstraintSet android:id="@+id/start">
<Constraint
android:id="@+id/bottomSheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
motion:layout_constraintBottom_toBottomOf="parent"/>
</ConstraintSet>
<ConstraintSet android:id="@+id/end">
<Constraint
android:id="@+id/bottomSheet"
android:layout_width="match_parent"
android:layout_height="0dp"
motion:layout_constraintTop_toTopOf="parent"
motion:layout_constraintBottom_toBottomOf="parent"/>
</ConstraintSet>
</MotionScene>
现在,如果我像下面的代码那样实现我的 MotionLayout,我不能在按钮区域拖动:
<androidx.constraintlayout.motion.widget.MotionLayout
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:id="@+id/ml"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
app:layoutDescription="@xml/activity_main_scene"
tools:context=".MainActivity">
<com.google.android.material.card.MaterialCardView
android:id="@+id/bottomSheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="@android:color/black">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_red_dark"
android:padding="40dp">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="A button" />
</FrameLayout>
</com.google.android.material.card.MaterialCardView>
</androidx.constraintlayout.motion.widget.MotionLayout>
我可以MaterialCardView
用NestedScrollView
then 包装,因为它NestedScrollView
会正确处理onInterceptTouchEvent
,它会起作用:
<androidx.constraintlayout.motion.widget.MotionLayout
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:id="@+id/ml"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
app:layoutDescription="@xml/activity_main_scene"
tools:context=".MainActivity">
<androidx.core.widget.NestedScrollView
android:id="@+id/bottomSheet"
android:background="@android:color/holo_blue_dark"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="@android:color/black">
<!-- other views -->
</com.google.android.material.card.MaterialCardView>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.motion.widget.MotionLayout>
如果我像这样实现 MotionLayout,我将面临另一个问题。当MaterialCardView
用户向上拖动视图时,高度不会改变,即使我将高度设置为match_parent
:
如何以红色区域覆盖整个蓝色区域的方式更改代码?
解决方案
最后,我找到了解决方案。我只需要添加
android:fillViewport="true"
到 NestedScrollView 来解决这个问题。发生这种情况是因为 NestedScrollView 在最终状态下的高度大于其子项的高度,因此我需要设置fillViewport
以true
解决该问题。
推荐阅读
- python - Python DataFrame:根据条件从数据框列中获取计数?
- spring - 将我的 Spring Boot 从 2.0.5 更新到 2.1.3 后无法反序列化
- javascript - Firebase Firestore Web - 编写不同的值类型
- node.js - 错误:Heroku 上的自签名证书 Node 12 应用程序
- python - 检测 Pandas 系列中的异常值
- ml.net - ML.Net 图像分类错误:无效参数:输入必须是 4 维[224,224,3]
- java - Server Tomcat v9.0 Server at localhost 无法启动,因为 com.mongodb.client.MongoDatabase
- android - 在 Kotlin 中创建具有泛型类型的抽象类
- python - 如何使用带有 if 语句的列的第一行来修复 df.apply?
- python - 与浮点数相比,Pandas 可以为空的整数类型对性能不利吗