【发布时间】:2021-01-07 06:19:54
【问题描述】:
我在 MotionLayout 中有 ImageView,用于动画在屏幕上移动 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, "Works", Toast.LENGTH_LONG)
.show()
}
感谢您的关注!
【问题讨论】:
标签: android xml kotlin animation clicklistener