在Android中滑动

时间:2011-08-04 06:42:09

标签: android swipe

大家好我正在做一个Android项目来实现滑动功能。 怎么做。

尝试使用onfling方法,但它对我不起作用..任何人都有正确的解决方案请回复此帖。

我的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;
}

2 个答案:

答案 0 :(得分:5)

确定。转发其他主题:

这是我能想到的最简单的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;
    }
}

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; } }

它不仅捕获水平,还捕获垂直(如果不需要,只删除垂直部分),水平滑动具有优先级,如您所见。 在方法返回的地方(nad我的注释)只需调用你的方法或其他:)

答案 1 :(得分:1)

如果您使用触摸输入,则应返回true以表明它未通过

还要确保你有setOnTouchListener或没有任何东西可以使用你的ontouchlistener