androiduser-interfaceswipegesture-recognitionuser-interaction

Detecting left and right swipes on Fragment


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.


Solution

  • I believe this line is wrong:

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

    should be

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