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!
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();
}
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.
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;
}
}
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