Android - JavaScript: touchstart event does not fire before scaling or scrolling a page

In my Android application, the user can view some HTML pages using the ViewPager, and the user can touch the item to be highlighted.

The problem is trying to get a touch event using javascript using the following code, elementFromPoint returns null when going to a new page, but after the user zooms in or scrolls the page, it works correctly.

I found that the touchstart event register occurs after zooming or scrolling the page. so it works right after that, although it is registered at $ (document) .ready ()

$(document).ready(function(){ document.addEventListener("touchstart", touchstart, false); }); function touchstart(e) { var x = e.targetTouches[0].clientX; var y = e.targetTouches[0].clientY; el = document.elementFromPoint(x, y); } 

thanks

+6
source share
2 answers

write the following code in your java code after calling javascript:

  myWebview.scrollTo(1, 0); myWebview.scrollTo(0, 0); 

or use scaling and then zoom out

  myWebview.zoomIn(); myWebview.zoomOut(); 
+7
source

using Mohamed Abdel Latif's solution (obviously this is another lame WebView bug), this is what fixed for me on Android 4.1.2. Note. I tested this on Android 4.4.2 and this hack-to-fix is ​​not needed.

 @Override public void onCreate(Bundle savedInstanceState) { final WebView myWebView = (WebView) findViewById(R.id.mywebview); myWebView.setHorizontalScrollBarEnabled(false); myWebView.getSettings().setJavaScriptEnabled(true); myWebView.loadDataWithBaseURL("file:///android_asset/", YOUR_HTML_GOES_HERE, "text/html", "utf-8", null); myWebView.setWebViewClient(new WebViewClient() { // overcome the ontouchstart registration bug ! @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); final WebView myWebView = (WebView) findViewById(R.id.mywebview); myWebview.scrollTo(1, 0); myWebview.scrollTo(0, 0); } }); } 
0
source

All Articles