检测片段上的左右滑动

时间:2018-07-23 14:23:46

标签: android user-interface swipe gesture-recognition user-interaction

我正在尝试在Android片段上检测到左,右,上和下滑动。使用一些在线资源,例如Stackoverflow上的出色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;
    }

任何帮助将不胜感激。谢谢。

2 个答案:

答案 0 :(得分:1)

我认为这行是错误的:

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

应该是

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

答案 1 :(得分:0)

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

 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

添加此类
SwipeGestureDetector swipeGestureDetector = new SwipeGestureDetector();
swipeGestureDetector.setViewPagerSwipeListener(this);
GestureDetector mDetector = new GestureDetector(getActivity(), swipeGestureDetector);