我在使用 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>
以下是我尝试过但无法开始工作的所有事情:onInterceptTouchEvent
父级中的 ViewGroup
(仅在此处返回 true 将导致我想要的滑动行为,但是应该不再是可点击的 subview 。)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)
}
onInterceptTouchEvent
中的 MotionLayout
override fun onInterceptTouchEvent(event: MotionEvent?): Boolean {
if (event?.action == MotionEvent.ACTION_MOVE) {
return super.onInterceptTouchEvent(event)
}
return false
}
我查看了所有这些 StackOverflow 帖子(还有更多,但这些是最相关的):关于我可以在这里做些什么来滑动和点击项目有什么想法吗?
最佳答案
我让它工作了。这里有两件事有帮助:
app:touchAnchorId
到 RecyclerView 的 id。 setTransition
在适当的时候将转换重置回 OnSwipe 转换。 我希望能帮助遇到这个问题的人。
关于android - MotionLayout - OnSwipe 不适用于可点击的子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64163016/