在安卓中滑动

发布于 2024-11-27 23:41:55 字数 1923 浏览 1 评论 0原文

大家好,我正在做一个 Android 项目来实现滑动功能。 如何做到这一点。

我正在尝试使用 onfling 方法,但它对我不起作用。任何人都有正确的解决方案,请回复这篇文章。

我的在线代码是

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    if (mGesturesEnabled) {
        try {
            if (e2.getY() - e1.getY() > StudyOptions.sSwipeMinDistance && Math.abs(velocityY) > StudyOptions.sSwipeThresholdVelocity && Math.abs(e1.getX() - e2.getX()) < StudyOptions.sSwipeMaxOffPath && !mIsYScrolling) {
                // down
                executeCommand(mGestureSwipeDown);
            } else if (e1.getY() - e2.getY() > StudyOptions.sSwipeMinDistance && Math.abs(velocityY) > StudyOptions.sSwipeThresholdVelocity && Math.abs(e1.getX() - e2.getX()) < StudyOptions.sSwipeMaxOffPath && !mIsYScrolling) {
                // up
                executeCommand(mGestureSwipeUp);
            } else if (e2.getX() - e1.getX() > StudyOptions.sSwipeMinDistance && Math.abs(velocityX) > StudyOptions.sSwipeThresholdVelocity && Math.abs(e1.getY() - e2.getY()) < StudyOptions.sSwipeMaxOffPath && !mIsXScrolling && !mIsSelecting) {
                // right
                executeCommand(mGestureSwipeRight);
            } else if (e1.getX() - e2.getX() > StudyOptions.sSwipeMinDistance && Math.abs(velocityX) > StudyOptions.sSwipeThresholdVelocity && Math.abs(e1.getY() - e2.getY()) < StudyOptions.sSwipeMaxOffPath && !mIsXScrolling && !mIsSelecting) {
                // left
                executeCommand(mGestureSwipeLeft);
            }

            mIsXScrolling = false;
            mIsYScrolling = false;
         } catch (Exception e) {
            Log.e(AnkiDroidApp.TAG, "onFling Exception = " + e.getMessage());
         }
    }
    return false;
}

Hi guys am doing an android project to implement the swipe functionality.
How to do that.

Am trying with onfling method but it doesnt work for me.. Anyone have the correct solution pls reply to this post.

My onfling code is

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    if (mGesturesEnabled) {
        try {
            if (e2.getY() - e1.getY() > StudyOptions.sSwipeMinDistance && Math.abs(velocityY) > StudyOptions.sSwipeThresholdVelocity && Math.abs(e1.getX() - e2.getX()) < StudyOptions.sSwipeMaxOffPath && !mIsYScrolling) {
                // down
                executeCommand(mGestureSwipeDown);
            } else if (e1.getY() - e2.getY() > StudyOptions.sSwipeMinDistance && Math.abs(velocityY) > StudyOptions.sSwipeThresholdVelocity && Math.abs(e1.getX() - e2.getX()) < StudyOptions.sSwipeMaxOffPath && !mIsYScrolling) {
                // up
                executeCommand(mGestureSwipeUp);
            } else if (e2.getX() - e1.getX() > StudyOptions.sSwipeMinDistance && Math.abs(velocityX) > StudyOptions.sSwipeThresholdVelocity && Math.abs(e1.getY() - e2.getY()) < StudyOptions.sSwipeMaxOffPath && !mIsXScrolling && !mIsSelecting) {
                // right
                executeCommand(mGestureSwipeRight);
            } else if (e1.getX() - e2.getX() > StudyOptions.sSwipeMinDistance && Math.abs(velocityX) > StudyOptions.sSwipeThresholdVelocity && Math.abs(e1.getY() - e2.getY()) < StudyOptions.sSwipeMaxOffPath && !mIsXScrolling && !mIsSelecting) {
                // left
                executeCommand(mGestureSwipeLeft);
            }

            mIsXScrolling = false;
            mIsYScrolling = false;
         } catch (Exception e) {
            Log.e(AnkiDroidApp.TAG, "onFling Exception = " + e.getMessage());
         }
    }
    return false;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怎会甘心 2024-12-04 23:41:55

好的。从其他主题转发:

这是我能想到的最简单的 flinger 工作版本。
实际上,您可以将其绑定到任何组件,而不仅仅是 ImageView。

public class MyActivity extends Activity {

private void onCreate() {
    final ImageView imageView  = (ImageView) findViewById(R.id.image_view);
    imageView.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(final View view, final MotionEvent event) {
            gdt.onTouchEvent(event);
            return true;
        }
    });
}

private final GestureDetector gdt = new GestureDetector(new GestureListener());

private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private class GestureListener extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            return true; // Right to left
        }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            return true; // Left to right
        }
        if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
            return true; // Bottom to top
        }  else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
            return true; // Top to bottom
        }
        return false;
    }
}

}

它不仅捕获水平方向,还捕获垂直方向(如果不需要,只需删除垂直部分),水平滑动优先为你可以看到。
在方法返回的地方(以及我的评论所在的地方)只需调用您的方法或其他:)

Ok. Repost from the other topic:

Here's the simpliest working version of flinger I can think of.
You can actually tie it to any component, not only ImageView.

public class MyActivity extends Activity {

private void onCreate() {
    final ImageView imageView  = (ImageView) findViewById(R.id.image_view);
    imageView.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(final View view, final MotionEvent event) {
            gdt.onTouchEvent(event);
            return true;
        }
    });
}

private final GestureDetector gdt = new GestureDetector(new GestureListener());

private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private class GestureListener extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            return true; // Right to left
        }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            return true; // Left to right
        }
        if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
            return true; // Bottom to top
        }  else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
            return true; // Top to bottom
        }
        return false;
    }
}

}

It captures not only horizontal, but vertical also (just delete vertical part if you don't need it), and horizontal swipes have priority as you can see.
In places where method returns (nad where my comments are) just call your method or whatever :)

断桥再见 2024-12-04 23:41:55

如果您使用触摸输入,您应该返回 true 以表明它没有传递它,

同时确保您有 setOnTouchListener 否则什么都不会使用您的 ontouchlistener

If you use the touch input you should return true to show that it doesn't pass it on

also make sure you have setOnTouchListener or nothing will be using your ontouchlistener

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文