android - 检测 Fragment 上的左右滑动

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

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

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();

关于android - 检测 Fragment 上的左右滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51481111/

相关文章:

Android 模拟器和 OpenGL ES3 : EGL_BAD_CONFIG

android:如何检测水平和垂直手势线

user-interface - 奇偶校验 GUI 错误(getTransactions TypeError : Failed to fetch)

java - Android ListView 像 Twitter 列表一样滑动操作

java - 如何使用 Intent.ACTION_PICK 带响应,但不带 Activity?

android - 如何在 ExpandableListView 中选择 child ?

java - 使用箭头键浏览 JButton

wpf - 请帮助我了解WPF的UI线程

iphone - 在 UIScrollView 中使用滑动手势而不禁用水平和垂直滚动

android - 单击其子项时禁用 View 寻呼机的滑动