检查Android WebView是否正在使用触摸事件 [英] Check if Android WebView is consuming touch events
问题描述
如何检测Android WebView是否消耗了触摸事件? onTouchEvent
始终返回true,并且永远不会触发 WebViewClient
的 onUnhandledInputEvent
.
How can I detect whether Android WebView consumed a touch event? onTouchEvent
always returns true and WebViewClient
's onUnhandledInputEvent
is never triggered.
我在TwoDScrollView中有多个WebView.顾名思义,TwoDScrollView可以垂直和水平滚动.TwoDScrollView的内容可以放大/缩小.当用户拖动手指或使用缩小缩放时,我想将touch事件调度到:
I have multiple WebViews inside a TwoDScrollView. As its name suggests, the TwoDScrollView can be scrolled both vertically and horizontally. The contents of TwoDScrollView can be zoomed in / out. When the user drags his finger or uses pinch-to-zoom, I want to dispatch the touch event to:
- WebView,如果其内容是可滚动/缩放的(即仅WebView内部将滚动/缩放)
- 如果上述条件为false,则TwoDScrollView(TwoDScrollView的所有内容都将滚动/缩放)
通过使用 canScrollHorizontally
和 canScrollVertically
方法,我已经部分实现了这一目标.但是这些方法仅适用于本地滚动".但是,在某些情况下,WebView内的某些JavaScript会消耗触摸事件,例如Google Maps.在这种情况下,这些方法返回false.有没有办法找出WebView的内容是否消耗了触摸事件,即是否可滚动/可缩放?我无法更改WebView的内容,因此我的问题不同于这个.
I have partially achieved this by using the canScrollHorizontally
and canScrollVertically
methods. But these methods only work for "native scrolling". However, in some cases, some JavaScript inside the WebView consumes the touch event, for example Google Maps. In this case, the methods return false. Is there any way to find out whether the WebView's contents consumes the touch events, i.e. is scrollable / zoomable? I cannot change the contents of the WebView, therefore my question is different from this one.
我考虑过通过 evaluateJavaScript
方法在Webview中执行一些JavaScript来检查触摸处理程序,但是根据这个答案没有简单的方法可以实现,而且页面上还可以有其他嵌套的iframe.任何帮助将不胜感激.
I have considered checking touch handlers by executing some JavaScript inside the Webview by the evaluateJavaScript
method, but according to this answer there is no easy way to achieve this and also the page can have some other nested iframes. Any help will be appreciated.
- 我覆盖了WebView的
onTouchEvent
并读取了super.onTouchEvent()
,无论如何,该代码始终返回true. 如上所述, -
canScrollHorizontally
和canScrollVertically
仅部分解决了此问题 -
onScrollChanged
也不有用 永远不会触发 -
WebViewClient.onUnhandledInputEvent
- 我考虑过通过
evaluateJavaScript
使用JavaScript,但这是一个非常复杂且丑陋的解决方案 - 我尝试通过
Debug.startMethodTracing
跟踪MotionEvent.我发现它的传播方式如下:-
android.webkit.WebView.onTouchEvent
-
com.android.webview.chromium.WebViewChromium.onTouchEvent
-
com.android.org.chromium.android_webview.AwContents.onTouchEvent
-
com.android.org.chromium.android_webview.AwContents $ AwViewMethodsImpl.onTouchEvent
-
com.android.org.chromium.content.browser.ContentViewCore.onTouchEventImpl
- 根据
-
- I overrode WebView's
onTouchEvent
and readsuper.onTouchEvent()
which always returns true, no matter what. canScrollHorizontally
andcanScrollVertically
only partially solve this problem, as mentioned aboveonScrollChanged
isn't useful eitherWebViewClient.onUnhandledInputEvent
is never triggered- I considered using JavaScript via
evaluateJavaScript
, but it is a very complicated and ugly solution - I tried to trace the MotionEvent by
Debug.startMethodTracing
. I found out it is propagated as follows:android.webkit.WebView.onTouchEvent
com.android.webview.chromium.WebViewChromium.onTouchEvent
com.android.org.chromium.android_webview.AwContents.onTouchEvent
com.android.org.chromium.android_webview.AwContents$AwViewMethodsImpl.onTouchEvent
com.android.org.chromium.content.browser.ContentViewCore.onTouchEventImpl
- According to ContentViewCore's source code the touch event ends up in a native method
nativeOnTouchEvent
and I don't know what further happens with it. Anyway,onTouchEvent
always returns true and even if it was possible to find out somewhere whether the event was consumed or not, it would require using private methods which is also quite ugly.
注意
我不需要知道如何拦截发送到WebView的触摸事件,但是WebView是否正在使用它们,即是否正在使用它们来执行任何操作,例如滚动,拖动等.
Note
I don't need to know how to intercept touch events sent to WebView, but whether the WebView is consuming them, i.e. is using them for doing anything, such as scrolling, dragging etc.
推荐答案
According to this issue report, not possible. If the web code is under your control, you can implement some JavaScriptInterface to workaround this. If not, I am afraid there is no solution here.
这篇关于检查Android WebView是否正在使用触摸事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!