Detecting gestures on multiple views in Android
I'm making a custom in-app keyboard. The keyboard is a ViewGroup
containing many key views. Each key needs to recognize gestures like click, long press, and swipe/fling. How do I add gesture detection to all the keys?
The SimpleOnGestureListener
methods do not pass in the view that triggered the MotionEvent
, so there is some difficulty in knowing which key was pressed if a single gesture detector is used for all the keys.
As I see it, there are three options:
Have the keyboard view add a new gesture detector/listener for every key (or have each key view implement its own gesture detector).
Possible problem: Not resource efficient? User @pskink says in this answer (although regarding a somewhat different topic than I am talking about here):
Implement GestureDetector in parent not in child view, otherwise you would need n detectors if parent has n child views.
Have the keyboard add a single OnTouchListener to each key. Then write my gesture detection code myself.
Problem: This is reinventing the wheel.
Put a GestureDetector
class implementation inside the OnTouchListener
so that the gesture listener has access to a member variable referencing the view that calls the OnTouchListener
. Here is the answer that shows this.
Possible problem: It seems like having a single GestureDetector for multiple views should be a common situation. If that is so, then why could I only find a single obscure answer describing how to do it? I also don't know enough about memory management if such a practice would be a problem.
A note about GestureOverlayView
:
The only answer to the question Set GestureDetector to all child views tersely suggests:
You can use a GestureOverlayView see a previus stackoverflow post on how to use it.
Although the documentation for GestureOverlayView
is poor, from what I can gather, it is more for recognizing custom gestures (see here), not for adding the same gesture set to multiple views.
上一篇: 与openssl签署的证书?
下一篇: 在Android中检测多个视图上的手势