I have a project that is based on a tab host and uses fragments,
on one tab it haves a listFragment, user taps a cell and goes to a different fragment,
from there user can swipe between fragments of that tab,
i have the project working until i go to the cell of the listFragment,
there i need to detect the swipes left or right to change the fragments accordingly,
here my code:
public class PrepareSkin extends Fragment implements OnGestureListener{
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;
private GestureDetector gestureScanner;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
gestureScanner = new GestureDetector(this);
// TODO Auto-generated method stub
View myFragmentView = inflater.inflate(R.layout.prepare_skin, container, false);
Log.i("Mirko", "CREATO <<");
return myFragmentView;
}
public boolean onTouchEvent(MotionEvent me)
{
return gestureScanner.onTouchEvent(me);
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@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) {
Log.i("Mirko", "LEFTERS <<");
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.i("Mirko", "rIGTHERS >>");
}
else if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
} else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
}
} catch (Exception e) {
// nothing
}
return true;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
}
so why is my gesture recognizer not working?
thanks,
This link seems up your alley : Android Fragment onCreateView with Gestures
Although they do not implement OnGestureListener
into their class, but rather attach a clickListener to their whole View and then trigger the Gesture Listener that way, it should still apply to your snippet of code.
Another quick fix may be that you return onDown()
as false - perhaps you should return it as true. I know that Android won't recognize an onUp()
if onDown()
doesn't return true
prior.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With