martes, 8 de noviembre de 2011

Procesando Eventos de un sólo Toque

Se hace con interfaces de escuchadores, que es como Android nos informa de que ha ocurrido un toque.
Los eventos de toque son pasados mediante la implementación de un interfaz OnTouchListener que debemos subscribir a un View.
  android.view.View.OnTouchListener
 Class Overview
 Interface definition for a callback to be invoked when a touch      event     is dispatched to this view. The callback will be invoked before the touch event is given to the view.
 Summary
 Public Methods
  abstract boolean onTouch(View v, MotionEvent event)
  Called when a touch event is dispatched to a view.
 Public Methods
  public abstract boolean onTouch (View v, MotionEvent event)
       
Called when a touch event is dispatched to a view. This allows listeners to get a chance to respond before the target view.
  Parameters
  v     The view the touch event has been dispatched to.
  event     The MotionEvent object containing full information about the event.
  Returns
   True if the listener has consumed the event, false otherwise.


El interfaz OnTouchListener  sólo tiene un método:
  public abstract boolean onTouch (View v, MotionEvent event)
El primer argumento es la Vista "View" a la cual se envian los eventos de toque.
El segundo, el MotionEvent es el que tiene la miga.

¿Como subscribimos la vista al OnTouchListener()?
Con el método View.setOnTouchListener
 public void setOnTouchListener (View.OnTouchListener l)
 Register a callback to be invoked when a touch event is sent to this view.
 Parameters
  the touch listener to attach to this view

El OnTouchListener será llamado antes de que sea mandado a la Vista en sí.  Lo que
haremos será procesarlo nosotros segun nos interese y luego avisar a la View de que hemos
recibido un onTouch. Devolviendo "true" al final del método para decirle que la hemos procesado
dentro del método o "false" para decirle al View que no la hemos procesado y debe hacerlo ella.

La instancia de MotionEvent tiene tres métodos que nos interesan:

MotionEvent.getX() y MotionEvent.getY(): las coordenadas x e y en las que se ha pulsado  relativas a la
view en la que estamos. Devuelve un float y la medida en es pixeles.
 These methods report
MotionEvent.getAction(): El tipo del evento toque
            MotionEvent.ACTION_DOWN
            MotionEvent.ACTION_MOVE
            MotionEvent.ACTION_CANCEL este no se da nunca
            MotionEvent.ACTION_UP

public class SingleTouchTest extends Activity implements OnTouchListener
{
    public void onCreate(Bundle savedInstanceState)
    {
    TextView textView = new TextView(this);
    textView.setOnTouchListener(this);
    setContentView(textView);
    }

    @Override public boolean onTouch(View v, MotionEvent event)
    {
        switch (event.getAction())
        {
                case MotionEvent.ACTION_DOWN:
                {};
                break;
                case MotionEvent.ACTION_MOVE:
                {};
                break;
                case MotionEvent.ACTION_CANCEL:
                {};
                break;
                case MotionEvent.ACTION_UP:
                {};
                break;
        }
        return true;
        }
}


2 comentarios:

  1. Hola! Lo primero, buen blog ^^
    Se que es de hace tiempo pero tengo una duda.
    Estoy haciendo una aplicación con lidgdx es como una enciclopedia y no consigo encontrar como hacer scroll en una lista de texture.
    He intentado buscar aquí y en otras pero la verdad no consigo nada...Podríais ayudarme por favor?
    Gracias de antemano

    ResponderEliminar
  2. Lo siento pero hace ya tiempo que no toco nada de android. Podrías buscar algun código parecido.
    No sé si esto te servirá.

    http://code.google.com/p/android-ui-utils/downloads/detail?name=CarouselExample.zip

    ResponderEliminar