I have ImageViews in a MotionLayout to animate moving the ImageView across the screen. I want to also add a ClickListener to the ImageView so that there can be functionality when the ImageView is simply clicked/tapped instead of moved.
I followed the advice here to write a custom MotionLayout which only handles ACTION_MOVE
but after adding the ClickListener to the ImageView in onCreate() only the ClickListener code would run and the MotionLayout would not respond. I've also tried overriding onTouchEvent() in a custom ImageView but that hasn't worked.
Inside Custom 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>
Inside Custom 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)
}
I've also tried in activity onCreate()
binding.topCard.setOnClickListener {
Toast.makeText(applicationContext, "Works", Toast.LENGTH_LONG)
.show()
}
Thanks for your attention!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…