检测 Fragment 上的左右滑动

Detecting left and right swipes on Fragment

我正在尝试在 Android 片段上检测到左、右、上和下滑动。使用几个在线资源,例如 Whosebug 上的这个很棒的 answer,我现在可以检测到上下滑动。但是,它不适用于左右滑动。

OnSwipeListener.java:

// Detects left and right swipes across a view
public class OnSwipeTouchListener implements View.OnTouchListener {

    // https://developer.android.com/reference/android/view/GestureDetector
    private final GestureDetector gestureDetector;

    public OnSwipeTouchListener(Context context) {
        this.gestureDetector = new GestureDetector(context, new GestureListener());
    }


    // from View.onTouchListener class
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        return gestureDetector.onTouchEvent(motionEvent);
    }

    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 boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            boolean result = false;

            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e2.getX();

                if(Math.abs(diffX) > Math.abs(diffY)) {
                    if(Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                        if(diffX > 0) {
                            onSwipeRight();
                        }else {
                            onSwipeLeft();
                        }
                        result = true;
                    }
                } else if(Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if(diffY > 0) {
                        onSwipeBottom();
                    } else {
                        onSwipeTop();
                    }
                    result = true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;

        }

    }

    public void onSwipeRight() {

    }

    public void onSwipeLeft() {

    }

    public void onSwipeTop() {

    }

    public void onSwipeBottom() {

    }

}

然后我在MyFragment.java中使用这些方法,在onCreateView方法中:

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

        View v = inflater.inflate(R.layout.myfragment_layout, container, false);

        v.setOnTouchListener(new OnSwipeTouchListener(getActivity()) {
            public void onSwipeTop() {
                Toast.makeText(getActivity(), "TOP SWIPE", Toast.LENGTH_SHORT).show();
            }

            public void onSwipeRight() {
                Toast.makeText(getActivity(), "RIGHT SWIPE", Toast.LENGTH_SHORT).show();
                //go back to landing page
                // Intent intent = new Intent (getApplicationContext(), MainScreen.class);
                // startActivity (intent);
            }


            public void onSwipeLeft() {
                Toast.makeText(getActivity(), "LEFT SWIPE", Toast.LENGTH_SHORT).show();
            }

            public void onSwipeBottom() {
                Toast.makeText(getActivity(), "BOTTOM SWIPE", Toast.LENGTH_SHORT).show();
            }
        });

        return v;
    }

如有任何帮助,我们将不胜感激。谢谢。

我认为这行是错误的:

float diffX = e2.getX() - e2.getX();

应该是

float diffX = e2.getX() - e1.getX();

检查以下代码以检测滑动手势

 static class SwipeGestureDetector extends GestureDetector.SimpleOnGestureListener {

    OnViewPagerSwipe onViewPagerSwipe;

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

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2,
                           float velocityX, float velocityY) {
        switch (getSlope(e1.getX(), e1.getY(), e2.getX(), e2.getY())) {
            case 2:
                onViewPagerSwipe.onViewPagerSwipe(MainActivity.SWIPE_LEFT);
                return true;
            case 4:
                onViewPagerSwipe.onViewPagerSwipe(MainActivity.SWIPE_RIGHT);
                return true;
        }
        return false;
    }

    private int getSlope(float x1, float y1, float x2, float y2) {
        Double angle = Math.toDegrees(Math.atan2(y1 - y2, x2 - x1));
        if (angle > 35 && angle <= 160)
            // top
            return 1;
        if (angle > 160 && angle <= 180 || angle < -160 && angle > -180)
            // left
            return 2;
        if (angle < -35 && angle >= -160)
            // down
            return 3;
        if (angle > -35 && angle <= 35)
            // right
            return 4;
        return 0;
    }

    void setViewPagerSwipeListener(OnViewPagerSwipe onViewPagerSwipe){
        this.onViewPagerSwipe = onViewPagerSwipe;
    }

}

界面

interface OnViewPagerSwipe{
    void onViewPagerSwipe(int swipe);
}

从片段 onCreateView

添加这个 class
SwipeGestureDetector swipeGestureDetector = new SwipeGestureDetector();
swipeGestureDetector.setViewPagerSwipeListener(this);
GestureDetector mDetector = new GestureDetector(getActivity(), swipeGestureDetector);