Skip to content

Commit f7589b1

Browse files
committed
Fix press highlight hotspot for long-press
1 parent a5b7ce0 commit f7589b1

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

library/src/main/kotlin/me/thanel/swipeactionview/SwipeActionView.kt

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ class SwipeActionView : FrameLayout {
5858
/**
5959
* The duration in milliseconds we will wait to see if a touch event is a tap or a scroll.
6060
*/
61-
private val tapTimeout = ViewConfiguration.getTapTimeout()
61+
private val tapTimeout = ViewConfiguration.getTapTimeout().toLong()
6262

6363
/**
6464
* The duration in milliseconds we will wait to see if a touch event is a long tap.
6565
*/
66-
private val longPressTimeout = tapTimeout + ViewConfiguration.getLongPressTimeout()
66+
private val longPressTimeout = tapTimeout + ViewConfiguration.getLongPressTimeout().toLong()
6767

6868
/**
6969
* The duration of the pressed state.
@@ -127,14 +127,14 @@ class SwipeActionView : FrameLayout {
127127
private var canPerformSwipeAction = true
128128

129129
/**
130-
* The x coordinate of the initial motion event.
130+
* The raw x coordinate of the initial motion event.
131131
*/
132-
private var initialX = 0f
132+
private var initialRawX = 0f
133133

134134
/**
135-
* The y coordinate of the initial motion event.
135+
* The raw y coordinate of the initial motion event.
136136
*/
137-
private var initialY = 0f
137+
private var initialRawY = 0f
138138

139139
/**
140140
* The x coordinate of the last received move motion event.
@@ -425,6 +425,7 @@ class SwipeActionView : FrameLayout {
425425
* @param enabled Whether swiping in the specified direction should be enabled.
426426
* @throws IllegalAccessException When view for the specified direction doesn't exist.
427427
*/
428+
@Suppress("unused")
428429
fun setDirectionEnabled(direction: SwipeDirection, enabled: Boolean) {
429430
val view = getViewForDirection(direction) ?:
430431
throw IllegalArgumentException("View for the specified direction doesn't exist.")
@@ -456,6 +457,7 @@ class SwipeActionView : FrameLayout {
456457
* @param direction The direction of the swipe gesture.
457458
* @param color The ripple color.
458459
*/
460+
@Suppress("unused")
459461
fun setRippleColor(direction: SwipeDirection, @ColorInt color: Int) = when (direction) {
460462
SwipeDirection.Left -> leftSwipeRipple.color = color
461463
SwipeDirection.Right -> rightSwipeRipple.color = color
@@ -587,8 +589,8 @@ class SwipeActionView : FrameLayout {
587589
velocityTracker.addMovement(e)
588590

589591
lastX = e.rawX
590-
initialX = e.rawX
591-
initialY = e.rawY
592+
initialRawX = e.rawX
593+
initialRawY = e.rawY
592594

593595
// Stop the animator to allow "catching" of view.
594596
// By "catching" I mean the possibility for user to click on the view and continue swiping
@@ -623,10 +625,12 @@ class SwipeActionView : FrameLayout {
623625
private fun prepareMessages(e: MotionEvent) {
624626
if (!isClickable && !isLongClickable) return
625627

626-
handler.sendEmptyMessageAtTime(TAP, e.downTime + tapTimeout)
628+
handler.x = e.x
629+
handler.y = e.y
630+
handler.sendEmptyMessageDelayed(TAP, tapTimeout)
627631

628632
if (isLongClickable) {
629-
handler.sendEmptyMessageAtTime(LONG_PRESS, e.downTime + longPressTimeout)
633+
handler.sendEmptyMessageDelayed(LONG_PRESS, longPressTimeout)
630634
}
631635
}
632636

@@ -667,13 +671,13 @@ class SwipeActionView : FrameLayout {
667671
*
668672
* @return Whether the user has moved their finger vertically.
669673
*/
670-
private fun hasMovedVertically(e: MotionEvent) = Math.abs(e.rawY - initialY) >= touchSlop
674+
private fun hasMovedVertically(e: MotionEvent) = Math.abs(e.rawY - initialRawY) >= touchSlop
671675

672676
/**
673677
* Tells whether the drag can be started by the user based on provided motion event.
674678
*/
675679
private fun canStartDrag(e: MotionEvent): Boolean {
676-
val movedFarEnough = Math.abs(e.rawX - initialX) > touchSlop
680+
val movedFarEnough = Math.abs(e.rawX - initialRawX) > touchSlop
677681
return movedFarEnough && isTouchValid
678682
}
679683

@@ -691,11 +695,11 @@ class SwipeActionView : FrameLayout {
691695
}
692696

693697
private fun startPress(x: Float, y: Float) {
694-
isPressed = true
695-
696698
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
697699
drawableHotspotChanged(x, y)
698700
}
701+
702+
isPressed = true
699703
}
700704

701705
/**
@@ -920,14 +924,17 @@ class SwipeActionView : FrameLayout {
920924
private const val TAP = 2
921925

922926
private class PressTimeoutHandler(private val swipeActionView: SwipeActionView) : Handler() {
927+
var x = 0f
928+
var y = 0f
929+
923930
override fun handleMessage(msg: Message) {
924931
when (msg.what) {
925932
LONG_PRESS -> {
926933
swipeActionView.inLongPress = true
927934
swipeActionView.performLongClick()
928935
}
929936
TAP -> {
930-
swipeActionView.startPress(swipeActionView.initialX, swipeActionView.initialY)
937+
swipeActionView.startPress(x, y)
931938
}
932939
}
933940
}

0 commit comments

Comments
 (0)