Android: Zoompicker Breaks Ontouchlistener
Solution 1:
So after running into the same problem myself, I tried to make sense of some of these answers to no avail. The problem at hand is that once the user invokes scroll (not pinch-zoom) (at least for me...), the WebView instance loses its OnTouchListener reference. How did I figure this out? Well...
Webview inherits dispatchTouchEvent() from View. dispatchTouchEvent calls onTouch() (which is the function not firing that should)
The reason why onTouch() wasn't getting called was, as I said before, that the WebView instance's OnTouchListener was getting set to null for some reason. This can be seen by putting a breakpoint in View's dispatchTouchEvent() method
So to solve this, we extend WebView like so:
import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebView;
publicclassTouchWebViewextendsWebView {
WebTouchListener wtl;
publicTouchWebView(Context context) {
super(context);
}
publicTouchWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
publicTouchWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
publicvoidresetTouchListener() {
if(this.wtl == null) {
this.wtl = newWebTouchListener();
}
this.setOnTouchListener(wtl);
}
@OverridepublicbooleandispatchTouchEvent(MotionEvent event) {
this.resetTouchListener();
returnsuper.dispatchTouchEvent(event);
}
}
And then we implement our OnTouchListener:
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.webkit.WebView;
publicclassWebTouchListenerimplementsOnTouchListener {
@OverridepublicbooleanonTouch(View v, MotionEvent event) {
WebView.HitTestResulthr= ((TouchWebView)v).getHitTestResult();
//Log.i(TAG, "getExtra = "+ hr.getExtra() + "\t\t Type=" + hr.getType());if (event.getAction() == MotionEvent.ACTION_DOWN) {
System.out.println(hr.getExtra() + " " + hr.getType());
}
// TODO Auto-generated method stubreturnfalse;
}
}
OnTouch will now fire even after zooming or scrolling
Solution 2:
I had the same problem, but managed to solve it by setting the listener again in my extended WebView class:
public void invalidate() {
super.invalidate();
setOnTouchListener(this);
}
Unfortunately, now I have the opposite problem in that the zoom controls (the plus/minus widget) does no longer receive touch events. There seems to be some exclusivity between having an OnTouchListener and zoom, at least in Android SDK level 8 (Froyo 2.2).
This sounds like a bug to me, but would love to find a workaround.
Solution 3:
The workaround is pretty simple. You have to create a custom class, that extends one of the ViewGroup subclasses like LinearLayout. Then override onInterceptTouchEvent - it will be called prior to ViewGroup child's onTouchEvent. It is not very elegant to put a webview control into your custom ViewGroup subclass, and then insert it into activity's view hierarchy - where it is desired, but - it works.
privateclassOwnedLayoutextendsLinearLayout
{
protectedOwnedLayout(Context context, AttributeSet attrs)
{
super(context, attrs);
}
protectedOwnedLayout(Context context)
{
super(context);
}
@OverridepublicbooleanonInterceptTouchEvent(MotionEvent ev)
{
//Put Your code herereturnsuper.onInterceptTouchEvent(ev);
}
}
Solution 4:
Try
youWebView.getSettings().setBuiltInZoomControls(false);
It's work for me.
Solution 5:
A dirty solution is to cover an invisible view on top of your web view and detects touches there, and you have to distinguish if the touch is consumed, or return false to pass touches to underlying web view.
Post a Comment for "Android: Zoompicker Breaks Ontouchlistener"