Skip to content

Instantly share code, notes, and snippets.

@BhavyaRattan
Created January 21, 2021 12:13
Show Gist options
  • Save BhavyaRattan/674e179d5e917816449272fbf1463f17 to your computer and use it in GitHub Desktop.
Save BhavyaRattan/674e179d5e917816449272fbf1463f17 to your computer and use it in GitHub Desktop.
Magic Touch Recycler
class ScaleItemOnTouchListener : RecyclerView.OnItemTouchListener {
.......
override fun onTouchEvent(rv: RecyclerView, event: MotionEvent) {
val childView = rv.findChildViewUnder(event.x, event.y)
val previousChild = rv.findChildViewUnder(previousX, previousY)
if (childView != null) {
when (event.action) {
MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -> {
if (childView.scaleX != SCALE_UP) {
scaleUp(childView)
}
if (previousChild != null && previousChild != childView) {
scaleDown(previousChild)
}
}
MotionEvent.ACTION_UP, MotionEvent.ACTION_POINTER_UP -> {
scaleDown(childView)
childView.callOnClick()
}
}
previousX = childView.x
previousY = childView.y
} else if (previousChild != null && (event.action == MotionEvent.ACTION_UP
|| event.action == MotionEvent.ACTION_CANCEL)) {
scaleDown(previousChild)
previousX = 0f
previousY = 0f
}
}
.......
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment