Skip to content

Commit

Permalink
Merge pull request #906 from RobbWatershed/develop
Browse files Browse the repository at this point in the history
Swipe customization (in response to #905)
  • Loading branch information
mikepenz authored Jul 1, 2020
2 parents 533f439 + 7eb8fb1 commit a97782e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ class SwipeListActivity : AppCompatActivity(), ItemTouchCallback, SimpleSwipeCal
.withBackgroundSwipeRight(Color.BLUE)
.withLeaveBehindSwipeRight(leaveBehindDrawableRight)
.withNotifyAllDrops(true)
.withSensitivity(10f)
.withSurfaceThreshold(0.8f)

touchHelper = ItemTouchHelper(touchCallback) // Create ItemTouchHelper and pass with parameter the SimpleDragCallback
touchHelper.attachToRecyclerView(rv) // Attach ItemTouchHelper to RecyclerView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class SimpleSwipeCallback @JvmOverloads constructor(private val itemSwipeCallbac
private var bgPaint: Paint? = null
private var horizontalMargin = Integer.MAX_VALUE

// Swipe movement control
private var sensitivityFactor = 1f
private var surfaceThreshold = 0.5f

interface ItemSwipeCallback {

/**
Expand Down Expand Up @@ -67,6 +71,26 @@ class SimpleSwipeCallback @JvmOverloads constructor(private val itemSwipeCallbac
return this
}

/**
* Control the sensitivity of the swipe gesture
* 0.5 : very sensitive
* 1 : Android default
* 10 : almost insensitive
*/
fun withSensitivity(f: Float): SimpleSwipeCallback {
sensitivityFactor = f
return this
}

/**
* % of the item's width or height needed to confirm the swipe action
* Android default : 0.5
*/
fun withSurfaceThreshold(f: Float): SimpleSwipeCallback {
surfaceThreshold = f
return this
}

override fun getSwipeDirs(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int {
val item = FastAdapter.getHolderAdapterItem<IItem<*>>(viewHolder)
return if (item is ISwipeable) {
Expand Down Expand Up @@ -94,6 +118,14 @@ class SimpleSwipeCallback @JvmOverloads constructor(private val itemSwipeCallbac
return false
}

override fun getSwipeEscapeVelocity(defaultValue: Float): Float {
return defaultValue * sensitivityFactor
}

override fun getSwipeThreshold(viewHolder: RecyclerView.ViewHolder): Float {
return surfaceThreshold
}

//Inspired/modified from: https://github.com/nemanja-kovacevic/recycler-view-swipe-to-delete/blob/master/app/src/main/java/net/nemanjakovacevic/recyclerviewswipetodelete/MainActivity.java
override fun onChildDraw(c: Canvas, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean) {
val itemView = viewHolder.itemView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ class SimpleSwipeDragCallback @JvmOverloads constructor(
return this
}

fun withSensitivity(f: Float): SimpleSwipeDragCallback {
simpleSwipeCallback.withSensitivity(f)
return this
}

fun withSurfaceThreshold(f: Float): SimpleSwipeDragCallback {
simpleSwipeCallback.withSurfaceThreshold(f)
return this
}

override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
simpleSwipeCallback.onSwiped(viewHolder, direction)
}
Expand All @@ -84,6 +94,13 @@ class SimpleSwipeDragCallback @JvmOverloads constructor(
return simpleSwipeCallback.getSwipeDirs(recyclerView, viewHolder)
}

override fun getSwipeEscapeVelocity(defaultValue: Float): Float {
return simpleSwipeCallback.getSwipeEscapeVelocity(defaultValue)
}

override fun getSwipeThreshold(viewHolder: RecyclerView.ViewHolder): Float {
return simpleSwipeCallback.getSwipeThreshold(viewHolder)
}

override fun onChildDraw(
c: Canvas,
Expand Down

0 comments on commit a97782e

Please sign in to comment.