Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect swipe gesture in fragment

Hi I have implemented a Fragment, and I want to detect touch event on my fragment.

But I am unable to detect that event, as a matter of fact, no event is beign detected at all. It works well in Activity, but it's not working on fragments.

Following is my code:

  public class Swipe_Fragment extends Fragment implements
    GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {

private SimpleGestureFilter detector;
private LinearLayout swipLinear;
private static int DEFAULT = 50;
private int brightness;
private GestureDetectorCompat mDetector;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.swipe_screen, container,
            false);
    swipLinear = (LinearLayout) rootView.findViewById(R.id.swipLinear);

    brightness = DEFAULT;
    mDetector = new GestureDetectorCompat(getActivity(), this);
    // Set the gesture detector as the double tap
    // listener.
    mDetector.setOnDoubleTapListener(this);
    // Detect touched area
    // detector = new SimpleGestureFilter(getActivity(), this);
    return rootView;
}


@Override
public boolean onDoubleTap(MotionEvent arg0) {
    Toast.makeText(getActivity(), "onDoubleTap", Toast.LENGTH_LONG).show();
    return false;
}

@Override
public boolean onDoubleTapEvent(MotionEvent e) {
    Toast.makeText(getActivity(), "onDoubleTapEvent", Toast.LENGTH_LONG).show();
    return false;
}

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
    Toast.makeText(getActivity(), "onSingleTapConfirmed", Toast.LENGTH_LONG).show();
    return false;
}

@Override
public boolean onDown(MotionEvent e) {
    Toast.makeText(getActivity(), "onDown", Toast.LENGTH_LONG).show();
    return false;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    Toast.makeText(getActivity(), "onFling", Toast.LENGTH_LONG).show();
    return false;
}

@Override
public void onLongPress(MotionEvent e) {
    Toast.makeText(getActivity(), "onLongPress", Toast.LENGTH_LONG).show();

}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
        float distanceY) {
    Toast.makeText(getActivity(), "onScroll", Toast.LENGTH_LONG).show();
    return false;
}

@Override
public void onShowPress(MotionEvent e) {
    Toast.makeText(getActivity(), "onShowPress", Toast.LENGTH_LONG).show();

}

@Override
public boolean onSingleTapUp(MotionEvent e) {
    Toast.makeText(getActivity(), "onSingleTapUp", Toast.LENGTH_LONG).show();
    return false;
}

}
like image 361
Siddhpura Amit Avatar asked Apr 10 '14 07:04

Siddhpura Amit


Video Answer


1 Answers

Finally I got a solution from the code below, which is from Android Fragment onCreateView with Gestures.

  final GestureDetector gesture = new GestureDetector(getActivity(),
    new GestureDetector.SimpleOnGestureListener() {

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
            Log.i(Constants.APP_TAG, "onFling has been called!");
            final int SWIPE_MIN_DISTANCE = 120;
            final int SWIPE_MAX_OFF_PATH = 250;
            final int SWIPE_THRESHOLD_VELOCITY = 200;
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    Log.i(Constants.APP_TAG, "Right to Left");
                } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    Log.i(Constants.APP_TAG, "Left to Right");
                }
            } catch (Exception e) {
                // nothing
            }
            return super.onFling(e1, e2, velocityX, velocityY);
        }
    });

v.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gesture.onTouchEvent(event);
    }
});

This goes in the onCreateView() method of the fragment.

like image 196
Siddhpura Amit Avatar answered Oct 03 '22 13:10

Siddhpura Amit