In this tutorial explain how to open new Activity when swipe this listview possition right to left .
i have earlier implement the swipe gesture detector code, this code get the X,Y pixel point when onTouch finger.
class SwipeGestureListener extends SimpleOnGestureListener implements
OnTouchListener {
Context context;
GestureDetector gestureDetector;
static final int SWIPE_MIN_DISTANCE = 120;
static final int SWIPE_MAX_OFF_PATH = 250;
static final int SWIPE_THRESHOLD_VELOCITY = 200;
public SwipeGestureListener() {
super();
}
public SwipeGestureListener(Context context) {
this(context, null);
}
public SwipeGestureListener(Context context, GestureDetector gestureDetector) {
if (gestureDetector == null)
gestureDetector = new GestureDetector(context, this);
this.context = context;
this.gestureDetector = gestureDetector;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
final int position = tutorials.pointToPosition(
Math.round(e1.getX()), Math.round(e1.getY()));
String tutorialName = (String) tutorials.getItemAtPosition(position);
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
if (Math.abs(e1.getX() - e2.getX()) > SWIPE_MAX_OFF_PATH
|| Math.abs(velocityY) < SWIPE_THRESHOLD_VELOCITY) {
return false;
}
if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE) {
Toast.makeText(MainActivity.this, "bottomToTop" + tutorialName,
Toast.LENGTH_SHORT).show();
} else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE) {
Toast.makeText(MainActivity.this,
"topToBottom " + tutorialName, Toast.LENGTH_SHORT)
.show();
}
} else {
if (Math.abs(velocityX) < SWIPE_THRESHOLD_VELOCITY) {
return false;
}
// open new activity
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE) {
Toast.makeText(MainActivity.this, "swipe RightToLeft " + tutorialName, 5000).show();
Intent intent=new Intent(MainActivity.this,second.class);
startActivity(intent);
}
else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE) {
Toast.makeText(MainActivity.this,
"swipe LeftToright " + tutorialName, 5000).show();
}
}
return super.onFling(e1, e2, velocityX, velocityY);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
public GestureDetector getDetector() {
return gestureDetector;
}
}
................................................................................................................................................................
learn More
http://androidbeginnerpoint.blogspot.in/2015/12/swipegesture-inside-listview-in-android.html
0 comments