【发布时间】:2020-10-01 20:51:45
【问题描述】:
我在使用MotionLayout 时遇到了一个问题,如果用户在可点击项目上开始滑动,他们将无法向上/向下滑动。我希望用户能够在屏幕的任何部分上滑动,并触发滑动过渡。
基本上,该行为将与目前在 Android 版 Yelp 应用上的行为非常相似。在屏幕上的任意位置向上滑动会将搜索栏移动到屏幕顶部。请参阅此video 的滑动行为。
我的过渡代码如下所示:
<Transition
app:constraintSetStart="@id/start"
app:constraintSetEnd="@id/end"
>
<OnSwipe
app:dragDirection="dragUp"
app:onTouchUp="decelerate"
/>
</Transition>
MotionLayout 的代码如下所示:
<androidx.constraintlayout.motion.widget.MotionLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutDescription="@xml/motion_scene">
>
<!-- other views -->
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title"
android:textColor="@color/black"
android:textSize="14sp"
android:lineSpacingExtra="7sp"
android:fontFamily="@font/avenir"
android:layout_marginBottom="16dp"
/>
<GridView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="2"
android:verticalSpacing="8dp"
android:horizontalSpacing="12dp"
android:stretchMode="columnWidth"
tools:listitem="@layout/griditem" <!-- This layout file contains a single <Button/> -->
/>
</LinearLayout>
</androidx.constraintlayout.motion.widget.MotionLayout>
以下是我尝试过但无法开始工作的所有事情:
- 在可点击视图的
ViewGroup父级中覆盖onInterceptTouchEvent(仅在此处返回 true 将导致我想要的滑动行为,但是应该可点击的子视图不再是。)
override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
if (ev?.action == MotionEvent.ACTION_DOWN) {
onTouchEvent(ev)
return false
}
return true
}
- 在可点击的子视图中覆盖
onTouchEvent
override fun onTouchEvent(event: MotionEvent?): Boolean {
if (event?.action == MotionEvent.ACTION_DOWN) {
super.onTouchEvent(event)
return false
}
return super.onTouchEvent(event)
}
- 在
MotionLayout中覆盖onInterceptTouchEvent
override fun onInterceptTouchEvent(event: MotionEvent?): Boolean {
if (event?.action == MotionEvent.ACTION_MOVE) {
return super.onInterceptTouchEvent(event)
}
return false
}
我查看了所有这些 StackOverflow 帖子(还有更多,但这些是最相关的):
- MotionLayout problems with children intercepting touch events
- Can we use OnSwipe and OnClick in the same transition for Android MotionLayout?
- How to handle OnClick and OnClickListener in MotionLayout similar to YouTube
- MotionLayout OnSwipe and OnClick on same view
- MotionLayout prevents click listener on all views
- MotionLayout - How to limit the OnSwipe event on a specific view instead of on whole screen
关于我可以在这里做些什么来滑动和点击项目有什么想法吗?
【问题讨论】:
标签: android android-animation android-motionlayout android-touch-event android-swipe