4

I'm implementing a fragment with a scrollview and I want to detect left to right swipe in this fragment. I've used the following code to detect the swipe event:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

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

                @Override
                public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                                       float velocityY) {
                    Log.i("SWIPE", "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("SWIPE", "Right to Left");
                        }

                        else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                            Log.i("SWIPE", "Left to Right");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return super.onFling(e1, e2, velocityX, velocityY);
                }
            });

    container.setOnTouchListener(new View.OnTouchListener()

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


    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_blank, container, false);
}

I've tested this code without the scrollview in the layout and it worked fine. Bus as soon as I insert the scrollview in the layout the swipe event stop being detected.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.doutvisions.teste.BlankFragment"
android:id="@+id/scrollView1">


<TextView
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/hello_blank_fragment" />

</ScrollView>

Anyone can please help? I would appreciate.

emariano
  • 41
  • 1
  • 3
  • 2
    Possible duplicate of [Android: How to handle right to left swipe gestures](http://stackoverflow.com/questions/4139288/android-how-to-handle-right-to-left-swipe-gestures) or [How to detect the swipe left or Right in Android?](http://stackoverflow.com/questions/6645537/how-to-detect-the-swipe-left-or-right-in-android) – Mogsdad Apr 15 '16 at 02:07
  • The answers posted on [this question](http://stackoverflow.com/questions/4139288/android-how-to-handle-right-to-left-swipe-gestures) or [this question](http://stackoverflow.com/questions/6645537/how-to-detect-the-swipe-left-or-right-in-android) may help. I don't have enough rep to comment, otherwise I would have posted a comment instead of an answer. – Ishita Sinha Feb 26 '16 at 18:31
  • How did you solved this in the end? I am facing the same problem and the linked questions do not help in this sense. – b-fg Nov 23 '17 at 18:52

0 Answers0