Swipt detector gestures android

大城市里の小女人 提交于 2019-12-12 02:47:42

问题


@Override
public boolean onTouchEvent(MotionEvent event) {
    if (gestureDetector.onTouchEvent(event)) {
        return true;
    }
    return super.onTouchEvent(event);
}
private void onLeftSwipe() {
// Do something
    System.out.println("left swipe");
}

private void onRightSwipe() {
// Do something
    System.out.println("right swipe");
}

// Private class for gestures
private class SwipeGestureDetector extends SimpleOnGestureListener {
    // Swipe properties, you can change it to make the swipe
    // longer or shorter and speed
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 200;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        try {
            float diffAbs = Math.abs(e1.getY() - e2.getY());
            float diff = e1.getX() - e2.getX();

            if (diffAbs > SWIPE_MAX_OFF_PATH)
            return false;

            // Left swipe
            if (diff > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                SlidingMenuActivity.this.onLeftSwipe();

                // Right swipe
            } else if (-diff > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                SlidingMenuActivity.this.onRightSwipe();
            }
        } catch (Exception e) {
            Log.e("YourActivity", "Error on gestures");
        }
        return false;
    }
}

I have that but its not working, I am not able to detect swiping left or right. I have declared private GestureDetector gestureDetector; on top of my class, and in onCreate method of activity.

gestureDetector = new GestureDetector(new SwipeGestureDetector());

But its not working, nothing is printed? I'm using kitkat nexus 4.


回答1:


You must apply the listener to the View you like it to work on, something like:

myView.setOnTouchListener(new SwipeGestureDetector(this)....



回答2:


Try updated code

public class OnSwipeTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector;
int SWIPE_THRESHOLD = 200;
int SWIPE_VELOCITY_THRESHOLD = 200;

public OnSwipeTouchListener(Context context) {
    gestureDetector = new GestureDetector(context, new CustomGestureListenerClass());
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
}

private final class CustomGestureListenerClass extends SimpleOnGestureListener {


    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        singleClicked(e);
        return super.onSingleTapUp(e);
    }

    @Override
    public boolean onScroll(MotionEvent startMotionEvent, MotionEvent endMotionEvent, float distanceX, float distanceY) {
        return super.onScroll(startMotionEvent, endMotionEvent, distanceX, distanceY);
    }


    @Override
    public boolean onFling(MotionEvent startMotionEvent, MotionEvent endMotionEvent, float velocityX, float velocityY) {

        boolean result = false;
        try {
            float diffY = endMotionEvent.getY() - startMotionEvent.getY();
            float diffX = endMotionEvent.getX() - startMotionEvent.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;
            }
            result = true;
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

public void onSwipeRight() {
    // if you want done some portion before call this method then write here
}

public void onSwipeLeft() {
    // if you want done some portion before call this method then write here
}

public void singleClicked(MotionEvent e) {
}
}



回答3:


create OnSwipeTouchListener.java as external class for reusabale and other behavior as

 public class OnSwipeTouchListener implements View.OnTouchListener {

 private final GestureDetector gestureDetector;
 Context context;

 public OnSwipeTouchListener(Context ctx,View mainView) {
gestureDetector = new GestureDetector(ctx, new GestureListener());
mainView.setOnTouchListener(this);
context = ctx;
 }

 @Override
 public boolean onTouch(View v, MotionEvent event) {
     return gestureDetector.onTouchEvent(event);
 }

 public 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() - e1.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 exception) {
        exception.printStackTrace();
    }
    return result;
     }
 }

 public void onSwipeRight() {
Toast.makeText(context, "Right", Toast.LENGTH_SHORT).show();
this.onSwipe.swipeRight();
 }

 public void onSwipeLeft() {
Toast.makeText(context, "Left", Toast.LENGTH_SHORT).show();
this.onSwipe.swipeLeft();
 }

 public void onSwipeTop() {
 }

 public void onSwipeBottom() {
 }

 interface onSwipeListener {
void swipeRight();

void swipeLeft();
 }

 ;
 onSwipeListener onSwipe;

 public void setOnSwipeListener(onSwipeListener onSwipeListener) {
this.onSwipe = onSwipeListener;
 }
 }

and in your main activity use it

  OnSwipeTouchListener onSwipeTouchListener = new OnSwipeTouchListener(this,           findViewById(R.id.main_view));
     onSwipeTouchListener.setOnSwipeListener(new OnSwipeTouchListener.onSwipeListener() {
         @Override
    public void swipeRight() {

    }

    @Override
    public void swipeLeft() {

    }
     });

in your activity_main layout set an id to your main layout like main_view

have nice coding ...



来源:https://stackoverflow.com/questions/23158750/swipt-detector-gestures-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!