Where should I implement the Android onTouchListen

2019-07-02 01:40发布

问题:

I am new to Android development and at the moment I must make a decision of where to control the gesture input, but I just don't know the pros and cons of where to grab the touch input. I have been given two alternatives, can you please explain to me the pros and cons of either way?

Set listener on View; implement methods:

faceView.setOnTouchListener(new OnTouchListener(){
   public boolean onTouch(View arg0, MotionEvent arg1){
     return false;
   }
);

or

Override method within View

public boolean onTouchEvent(MotionEvent event){
   return false;
}

回答1:

APPROACH 1: Anonymous Inner Type.

faceView.setOnTouchListener(new OnTouchListener(){
   public boolean onTouch(View arg0, MotionEvent arg1){
     return false;
   } 
);

This is Anonymous, because it is a declaration without a name. There is no OnTouchListener mytouch =, there is only the declaration new OnTouchListener(). This is inner, because it is inside another class, and it is a type because it is an implementation of an interface.

OK, so with that in mind. This approach is more convenient for developers. Functionality is isolated (generally by the declaration of the listenerable) and this is easily managed. But this convenience comes with a cost. Assume you have buttons, with ten onClickListeners. With this approach, the garbage collection queue will fill up faster with each new anonymous inner type.

But does it matter? Not really. If you are trying to get every microsecond of performance out of your system, then sure don't use this approach. But generally, this is viable.

APPROACH 2: Single Inheritance

public boolean onTouchEvent(MotionEvent event){
   return false;
} 

Assuming you read the above, this approach has a much lower cost for creation and cleanup. There is only 1 extra class instantiated, and only 1 object added to the GC queue.

This is the approach I use, and have used for a while. It's also what I see in Google's sample source code.

But it's not perfect! The implementation of onTouchEvent will end up looking as such:

public void onTouch(Event e){
if (e.equals(View1)){
}else if (e.equals(View2)){
}else if (e.equals(View 3)){
...}

SO

Honestly, it doesn't matter. If you want the highest performance possible, use a static onTouchEvent listener, and you won't have to pay the above mentioned costs, but for the most part they are not that expensive anyways.



回答2:

I would personally use the listener, just use that in your onCreate method, should be fine for now



回答3:

In most cases, you'll likely be using the setOnTouchListener as it provides more flexibility and follows a general java pattern. Normally, overriding onTouchEvent would only be used for touchevents that would always occur within a View and touchevents that would not change. You would also have to define the View class to override onTouchEvent. When using something like a button, it wouldn't make sense to have to implement your own button class just to handle the click event!

faceView.setOnTouchListener

Pro

  • Allows for easy customization
  • Follows a generally accepted java pattern

Cons

  • Could result in unnecessary setters if the behavior does not change.

Override onTouchEvent Pro

  • Encapsulates the functionality in the base class

Cons

  • Does not allow for customization