Android: How to handle right to left swipe gestures

前端 未结 22 1526
日久生厌
日久生厌 2020-11-21 06:18

I want my app to recognize when a user swipes from right to left on the phone screen.

How to do this?

22条回答
  •  别那么骄傲
    2020-11-21 07:22

    My solution is similar to those above but I have abstracted the gesture handling into an abstract class OnGestureRegisterListener.java, which includes swipe, click and long click gestures.

    OnGestureRegisterListener.java

    public abstract class OnGestureRegisterListener implements View.OnTouchListener {
    
        private final GestureDetector gestureDetector;
        private View view;
    
        public OnGestureRegisterListener(Context context) {
            gestureDetector = new GestureDetector(context, new GestureListener());
        }
    
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            this.view = view;
            return gestureDetector.onTouchEvent(event);
        }
    
        public abstract void onSwipeRight(View view);
        public abstract void onSwipeLeft(View view);
        public abstract void onSwipeBottom(View view);
        public abstract void onSwipeTop(View view);
        public abstract void onClick(View view);
        public abstract boolean onLongClick(View view);
    
        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 void onLongPress(MotionEvent e) {
                onLongClick(view);
                super.onLongPress(e);
            }
    
            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                onClick(view);
                return super.onSingleTapUp(e);
            }
    
            @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() - e1.getX();
                    if (Math.abs(diffX) > Math.abs(diffY)) {
                        if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                            if (diffX > 0) {
                                onSwipeRight(view);
                            } else {
                                onSwipeLeft(view);
                            }
                            result = true;
                        }
                    }
                    else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffY > 0) {
                            onSwipeBottom(view);
                        } else {
                            onSwipeTop(view);
                        }
                        result = true;
                    }
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
                return result;
            }
    
        }
    }
    

    And use it like so. Note that you can also easily pass in your View parameter.

    OnGestureRegisterListener onGestureRegisterListener = new OnGestureRegisterListener(this) {
        public void onSwipeRight(View view) {
            // Do something
        }
        public void onSwipeLeft(View view) {
            // Do something
        }
        public void onSwipeBottom(View view) {
            // Do something
        }
        public void onSwipeTop(View view) {
            // Do something
        }
        public void onClick(View view) {
            // Do something
        }
        public boolean onLongClick(View view) { 
            // Do something
            return true;
        }
    };
    
    Button button = findViewById(R.id.my_button);
    button.setOnTouchListener(onGestureRegisterListener);
    

提交回复
热议问题