Advertisement
Kostiggig

CustomTouchViewGroup

Jul 19th, 2022
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.47 KB | None | 0 0
  1. abstract class ConstraintTouchEvent(
  2.     context: Context,
  3.     attrs: AttributeSet? = null,
  4.     defStyleAttr: Int = 0
  5. )  : ConstraintLayout(context, attrs, defStyleAttr) {
  6.  
  7.  
  8.     abstract fun onClick() : () -> Unit
  9.     abstract fun onLongClick() : () -> Unit
  10.  
  11.     private var timeDownAction: Long = DEFAULT_LONG
  12.     private var timeUpAction: Long = DEFAULT_LONG
  13.     private var timeDown = DEFAULT_LONG
  14.  
  15.     private var isClick = false
  16.     private var isLongClick = false
  17.  
  18.     private var startDx = DEFAULT
  19.     private var endDx = DEFAULT
  20.  
  21.     private var isNotSwiped = true
  22.  
  23.     override fun onTouchEvent(event: MotionEvent?): Boolean {
  24.         val eventAction = event?.action
  25.         isClick = false
  26.         isLongClick = false
  27.         timeDown = DEFAULT_LONG
  28.  
  29.         when (eventAction) {
  30.             MotionEvent.ACTION_UP -> {
  31.                 timeUpAction = System.currentTimeMillis()
  32.  
  33.                 val difference = abs(startDx - endDx)
  34.  
  35.                 if(difference <= INFELICITY_SWIPE) {
  36.                     startDx = DEFAULT
  37.                     endDx = DEFAULT
  38.                     isNotSwiped = true
  39.                 }
  40.  
  41.                 val timeFocusing = timeUpAction - timeDownAction
  42.  
  43.                 if (timeFocusing >= LONG_CLICK_TIME) {
  44.                     isLongClick = true
  45.                 } else {
  46.                     isClick = true
  47.                 }
  48.             }
  49.             MotionEvent.ACTION_DOWN -> {
  50.                 isNotSwiped = true
  51.                 timeDownAction = System.currentTimeMillis()
  52.             }
  53.  
  54.             MotionEvent.ACTION_MOVE -> {
  55.                 if(startDx == DEFAULT) {
  56.                     startDx = event.rawX
  57.                 } else {
  58.                     endDx = max(endDx,event.rawX)
  59.                 }
  60.                 isNotSwiped = false
  61.             }
  62.         }
  63.  
  64.         if (isNotSwiped) {
  65.             if (startDx != DEFAULT && endDx != DEFAULT) {
  66.                 startDx = DEFAULT
  67.                 endDx = DEFAULT
  68.             } else {
  69.                 if (isLongClick) {
  70.                     onLongClick().invoke()
  71.                 }
  72.                 if (isClick) {
  73.                     onClick().invoke()
  74.                 }
  75.             }
  76.         }
  77.  
  78.         return true
  79.     }
  80.  
  81.     private companion object {
  82.         private const val LONG_CLICK_TIME = 600L
  83.  
  84.         private const val DEFAULT = -1.0f
  85.         private const val DEFAULT_LONG = -1L
  86.         private const val INFELICITY_SWIPE = 5.0f
  87.     }
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement