3

I am trying to get left, right, top, and bottom swipes detected on Android fragments. Using a couple online resources, such as this great answer on Stackoverflow, I now have up and down swipes being detected. However, it is not working for left and right swipes.

OnSwipeListener.java:

// Detects left and right swipes across a view public class OnSwipeTouchListener implements View.OnTouchListener { // https://developer.android.com/reference/android/view/GestureDetector private final GestureDetector gestureDetector; public OnSwipeTouchListener(Context context) { this.gestureDetector = new GestureDetector(context, new GestureListener()); } // from View.onTouchListener class @Override public boolean onTouch(View view, MotionEvent motionEvent) { return gestureDetector.onTouchEvent(motionEvent); } private final class GestureListener extends GestureDetector.SimpleOnGestureListener { private static final int SWIPE_THRESHOLD = 100; private static final int SWIPE_VELOCITY_THRESHOLD = 100; @Override public boolean onDown(MotionEvent e) { return true; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { boolean result = false; try { float diffY = e2.getY() - e1.getY(); float diffX = e2.getX() - e2.getX(); if(Math.abs(diffX) > Math.abs(diffY)) { if(Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { if(diffX > 0) { onSwipeRight(); }else { onSwipeLeft(); } result = true; } } else if(Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { if(diffY > 0) { onSwipeBottom(); } else { onSwipeTop(); } result = true; } } catch (Exception e) { e.printStackTrace(); } return result; } } public void onSwipeRight() { } public void onSwipeLeft() { } public void onSwipeTop() { } public void onSwipeBottom() { } } 

Then I use these methods in MyFragment.java, in the onCreateView method:

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.myfragment_layout, container, false); v.setOnTouchListener(new OnSwipeTouchListener(getActivity()) { public void onSwipeTop() { Toast.makeText(getActivity(), "TOP SWIPE", Toast.LENGTH_SHORT).show(); } public void onSwipeRight() { Toast.makeText(getActivity(), "RIGHT SWIPE", Toast.LENGTH_SHORT).show(); //go back to landing page // Intent intent = new Intent (getApplicationContext(), MainScreen.class); // startActivity (intent); } public void onSwipeLeft() { Toast.makeText(getActivity(), "LEFT SWIPE", Toast.LENGTH_SHORT).show(); } public void onSwipeBottom() { Toast.makeText(getActivity(), "BOTTOM SWIPE", Toast.LENGTH_SHORT).show(); } }); return v; } 

Any help would be appreciated. Thank you.

2 Answers 2

1

I believe this line is wrong:

float diffX = e2.getX() - e2.getX(); 

should be

float diffX = e2.getX() - e1.getX(); 
Sign up to request clarification or add additional context in comments.

1 Comment

Can't believe I missed that. After this change it is detecting left and right swipes, but only sometimes. Thank you.
0

Check below code to detect swipe gesture

 static class SwipeGestureDetector extends GestureDetector.SimpleOnGestureListener { OnViewPagerSwipe onViewPagerSwipe; @Override public boolean onSingleTapUp(MotionEvent e) { return true; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { switch (getSlope(e1.getX(), e1.getY(), e2.getX(), e2.getY())) { case 2: onViewPagerSwipe.onViewPagerSwipe(MainActivity.SWIPE_LEFT); return true; case 4: onViewPagerSwipe.onViewPagerSwipe(MainActivity.SWIPE_RIGHT); return true; } return false; } private int getSlope(float x1, float y1, float x2, float y2) { Double angle = Math.toDegrees(Math.atan2(y1 - y2, x2 - x1)); if (angle > 35 && angle <= 160) // top return 1; if (angle > 160 && angle <= 180 || angle < -160 && angle > -180) // left return 2; if (angle < -35 && angle >= -160) // down return 3; if (angle > -35 && angle <= 35) // right return 4; return 0; } void setViewPagerSwipeListener(OnViewPagerSwipe onViewPagerSwipe){ this.onViewPagerSwipe = onViewPagerSwipe; } } 

Interface

interface OnViewPagerSwipe{ void onViewPagerSwipe(int swipe); } 

Add this class from fragment onCreateView

SwipeGestureDetector swipeGestureDetector = new SwipeGestureDetector(); swipeGestureDetector.setViewPagerSwipeListener(this); GestureDetector mDetector = new GestureDetector(getActivity(), swipeGestureDetector); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.