-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Fixed the home screen gestures not working in blank spaces.
- Loading branch information
1 parent
48d4251
commit 0513351
Showing
8 changed files
with
137 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
app/src/main/java/com/github/droidworksstudio/launcher/listener/ViewSwipeTouchListener.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.github.droidworksstudio.launcher.listener | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.Context | ||
import android.view.GestureDetector | ||
import android.view.GestureDetector.SimpleOnGestureListener | ||
import android.view.MotionEvent | ||
import android.view.View | ||
import android.view.View.OnTouchListener | ||
import kotlin.math.abs | ||
|
||
internal open class ViewSwipeTouchListener(c: Context?, v: View) : OnTouchListener { | ||
private val gestureDetector: GestureDetector = GestureDetector(c, GestureListener(v)) | ||
|
||
@SuppressLint("ClickableViewAccessibility") | ||
override fun onTouch(view: View, motionEvent: MotionEvent): Boolean { | ||
when (motionEvent.action) { | ||
MotionEvent.ACTION_DOWN -> view.isPressed = true | ||
MotionEvent.ACTION_UP -> view.isPressed = false | ||
} | ||
return gestureDetector.onTouchEvent(motionEvent) | ||
} | ||
|
||
private inner class GestureListener(private val view: View) : SimpleOnGestureListener() { | ||
private val swipeThreshold: Int = 100 | ||
private val swipeVelocityThreshold: Int = 100 | ||
|
||
override fun onDown(e: MotionEvent): Boolean { | ||
return true | ||
} | ||
|
||
override fun onSingleTapUp(e: MotionEvent): Boolean { | ||
onClick(view) | ||
return super.onSingleTapUp(e) | ||
} | ||
|
||
override fun onDoubleTap(e: MotionEvent): Boolean { | ||
onDoubleClick() | ||
return super.onDoubleTap(e) | ||
} | ||
|
||
override fun onLongPress(e: MotionEvent) { | ||
onLongClick(view) | ||
super.onLongPress(e) | ||
} | ||
|
||
override fun onFling( | ||
event1: MotionEvent?, | ||
event2: MotionEvent, | ||
velocityX: Float, | ||
velocityY: Float | ||
): Boolean { | ||
try { | ||
val diffY = event2.y - event1!!.y | ||
val diffX = event2.x - event1.x | ||
if (abs(diffX) > abs(diffY)) { | ||
if (abs(diffX) > swipeThreshold && abs(velocityX) > swipeVelocityThreshold) { | ||
if (diffX > 0) onSwipeRight() else onSwipeLeft() | ||
} | ||
} else { | ||
if (abs(diffY) > swipeThreshold && abs(velocityY) > swipeVelocityThreshold) { | ||
if (diffY < 0) onSwipeUp() else onSwipeDown() | ||
} | ||
} | ||
} catch (exception: Exception) { | ||
exception.printStackTrace() | ||
} | ||
return false | ||
} | ||
} | ||
|
||
open fun onSwipeRight() {} | ||
open fun onSwipeLeft() {} | ||
open fun onSwipeUp() {} | ||
open fun onSwipeDown() {} | ||
open fun onLongClick(view: View) {} | ||
open fun onDoubleClick() {} | ||
open fun onClick(view: View) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters