Skip to content

Commit 03c14d6

Browse files
authored
Swipe distance ratio customization (#18)
1 parent 9d04ccd commit 03c14d6

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ SwipeActionView is a swipe-able view, which allows users to perform actions by s
2020
- [Ripple animations](#ripple-animations)
2121
- [Click listeners](#click-listeners)
2222
- [Animate from code](#code-animation)
23+
- [Activation distance](#activation-distance)
2324
- [Attributes](#attr)
2425
- [sav_rippleTakesPadding](#attr-rippleTakesPadding)
2526
- [sav_swipeLeftRippleColor](#attr-swipeLeftRippleColor)
@@ -247,6 +248,9 @@ swipeView.animateToOriginalPosition(500)
247248
swipeView.animateToOriginalPosition()
248249
```
249250

251+
# <a id="activation-distance">Activation distance</a>
252+
You can customize the siwpe distance required for callbacks to be executed by using the `activationDistanceRatio` property. It receives a value in range from `0.0f` to `1.0f`, which means the percentage of background view that has to be revealed. For example if set to `0.5f` the user has to reveal at least half of the background view before releasing their finger in order for the gesture callbacks to be executed.
253+
250254
# <a id="attr">Attributes</a>
251255

252256
#### <a id="attr-rippleTakesPadding">`app:sav_rippleTakesPadding="true|false"`</a>

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

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,6 @@ class SwipeActionView : FrameLayout {
9494
*/
9595
private val handler = PressTimeoutHandler(this)
9696

97-
/**
98-
* The percentage of the [maxLeftSwipeDistance] or [maxRightSwipeDistance] after which swipe
99-
* callbacks can can be executed.
100-
*/
101-
private val minActivationDistanceRatio = 0.8f
102-
10397
/**
10498
* Ripple displayed after performing swipe left gesture.
10599
*/
@@ -198,12 +192,14 @@ class SwipeActionView : FrameLayout {
198192
/**
199193
* The minimum distance required to execute swipe callbacks when swiping to the left side.
200194
*/
201-
private var minLeftActivationDistance = 0f
195+
private val minLeftActivationDistance: Float
196+
get() = activationDistanceRatio * maxLeftSwipeDistance
202197

203198
/**
204199
* The minimum distance required to execute swipe callbacks when swiping to the right side.
205200
*/
206-
private var minRightActivationDistance = 0f
201+
private val minRightActivationDistance: Float
202+
get() = activationDistanceRatio * maxRightSwipeDistance
207203

208204
/**
209205
* Determines whether ripple drawables should have padding.
@@ -230,6 +226,18 @@ class SwipeActionView : FrameLayout {
230226
*/
231227
private var onLongClickListener: OnLongClickListener? = null
232228

229+
/**
230+
* The percentage of the swipe distance (width of the revealed view) after which swipe
231+
* callbacks should be executed. (Defaults to 80%)
232+
*/
233+
var activationDistanceRatio = 0.8f
234+
set(newRatio) {
235+
if (newRatio < 0f || newRatio > 1f) {
236+
throw IllegalArgumentException("Activation distance ratio must be a value in range <0.0f, 1.0f>. Provided: $newRatio")
237+
}
238+
field = newRatio
239+
}
240+
233241
/**
234242
* Listener for the swipe left and right gestures.
235243
*/
@@ -347,15 +355,13 @@ class SwipeActionView : FrameLayout {
347355
leftSwipeRipple.maxRadius = maxRadius
348356
rightSwipeRipple.maxRadius = maxRadius
349357

350-
leftSwipeView?.let {
351-
maxLeftSwipeDistance = it.totalWidth.toFloat() - container.marginEnd
352-
minLeftActivationDistance = minActivationDistanceRatio * maxLeftSwipeDistance
353-
}
358+
maxLeftSwipeDistance = leftSwipeView?.let {
359+
it.totalWidth.toFloat() - container.marginEnd
360+
} ?: 0f
354361

355-
rightSwipeView?.let {
356-
maxRightSwipeDistance = it.totalWidth.toFloat() - container.marginStart
357-
minRightActivationDistance = minActivationDistanceRatio * maxRightSwipeDistance
358-
}
362+
maxRightSwipeDistance = rightSwipeView?.let {
363+
it.totalWidth.toFloat() - container.marginStart
364+
} ?: 0f
359365

360366
if (isInEditMode) {
361367
when (previewBackground) {
@@ -541,7 +547,9 @@ class SwipeActionView : FrameLayout {
541547
}
542548

543549
MotionEvent.ACTION_UP -> {
544-
if (isClickable && isTouchValid && !dragging && !inLongPress && !hasMovedVertically(e)) {
550+
if (isClickable && isTouchValid && !dragging && !inLongPress
551+
&& !hasMovedVertically(e)
552+
) {
545553
startPress(e.x, e.y)
546554
performClick()
547555
}

sample/src/main/java/me/thanel/swipeactionview/sample/MainActivity.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public boolean onSwipedRight(@NonNull SwipeActionView swipeActionView) {
5656
swipeLeft.setSwipeGestureListener(swipeGestureListener);
5757

5858
SwipeActionView swipeBoth = findViewById(R.id.swipe_both);
59+
swipeBoth.setActivationDistanceRatio(0.5f);
5960
swipeBoth.setSwipeGestureListener(swipeGestureListener);
6061

6162
SwipeActionView swipeWithRipples = findViewById(R.id.swipe_with_ripples);
@@ -93,9 +94,11 @@ private void showToast(Boolean swipedRight) {
9394

9495
public void swipeLeft(View view) {
9596
swipeCustomLayout.animateInDirection(SwipeDirection.Left, true);
97+
swipeCustomLayout.setActivationDistanceRatio(0.2f);
9698
}
9799

98100
public void swipeRight(View view) {
99101
swipeCustomLayout.animateInDirection(SwipeDirection.Right, true);
102+
swipeCustomLayout.setActivationDistanceRatio(0.8f);
100103
}
101104
}

0 commit comments

Comments
 (0)