在安卓中滑动
大家好,我正在做一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的。从其他主题转发:
这是我能想到的最简单的 flinger 工作版本。
实际上,您可以将其绑定到任何组件,而不仅仅是 ImageView。
public class MyActivity extends Activity {
}
它不仅捕获水平方向,还捕获垂直方向(如果不需要,只需删除垂直部分),水平滑动优先为你可以看到。
在方法返回的地方(以及我的评论所在的地方)只需调用您的方法或其他:)
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 {
}
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 :)
如果您使用触摸输入,您应该返回
true
以表明它没有传递它,同时确保您有
setOnTouchListener
否则什么都不会使用您的 ontouchlistenerIf you use the touch input you should return
true
to show that it doesn't pass it onalso make sure you have
setOnTouchListener
or nothing will be using your ontouchlistener