Skip to content

Instantly share code, notes, and snippets.

@rsuper
Last active June 27, 2022 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsuper/063af6442e144e30eae0191c9c50aea5 to your computer and use it in GitHub Desktop.
Save rsuper/063af6442e144e30eae0191c9c50aea5 to your computer and use it in GitHub Desktop.
Share click and touch events over multiple views for Android
import android.view.View
object ViewTouchSharing {
fun shareTouchEvents(vararg views: View, clickListener: (() -> Unit)? = null) {
views.firstOrNull()?.let { view ->
view.setOnClickListener {
clickListener?.invoke()
}
}
views.forEach { view ->
view.setOnTouchListener { _, motionEvent ->
views
.map { delegatedView ->
delegatedView.onTouchEvent(motionEvent)
}
.any { it }
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment