Search code examples
androidwebviewtouchlistener

Is there anyway to remove an onTouchListener from a view object?


I have an on touch listener for a webview, but it has a bad effect on the functionality of the webview, so I am wondering if there is anyway to removed the on touch listener after the initial interaction?


Solution

  • So in you activity you would set your overridden onTouchListener:

                mWebView.setOnTouchListener(new View.OnTouchListener() {
    
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    v.setOnTouchListener(mWebView.mOnTouchListener);
                    return false;
                }
            });
    

    And you would have to make a new class, extending WebView. And within it you would define an OnTouchListener.

        public final OnTouchListener mOnTouchListener = new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent rawEvent) {
            return false;
        }
    }; 
    

    Setting the ontouchlistener to null doesn't reset it to the default definition. You still have to provide an actual listener.