Skip to content

Commit

Permalink
Swipe distance ratio customization (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tunous authored Apr 7, 2019
1 parent 9d04ccd commit 03c14d6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ SwipeActionView is a swipe-able view, which allows users to perform actions by s
- [Ripple animations](#ripple-animations)
- [Click listeners](#click-listeners)
- [Animate from code](#code-animation)
- [Activation distance](#activation-distance)
- [Attributes](#attr)
- [sav_rippleTakesPadding](#attr-rippleTakesPadding)
- [sav_swipeLeftRippleColor](#attr-swipeLeftRippleColor)
Expand Down Expand Up @@ -247,6 +248,9 @@ swipeView.animateToOriginalPosition(500)
swipeView.animateToOriginalPosition()
```

# <a id="activation-distance">Activation distance</a>
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.

# <a id="attr">Attributes</a>

#### <a id="attr-rippleTakesPadding">`app:sav_rippleTakesPadding="true|false"`</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,6 @@ class SwipeActionView : FrameLayout {
*/
private val handler = PressTimeoutHandler(this)

/**
* The percentage of the [maxLeftSwipeDistance] or [maxRightSwipeDistance] after which swipe
* callbacks can can be executed.
*/
private val minActivationDistanceRatio = 0.8f

/**
* Ripple displayed after performing swipe left gesture.
*/
Expand Down Expand Up @@ -198,12 +192,14 @@ class SwipeActionView : FrameLayout {
/**
* The minimum distance required to execute swipe callbacks when swiping to the left side.
*/
private var minLeftActivationDistance = 0f
private val minLeftActivationDistance: Float
get() = activationDistanceRatio * maxLeftSwipeDistance

/**
* The minimum distance required to execute swipe callbacks when swiping to the right side.
*/
private var minRightActivationDistance = 0f
private val minRightActivationDistance: Float
get() = activationDistanceRatio * maxRightSwipeDistance

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

/**
* The percentage of the swipe distance (width of the revealed view) after which swipe
* callbacks should be executed. (Defaults to 80%)
*/
var activationDistanceRatio = 0.8f
set(newRatio) {
if (newRatio < 0f || newRatio > 1f) {
throw IllegalArgumentException("Activation distance ratio must be a value in range <0.0f, 1.0f>. Provided: $newRatio")
}
field = newRatio
}

/**
* Listener for the swipe left and right gestures.
*/
Expand Down Expand Up @@ -347,15 +355,13 @@ class SwipeActionView : FrameLayout {
leftSwipeRipple.maxRadius = maxRadius
rightSwipeRipple.maxRadius = maxRadius

leftSwipeView?.let {
maxLeftSwipeDistance = it.totalWidth.toFloat() - container.marginEnd
minLeftActivationDistance = minActivationDistanceRatio * maxLeftSwipeDistance
}
maxLeftSwipeDistance = leftSwipeView?.let {
it.totalWidth.toFloat() - container.marginEnd
} ?: 0f

rightSwipeView?.let {
maxRightSwipeDistance = it.totalWidth.toFloat() - container.marginStart
minRightActivationDistance = minActivationDistanceRatio * maxRightSwipeDistance
}
maxRightSwipeDistance = rightSwipeView?.let {
it.totalWidth.toFloat() - container.marginStart
} ?: 0f

if (isInEditMode) {
when (previewBackground) {
Expand Down Expand Up @@ -541,7 +547,9 @@ class SwipeActionView : FrameLayout {
}

MotionEvent.ACTION_UP -> {
if (isClickable && isTouchValid && !dragging && !inLongPress && !hasMovedVertically(e)) {
if (isClickable && isTouchValid && !dragging && !inLongPress
&& !hasMovedVertically(e)
) {
startPress(e.x, e.y)
performClick()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public boolean onSwipedRight(@NonNull SwipeActionView swipeActionView) {
swipeLeft.setSwipeGestureListener(swipeGestureListener);

SwipeActionView swipeBoth = findViewById(R.id.swipe_both);
swipeBoth.setActivationDistanceRatio(0.5f);
swipeBoth.setSwipeGestureListener(swipeGestureListener);

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

public void swipeLeft(View view) {
swipeCustomLayout.animateInDirection(SwipeDirection.Left, true);
swipeCustomLayout.setActivationDistanceRatio(0.2f);
}

public void swipeRight(View view) {
swipeCustomLayout.animateInDirection(SwipeDirection.Right, true);
swipeCustomLayout.setActivationDistanceRatio(0.8f);
}
}

0 comments on commit 03c14d6

Please sign in to comment.