android - MotionLayout - OnSwipe 不适用于可点击的子项

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

我在使用 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>
以下是我尝试过但无法开始工作的所有事情:
  • 覆盖可点击 View 的 onInterceptTouchEvent 父级中的 ViewGroup (仅在此处返回 true 将导致我想要的滑动行为,但是应该不再是可点击的 subview 。)

  • override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        if (ev?.action == MotionEvent.ACTION_DOWN) {
            onTouchEvent(ev)
            return false
        }
        return true
    }
    
  • 在可点击的 subview 中覆盖 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 帖子(还有更多,但这些是最相关的):
  • 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

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

    最佳答案

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

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

  • 我希望能帮助遇到这个问题的人。

    关于android - MotionLayout - OnSwipe 不适用于可点击的子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64163016/

    相关文章:

    android - RecyclerView 为其项目设置了错误的 MotionLayout 状态

    java - 如何使用响应式 android 和 Retrofit 发出多个请求

    java - 如何查询默认的 SpeechRecognizer

    android - AnimationDrawable 不适用于 Android 2.2

    java - 按钮不适用于 AlexKolpa/fab-toolbar

    android - 资源$NotFoundException : Unable to find resource ID #0xffffffff using NestedScrollView with Motionlayout

    android - 如何在单个 View 上创建多个转换

    Android - ListView 列表选择器不使用默认值

    javascript - undefined is not a function (evaluating '_this2.closeDrawer()') react native app 错误

    java - 显示时对Listview的每一项进行动画处理