【问题标题】:MotionLayout - OnSwipe not working on clickable childrenMotionLayout - OnSwipe 不适用于可点击的子项
【发布时间】: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>

以下是我尝试过但无法开始工作的所有事情:

  1. 在可点击视图的 ViewGroup 父级中覆盖 onInterceptTouchEvent(仅在此处返回 true 将导致我想要的滑动行为,但是应该可点击的子视图不再是。)
override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
    if (ev?.action == MotionEvent.ACTION_DOWN) {
        onTouchEvent(ev)
        return false
    }
    return true
}
  1. 在可点击的子视图中覆盖onTouchEvent
override fun onTouchEvent(event: MotionEvent?): Boolean {
    if (event?.action == MotionEvent.ACTION_DOWN) {
        super.onTouchEvent(event)
        return false
    }
    return super.onTouchEvent(event)
}
  1. MotionLayout 中覆盖onInterceptTouchEvent
override fun onInterceptTouchEvent(event: MotionEvent?): Boolean {
    if (event?.action == MotionEvent.ACTION_MOVE) {
        return super.onInterceptTouchEvent(event)
    }
    return false
}

我查看了所有这些 StackOverflow 帖子(还有更多,但这些是最相关的):

关于我可以在这里做些什么来滑动和点击项目有什么想法吗?

【问题讨论】:

    标签: android android-animation android-motionlayout android-touch-event android-swipe


    【解决方案1】:

    我让它工作了。这里有两件事有所帮助:

    1. 我将 GridView 切换为带有 GridLayoutManager 的 RecyclerView,并将 app:touchAnchorId 设置为 RecyclerView 的 id。
    2. 实际上我在 MotionScene 文件中有两个过渡,另一个过渡(不是 OnSwipe)是第一个。 MotionScene 文件中的顺序很重要。我切换了两个过渡的顺序,然后我让它工作了。但是,一旦触发了另一个转换,它就会停止工作,所以我不得不在适当的时候使用setTransition 将转换重置回 OnSwipe 转换。

    希望对遇到这个问题的人有所帮助。

    【讨论】:

      猜你喜欢
      • 2020-06-15
      • 1970-01-01
      • 2019-09-07
      • 2022-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-30
      相关资源
      最近更新 更多