Search code examples
androidgesture

How to handle swipe down gesture on an ImageView


I am trying for recognizing swipe down gesture on an imageview. I tried GestureDetector but it's deprecated now. Sample code I am using is below. But, it's not going inside onFling() method.

GestureDetector gestureDetector;
gestureDetector = new GestureDetector(new GestureListener());

        view.findViewById(R.id.imageView).setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    System.out.println("DONE-------");
                    updateSwipeAction();
                    return true;
               }
                return false;
            }
        });

class GestureListener extends SimpleOnGestureListener {

        private static final int SWIPE_MIN_DISTANCE = 10;
        private static final int SWIPE_THRESHOLD_VELOCITY = 10;

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE) {
                //From Right to Left
                return true;
            } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE) {
                //From Left to Right
                return true;
            }

            if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE) {
                //From Bottom to Top
                return true;
            } else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE) {
                //From Top to Bottom
                return true;
            }
            return false;
        }

        @Override
        public boolean onDown(MotionEvent e) {
            //always return true since all gestures always begin with onDown and<br>
            //if this returns false, the framework won't try to pick up onFling for example.
            return true;
        }
    }

Solution

  • Try this ,Implements ongestureListener and theres a onscroll method u can access directly

    @SuppressLint("NewApi")

    public class AndroidExplorer extends Activity implements    
         android.view.GestureDetector.OnGestureListener
    
        {
        private Handler mHandler = new Handler();
        @Override
        protected void onCreate(Bundle mybundle) 
        {
        // TODO Auto-generated method stub  
        super.onCreate(mybundle);
        setContentView(R.layout.main);
        }
        @Override
        public boolean onDown(MotionEvent e) {
            // TODO Auto-generated method stub
            return false;
        }
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            // TODO Auto-generated method stub
            return false;
        }
        @Override
        public void onLongPress(MotionEvent e) {
            // TODO Auto-generated method stub
    
        }
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                float distanceY) {
            // TODO Auto-generated method stub
            return false;
        }
        @Override
        public void onShowPress(MotionEvent e) {
            // TODO Auto-generated method stub
    
        }
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            // TODO Auto-generated method stub
            return false;
        }
    
    
    
    
    }