Handling Touch Events In Android When Using Multiple Views/layouts
I'm very new to Android programming, and trying to understand touch events with nested views. To start, here's a description of my app: I have a relative layout that I've added via
Solution 1:
The way touch events are handled is kind of a cascading effect that starts from the top view and goes down to the lower nested views. Basically, Android will pass the event to each view until true
is returned.
The general way you could implement the onTouchEvent
event of a View
would be:
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean actionHandled = false;
final int action = event.getAction();
switch(action & MotionEventCompat.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
// user first touches view with pointer
break;
case MotionEvent.ACTION_MOVE:
// user is still touching view and moving pointer around
break;
case MotionEvent.ACTION_UP:
// user lifts pointer
break;
}
// if the action was not handled by this touch, send it to other views
if (!actionHandled)
actionHandled |= super.onTouch(v, MotionEvent.event);
return actionHandled;
}
Post a Comment for "Handling Touch Events In Android When Using Multiple Views/layouts"