admin管理员组文章数量:1291093
In my Android application, The user can browse some HTML pages using ViewPager, and the user can touch an element to highlight.
the problem is when trying to get the touch event using javascript using the following code, elementFromPoint returns null when navigate to new page, but after the user zoom the page or scroll on it, it works right.
I found that register of the touchstart event happens after zoom or scroll the page. so it works right after that although it is registered on $(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);
}
Thank you
In my Android application, The user can browse some HTML pages using ViewPager, and the user can touch an element to highlight.
the problem is when trying to get the touch event using javascript using the following code, elementFromPoint returns null when navigate to new page, but after the user zoom the page or scroll on it, it works right.
I found that register of the touchstart event happens after zoom or scroll the page. so it works right after that although it is registered on $(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);
}
Thank you
Share Improve this question asked Jan 23, 2013 at 10:33 Ahmed Al KhashabAhmed Al Khashab 3731 gold badge9 silver badges19 bronze badges2 Answers
Reset to default 7write next code in your java code after calling javascript :
myWebview.scrollTo(1, 0);
myWebview.scrollTo(0, 0);
or use zoom in then zoom out
myWebview.zoomIn();
myWebview.zoomOut();
using Mohamed Abdel Latif's solution (obviously this is another lame WebView bug) below is what fixed it for me on Android 4.1.2. Note: I tested this on Android 4.4.2 and this hack-to-fix-a-bug isn't 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()
{
// overe 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);
}
});
}
本文标签: AndroidJavaScripttouchstart event not fired until zoom or scroll the pageStack Overflow
版权声明:本文标题:Android - JavaScript : touchstart event not fired until zoom or scroll the page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741512687a2382698.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论