Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. public abstract class SwipeActivity extends Activity {
  2.  
  3. private static final int SWIPE_MIN_DISTANCE = 120;
  4. private static final int SWIPE_MAX_OFF_PATH = 250;
  5. private int SWIPE_THRESHOLD_VELOCITY;
  6. private GestureDetector gestureDetector;
  7.  
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. gestureDetector = new GestureDetector(new SwipeDetector());
  12. ViewConfiguration configuration = ViewConfiguration.get(this);
  13. SWIPE_THRESHOLD_VELOCITY = configuration.getScaledMinimumFlingVelocity();
  14. }
  15.  
  16. private class SwipeDetector extends SimpleOnGestureListener {
  17. @Override
  18. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
  19. float velocityY) {
  20.  
  21. // Check movement along the Y-axis. If it exceeds
  22. // SWIPE_MAX_OFF_PATH,
  23. // then dismiss the swipe.
  24. if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
  25. return false;
  26.  
  27. // Swipe from right to left.
  28. // The swipe needs to exceed a certain distance (SWIPE_MIN_DISTANCE)
  29. // and a certain velocity (SWIPE_THRESHOLD_VELOCITY).
  30. if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
  31. && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
  32. leftSwipe();
  33. return true;
  34. }
  35.  
  36. // Swipe from left to right.
  37. // The swipe needs to exceed a certain distance (SWIPE_MIN_DISTANCE)
  38. // and a certain velocity (SWIPE_THRESHOLD_VELOCITY).
  39. if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
  40. && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
  41. rightSwipe();
  42. return true;
  43. }
  44.  
  45. return false;
  46. }
  47. }
  48.  
  49. @Override
  50. public boolean dispatchTouchEvent(MotionEvent ev) {
  51. // TouchEvent dispatcher.
  52. if (gestureDetector != null) {
  53. if (gestureDetector.onTouchEvent(ev))
  54. // If the gestureDetector handles the event, a swipe has been
  55. // executed and no more needs to be done.
  56. return true;
  57. }
  58. return super.dispatchTouchEvent(ev);
  59. }
  60.  
  61. @Override
  62. public boolean onTouchEvent(MotionEvent event) {
  63. return gestureDetector.onTouchEvent(event);
  64. }
  65.  
  66. protected abstract void rightSwipe();
  67.  
  68. protected abstract void leftSwipe();
  69. }
  70.  
  71. public class ContentActivity extends SwipeActivity implements OnLongClickListener {
  72.  
  73. @Override
  74. public void onCreate(Bundle savedInstanceState) {
  75. super.onCreate(savedInstanceState);
  76.  
  77. // Here I add a TextView programmatically to my Vertical ScrollView and set this activity to listen to its long clicks (touches)
  78. ScrollView scrollView = (ScrollView) findViewById(R.id.content_scroll);
  79. TextView textView = new TextView(ContentActivity.this);
  80. textView.setText("Some Really Long Text!");
  81. textView.setOnLongClickListener(this);
  82. scrollView.addView(textView);
  83. }
  84.  
  85. @Override
  86. public boolean onLongClick(View v) {
  87. return doLongClickAction();
  88. }
  89.  
  90. @Override
  91. public void rightSwipe() {
  92. doSwipeAction();
  93. }
  94.  
  95. @Override
  96. public void leftSwipe() {
  97. doSwipeAction();
  98. }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement