To handle gesture events for a UI component set
to receive them, the MIDlet must implement the GestureListener
class. You can define multiple GestureListeners
for a MIDlet.
To handle gesture events:
Create a new
listener class that implements GestureListener
. The
following code snippet creates the framework for the MyGestureListener
custom class.
// import the necessary classes import com.nokia.mid.ui.gestures.GestureEvent; import com.nokia.mid.ui.gestures.GestureInteractiveZone; import com.nokia.mid.ui.gestures.GestureListener; public class MyGestureListener implements GestureListener { // implement gestureAction (see step 2 of this example) } // end class MyGestureListener
Define the gestureAction
method for the class. This
method is called every time a gesture event occurs for a UI component
that uses MyGestureListener
. The method is called
with all the information related to the gesture event. The following
code snippet creates the framework for a gestureAction
implementation.
public void gestureAction(Object container, GestureInteractiveZone gestureInteractiveZone, GestureEvent gestureEvent) { // handle received gesture events (see step 3 of this example) } // end gestureAction()
The gestureAction
method is called with the following parameters detailing the gesture
event:
Parameter |
Description |
Type |
---|---|---|
|
The UI component for which the gesture event occurred. This
is either a |
|
|
The |
|
|
The gesture event that occurred for The
The method returns one of
the gesture event constants defined in the For more information about the methods supported for each type, see the following table. |
Handle the gesture
events returned by the gestureAction
method. Use
the GestureEvent.getType
method to determine the
gesture event type and then call the appropriate methods to retrieve
the event details (see the following table). You can handle the gesture
events either directly in the gestureAction
method,
or you can use the gestureAction
method to call separately
defined handler methods or classes. For information about optimizing
the gestureAction
method for performance, see Using the gestureAction method correctly.
Gesture event type |
Supported methods |
Description |
---|---|---|
|
|
The |
|
|
The For a For a |
|
|
The The The |
The following code snippet handles received gesture events
by first determining their type and then calling the corresponding
getter methods to store the event details in local variables. In this
example, the GestureInteractiveZone
only receives
taps and long taps, so GESTURE_TAP
and GESTURE_LONG_PRESS
are the only gesture events that need handling.
// retrieve the tap or long tap details and store them switch (gestureEvent.getType()) { case GestureInteractiveZone.GESTURE_TAP: case GestureInteractiveZone.GESTURE_LONG_PRESS: try { int startLocationX = gestureEvent.getStartX(); int startLocationY = gestureEvent.getStartY(); } catch (IllegalStateException e) { ; } break; }
The gestureAction
method is called from the MIDlet UI thread. This single thread is
shared by all UI notifications, key and pointer events, command actions,
and other UI operations. To maximize performance, handle any time-consuming
or resource-intensive gesture operations in a separate thread. If you program the MIDlet to perform lengthy operations
directly in the gestureAction
method, and thus in
the MIDlet UI thread, these operations can block other UI events and
operations and cause the user interface to become unresponsive.
During a drag gesture, the platform can generate a large number
of drag event notifications and thus a large number of calls to the gestureAction
method. To prevent the Java heap from filling
up and fragmenting with multiple GestureEvent
instances,
the platform recycles the same GestureEvent
instance
for each call. The GestureEvent
values are therefore
valid only during the current gestureAction
call,
since each call overwrites the single GestureEvent
instance with its own values. If you access a copy of the GestureEvent
instance from outside the gestureAction
method, and thus outside the MIDlet UI thread, the returned values
are those from the latest gestureAction
call. The
values are not from the call during which the copy was originally
created. If you need to use the GestureEvent
values
for a specific gesture event outside the gestureAction
method, copy the values from GestureEvent
instance
before returning from the gestureAction
method.