Created
May 25, 2017 04:23
-
-
Save socmia/3e92868951d6732cab7767f5c3d0c450 to your computer and use it in GitHub Desktop.
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
public abstract class SwipeDismissBaseActivity extends AppCompatActivity { | |
private static final int SWIPE_MIN_DISTANCE = 120; | |
private static final int SWIPE_MAX_OFF_PATH = 250; | |
private static final int SWIPE_THRESHOLD_VELOCITY = 200; | |
private GestureDetector gestureDetector; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
gestureDetector = new GestureDetector(new SwipeDetector()); | |
} | |
private class SwipeDetector extends GestureDetector.SimpleOnGestureListener { | |
@Override | |
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { | |
// Check movement along the Y-axis. If it exceeds SWIPE_MAX_OFF_PATH, | |
// then dismiss the swipe. | |
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) | |
return false; | |
// Swipe from left to right. | |
// The swipe needs to exceed a certain distance (SWIPE_MIN_DISTANCE) | |
// and a certain velocity (SWIPE_THRESHOLD_VELOCITY). | |
if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { | |
finish(); | |
return true; | |
} | |
return false; | |
} | |
} | |
@Override | |
public boolean dispatchTouchEvent(MotionEvent ev) { | |
// TouchEvent dispatcher. | |
if (gestureDetector != null) { | |
if (gestureDetector.onTouchEvent(ev)) | |
// If the gestureDetector handles the event, a swipe has been | |
// executed and no more needs to be done. | |
return true; | |
} | |
return super.dispatchTouchEvent(ev); | |
} | |
@Override | |
public boolean onTouchEvent(MotionEvent event) { | |
return gestureDetector.onTouchEvent(event); | |
} | |
} | |
/** | |
Advantages of this way | |
Purley OOPS approach | |
Clean code- no need to write swipe listener in each type of layout used in project (Relative,Linear etc) | |
Work perfectly in ScrollView | |
*/ | |
public class SomeActivity extends SwipeDismissBaseActivity { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment