-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSwipeControlTouchListener.kt
More file actions
47 lines (39 loc) · 1.6 KB
/
SwipeControlTouchListener.kt
File metadata and controls
47 lines (39 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package dev.abhishekbansal.swipecontrolviewpager2
import android.view.MotionEvent
import androidx.recyclerview.widget.RecyclerView
class SwipeControlTouchListener: RecyclerView.OnItemTouchListener {
private var initialXValue = 0f
private var direction: SwipeDirection = SwipeDirection.ALL
fun setSwipeDirection(direction: SwipeDirection) {
this.direction = direction
}
override fun onInterceptTouchEvent(rv: RecyclerView, event: MotionEvent): Boolean {
return !isSwipeAllowed(event)
}
override fun onTouchEvent(rv: RecyclerView, e: MotionEvent) {}
override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {}
private fun isSwipeAllowed(event: MotionEvent): Boolean {
if (direction === SwipeDirection.ALL) return true
if (direction == SwipeDirection.NONE) //disable any swipe
return false
if (event.action == MotionEvent.ACTION_DOWN) {
initialXValue = event.x
return true
}
if (event.action == MotionEvent.ACTION_MOVE) {
try {
val diffX: Float = event.x - initialXValue
if (diffX > 0 && direction == SwipeDirection.RIGHT) {
// swipe from left to right detected
return false
} else if (diffX < 0 && direction == SwipeDirection.LEFT) {
// swipe from right to left detected
return false
}
} catch (exception: Exception) {
exception.printStackTrace()
}
}
return true
}
}