MotionLayout 视图中添加的 ClickListener 阻止布局响应事件

问题描述

我在 MotionLayout 中有 ImageViews 来动画在屏幕上移动 ImageView。我还想向 ImageView 添加一个 ClickListener,以便在 ImageView 被简单地单击/点击而不是移动时可以使用功能

我按照建议 here 编写了一个仅处理 ACTION_MOVE自定义 MotionLayout,但是在 onCreate() 中将 ClickListener 添加到 ImageView 后,只有 ClickListener 代码会运行,而 MotionLayout 不会响应。我也试过在自定义 ImageView 中覆盖 onTouchEvent() 但没有奏效。

自定义 MotionLayout 内部

       <FrameLayout
            android:id="@+id/bottomCard"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TapImageView
                android:id="@+id/bottom"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitXY" />
        </FrameLayout>

        <FrameLayout
            android:id="@+id/topCard"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TapImageView
                android:id="@+id/top"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitXY" />
        </FrameLayout>

内部自定义 ImageView

override fun onTouchEvent(event: MotionEvent?): Boolean {
        when (event?.action) {
            MotionEvent.ACTION_UP -> {
                super.onTouchEvent(event)
                Toast.makeText(context,"Works",Toast.LENGTH_LONG)
                    .show()
                return false
            }

        }
        return super.onTouchEvent(event)
    }

我也尝试过 onCreate() 活动

  binding.topCard.setonClickListener {
            Toast.makeText(applicationContext,Toast.LENGTH_LONG)
                .show()
        }

感谢您的关注!

解决方法

如果您有自己的 Click 侦听器,则需要从中启动转换。 如果按钮不仅仅是动画。您可能想要对动画做出一些决定。 从 motionScene 中删除 并添加 motionLayout.transitionToState(R.id.mystate);到您的点击处理程序