开发者

Swipe in android

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.sSwipeMinDist开发者_运维问答ance && 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;
}


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 :)


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜