Estoy intentando utilizar gestos dentro de un fragmento; Tengo lo siguiente dentro de FragmentActivity que maneja mi fragmento de detalles. Lo que intento que ocurra es cuando se detecta un deslizamiento en la vista para reemplazar los datos dentro de esa vista con la entrada anterior o siguiente.Fragmento de Android onCreateView con gestos
Si hay una mejor manera de manejar esto; Estoy totalmente de acuerdo. Sin embargo, lo que está sucediendo aquí es que el método onFling en realidad nunca se llama.
public static class DetailsFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
View v = inflater.inflate(R.layout.my_view, null, false);
final GestureDetector gesture = new GestureDetector(getActivity(),
new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
final int SWIPE_MIN_DISTANCE = 120;
final int SWIPE_MAX_OFF_PATH = 250;
final int SWIPE_THRESHOLD_VELOCITY = 200;
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.i(Constants.APP_TAG, "Right to Left");
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Log.i(Constants.APP_TAG, "Left to Right");
titles.showDetails(getPosition() - 1);
}
} catch (Exception e) {
// nothing
}
return super.onFling(e1, e2, velocityX, velocityY);
}
});
v.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return gesture.onTouchEvent(event);
}
});
return v;
}
}
solo curiosidad: ¿por qué no se pierde la referencia al gesto? –
@LemLordjeKo ¿por qué se va a perder la referencia a 'gesture'? 'gesture' se mantiene como referencia en el' View.OnTouchListener' que creamos, y se mantiene como referencia en la vista en sí, 'v'. Java es lo suficientemente maduro, si hay un camino hacia un objeto, es casi seguro que no se obtendrá GC. – bclymer
Se necesitan las siguientes importaciones para que el fragmento de código anterior funcione: import android.view.View.OnTouchListener; import android.view.MotionEvent; import android.view.GestureDetector; import android.util.Log; constantes de clase { public static final Cadena APP_TAG = "mytag"; } – FuzzyAmi