开发者

WebView onTouch handling when the user does NOT click a link

I am working on an Android app which displays content in a WebView. The content contains links, and those links are being handled via a custom WebViewClient and use of javascript and shouldOverrideUrlLoading() method to follow the link depending on what the link is.

Here's the problem:

I want to handle clicks different depending on where on the WebView the clic开发者_如何学运维k happens. In particular, I want to do some UI changes any time the user clicks on a WebView, but does NOT click on the link.

I have found that the onTouch method completes and finishes (including the super.onTouch) before the shouldOverrideUrlLoading() method is called, and I can't find any indication of a method that runs on the WebView post-shouldOverrideUrlLoading(). I am also using a GestureDetector, and again, it's onSingleTapUp() method completes before the shouldOverrideUrlLoading() code is fired.

There has to be some way to notice when a click happens on the WebView whether or not the shouldOverrideUrlLoading() code has run before finishing the event.

Anyone have any tips on where to look? Anyone know an execution point that is called on the WebView after shouldOverrideUrlLoading() code has been executed?

Thanks, Dale.


I think you should be able to use the onTouch(), inside your there you can check to see if it was a link that was clicked by using:

HitTestResult result = wv.getHitTestResult();
url = result.getExtra();

If it was a url then return false from your onTouch() so that it gets passed through to shouldOverrideUrlLoading(); If it wasn't a url then do whatever you want to with it and return true so the system knows that you consumed the event.


For those fighting the same fight, here is the sample code that solved this issue for me.

private GestureDetector.SimpleOnGestureListener _simpleOnGestureListener = new GestureDetector.SimpleOnGestureListener()
{    
        /* ... */

        @Override
        public boolean onSingleTapUp(MotionEvent e)
        {
            HitTestResult htResult = getHitTestResult();
            if (htResult != null && htResult.getExtra() != null)
            {
                // The click was on a link! Return true so to bypass processing.
                return true;
            }

            /**
             * 
             * Do your stuff
             * 
             */

            return super.onSingleTapUp(e);
        }
}


You could add javascript onclick code to the outer most element of the html that calls back to your activity.

In your Activity you'd need:

private void initializeClickableWebView( WebView webView, String innerHtml ) {
    webView.getSettings().setJavaScriptEnabled(true);
    webView.addJavascriptInterface(this, "jsBridge");

    String html = "<html><body><div onclick=\"window.jsBridge.processClick();\">" +
        innerHtml +
        "</div></body></html>";

    webView.loadDataWithBaseURL("file:///android_asset/", html,
            "text/html", "utf-8", null);
}

private void processClick() {
    Log.d( "webView", "processClick()" );
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜