Create Simple gesture for swipe left to right and

2019-04-02 00:36发布

问题:

I want to create simple swipe gestures between two activity ,i have searched a lot got something like below.But my doubt is how could i swipe the activity or view using swipe gesture!!

class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                // right to left swipe
                if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    Toast.makeText(SelectFilterActivity.this, "Left Swipe", Toast.LENGTH_SHORT).show();
                }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    Toast.makeText(SelectFilterActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }

Hope anybody will help me out @Thanks!

回答1:

Try this code now..

page = (LinearLayout) findViewById(R.id.flipper);

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    super.dispatchTouchEvent(event);
    return gestureDetector.onTouchEvent(event);
}

SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener() {

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {

        float sensitvity = 50;
        if ((e1.getX() - e2.getX()) > sensitvity) {
            SwipeLeft();
        } else if ((e2.getX() - e1.getX()) > sensitvity) {
            SwipeRight();
        }

        return true;
    }

};

 GestureDetector gestureDetector = new GestureDetector(
        simpleOnGestureListener);



private void SwipeLeft() {

     page.setInAnimation(animFlipInForeward);
      page.setOutAnimation(animFlipOutForeward);
      page.showNext();

}


 private void SwipeRight() { 

     page.setInAnimation(animFlipInBackward);
     page.setOutAnimation(animFlipOutBackward);
     page.showPrevious();

}


回答2:

implement your activity with OnGestureListener

 yourview.setOnTouchListener(touchListener);

implement touchListener as shown below

 OnTouchListener touchListener = new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                gestureScanner.onTouchEvent(event);
                return false;
            }
        };      

onFling method for swipe.

  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                    float velocityY) {
                try {
//                  if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
//                      return false;
                    // right to left swipe
                     if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE  && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                         viewFlipper.setInAnimation(animFlipInPrevious);
                         viewFlipper.setOutAnimation(animFlipOutPrevious);
                         Globle.falg =false;
                    if (count != 2) {
                        if (count == 2) {
                            count = 0;
                        } else {
                            count = count + 1;
                        }
                        if (currentView == 0) {
                            viewFlipper.showNext();
                            currentView = 1;
                        } else if (currentView == 1) {
                            viewFlipper.showNext();
                            currentView = 2;
                        } else {
                            currentView = 0;
                            viewFlipper.showNext();
                        }
                    }
                    count = viewFlipper.getDisplayedChild();
                    } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE  && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                        viewFlipper.setInAnimation(animFlipInNext);
                        viewFlipper.setOutAnimation(animFlipOutNext);
                        Globle.falg =false;
                    if (count != 0) {
                        if (count == 0) {
                            count = 2;
                        } else {
                            count = count - 1;
                        }
                        if (currentView == 2) {
                            currentView = 1;
                            viewFlipper.showPrevious();
                        } else if (currentView == 1) {
                            currentView = 0;
                            viewFlipper.showPrevious();
                        } else {
                            currentView = 2;
                            viewFlipper.showPrevious();
                        }
                    }
                    count = viewFlipper.getDisplayedChild();
                }
                } catch (Exception e) {
                    // nothing
                }
                return false;
            }
        }

as above code you can change your view and here in my case there is three view but you need only two so change above count value. hop this is helpful to you.



回答3:

public class Main extends Activity implements OnGestureListener {

    private GestureDetector gDetector;
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        gDetector = new GestureDetector(this);
    }


    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        try {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
                return false;
            }
            // right to left swipe
            if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                ((ImageView) findViewById(R.id.image_place_holder))
                        .setImageResource(R.drawable.down);

            }
            // left to right swipe
            else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                    && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                ((ImageView) findViewById(R.id.image_place_holder))
                        .setImageResource(R.drawable.up);

            }
        } catch (Exception e) {

        }
        return false;
    }

}


回答4:

see this link it might be helpfull to you.

gestures detector

or you have to use viewpager in your app.

for the viewpager the following link will help you

view-pager-example