0

So I have a TextView that resides inside a fragment in my activity. I'm trying to add a gestureDetector to that TextView so that the user can carry out certain actions that pertain to it. I especially want the swipe or fling. My code is as follows:

public void onViewCreated(View view, Bundle savedInstanceState)
{       
    myGestDetector = new GestureDetector(getActivity(), new    GestureDetector.SimpleOnGestureListener());
    TextView mainTextView = (TextView)getView().findViewById(R.id.HourCount);
    mainTextView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            myGestDetector.onTouchEvent(motionEvent);
            return false;
        }

    });

}
@Override
public boolean onDown(MotionEvent motionEvent) {
    return false;
}

@Override
public void onShowPress(MotionEvent motionEvent) {

}

@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
    return false;
}

@Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float v, float v2) {
    return false;
}

@Override
public void onLongPress(MotionEvent motionEvent) {

}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {
    Log.d("GESTURES", "onFling: " + event1.toString()+event2.toString());
    return true;
}

The issue is that I cannot get any of these to fire. I've tried doing the fling several times, but the log isn't showing that I am calling the onFling. Does anyone have any idea what I'm doing wrong?

Morteza Soleimani
  • 2,652
  • 3
  • 25
  • 44
axtscz
  • 631
  • 2
  • 6
  • 21

1 Answers1

0
 mainTextView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        myGestDetector.onTouchEvent(motionEvent);
        return false;  // Return true from here or better return  myGestDetector.onTouchEvent(motionEvent);
    }

});

By returning true android knows that you are consuming touch event Refer : onFling for a TextView not working

Edit :

myGestDetector = new GestureDetector(getActivity(), new GestureListener());

make a private class like this

private class GestureListener implements GestureDetector.OnGestureListener {

        public GestureListener() {
        }

        @Override
        public boolean onDown(MotionEvent motionEvent) {
            return false;
        }

        @Override
        public void onShowPress(MotionEvent motionEvent) {

        }

        @Override
        public boolean onSingleTapUp(MotionEvent motionEvent) {
            return false;
        }

        @Override
        public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float v, float v2) {
            return false;
        }

        @Override
        public void onLongPress(MotionEvent motionEvent) {

        }
        @Override
        public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {
            Log.d("GESTURES", "onFling: " + event1.toString()+event2.toString());
            return true;
        }
    }
Community
  • 1
  • 1
maaz
  • 204
  • 3
  • 5
  • Still not working. No log entries to indicate onFling is even getting called – axtscz Aug 31 '14 at 18:41
  • Also why are you using GestureDetector.SimpleOnGestureListener() please use the custom gesture listener that you have created , view edited answer – maaz Aug 31 '14 at 18:52