GridView onTouch

Posted by Jeff on Stack Overflow See other posts from Stack Overflow or by Jeff
Published on 2010-05-13T14:32:50Z Indexed on 2010/05/13 14:34 UTC
Read the original article Hit count: 338

Filed under:

I am trying to set up a GridView of buttons that responds to onTouch. For example, if I swipe my finger across the screen horizontally, vertically or diagonally then I want the buttons which were touched to be selected. I tried setting OnTouchListener's for the buttons, but this didn't work, only the first button in the drag event received the onTouch events.

Next I tried creating an OnTouchListener for the GridView and then using the X,Y coords to determine which buttons where touched. This doesn't seem to work either since the MOTION_DOWN events are only passed to the GridView's onTouch() if I start the drag on the very edge of the GridView. In fact if I drag my finger horizontally across the grid, the onTouch events aren't fired at all unless I start the drag at the edge of the GridView. Does anyone know of a better way to do this?

Thanks in advance.

gridview.setOnTouchListener(new View.OnTouchListener()
{
 @Override
 public boolean onTouch(@SuppressWarnings("unused") View view, MotionEvent event)
 {
  float X = event.getX();
  float Y = event.getY();

  switch (event.getAction()) { 
  case MotionEvent.ACTION_DOWN:
   System.out.println("Down: " + X + "," + Y);
   break;
  case MotionEvent.ACTION_MOVE:
   System.out.println("Move: " + X + "," + Y);
   break;
  case MotionEvent.ACTION_UP: 
   System.out.println("Up: " + X + "," + Y);
   break; 
  }

  return true;
 }
}

© Stack Overflow or respective owner

Related posts about android