1

when deriving a class from android.view.View, I see 3 possibilities to handle touch events:

  1. use the setOnTouchListener method to add a listener instance
  2. override the View.onTouchEvent method
  3. implement OnTouchListener in derived View

    class ExtendedView extends View {
        public ExtendedView(Context context) {
            super(context);
    
            // 1. set a listener
            setOnTouchListener(new OnTouchListener() {
    
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    // TODO Auto-generated method stub
                    return false;
                }
            });
        }
    
        // 2. override onTouchEvent
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            // TODO Auto-generated method stub
            return super.onTouchEvent(event);
        }
    }
    
    
    // 3. implement and use OnTouchListener
    class ExtendedView3 extends View implements OnTouchListener {
    
        public ExtendedView3(Context context) {
            super(context);
            setOnTouchListener(this);
        }
    
        // not override this 
        // public boolean onTouchEvent() {...}
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            return false;
        }
    }
    

These alternatives seems to serve the same purpose. The only obvious difference is the extra View parameter, when using OnTouchListener - so one could listen to different views (in this case a non anonymous listener would be used). But this is not needed in my scenario; I just want to listen to the events of the instance of my ExtendedView.

I use - with some success - alternative 2, override onTouchEvent. Overriding the existing method seems to me the natural and simplest way to accomplish the task.

The reason why I'm here: I've see code in which a View derived class uses setOnTouchListener to set an extra listener, instead of overriding onTouchEvent.

So my questions are:

  1. do I miss something?
  2. is alternative 1 (and 3) appropriate, when deriving from a View?

Extra Bonus question: View does NOT implement the OnTouchListener interface, but has the onTouchEvent method. Why? Wouldn't it be more coherent for the View to implement this interface instead of having the onTouchEvent method?


PS: I found a discussion about this issue:

Where should I implement the Android onTouchListener?

Frankly, I don't understand the accepted answer: "edthethird" wrote about overriding onTouchEvent:

There is only 1 extra class instantiated ...

which extra class - I don't see one.

And in the onTouchEvent implementation he compares an event with a view - this doesn't make much sense for me. Additionally he assumes that different views will call onTouchEvent - but - if I understand the override approach right - this will NOT happen.

--

"BeatingToADifferentRobot" wrote "... setOnTouchListener [..] provides more flexibility and follows a general java pattern"

I can't follow this. What more flexibility could that be? I can always derive from the class and override the method.

And overriding a method is - hopefully - nothing "not following a general java pattern"

4

0 回答 0