如何在WebView Android中获取OnScroll位置

我尝试使用此代码在webview中获取当前位置

class ObservableWebView : WebView { private val TAG: String = ObservableWebView::class.java.simpleName constructor(context: Context?) : super(context) constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) override fun onScrollChanged(l: Int, t: Int, oldl: Int, oldt: Int) { super.onScrollChanged(l, t, oldl, oldt) Log.d(TAG, "scroll1 " + l + " " + y + " " + oldl + " " + " " + oldt) } override fun onOverScrolled(scrollX: Int, scrollY: Int, clampedX: Boolean, clampedY: Boolean) { super.onOverScrolled(scrollX, scrollY, clampedX, clampedY) Log.d(TAG, "scroll2 " + scrollX + " " + scrollY + " " + clampedX + " " + " " + clampedY) } 

但方法onScrollChanged从来没有调用。 我尝试使用JavaScript知道滚动位置。 这个脚本在Chrome浏览器中完美工作,但是在webview中只能使用onClick方法。 方法scrollTo和addEventListener从未调用.Google Chrome版本为62.0.3202.75,webview版本为37.0.0.0 WebView代码

  webView = viw?.findViewById(R.id.webview_fragment) as ObservableWebView webView?.setVerticalScrollBarEnabled(true) webView?.settings?.domStorageEnabled = true webView?.settings?.javaScriptEnabled = true showProgressDialog() webView?.addJavascriptInterface(JavaScriptInterface(), AppConstants.INTERFACE_JS) webView?.setWebViewClient(OnLoadPageListener()) webView?.setWebChromeClient(ChromeClient()) webView?.loadDataWithBaseURL(null, AppConstants?.FIRST_PART_JS + arguments?.getString(AppConstants?.PUT_ARTICLE_BODY_BUNDLE) + AppConstants?.LAST_PART_JS + AppConstants.GET_CURRENT_POSITION_SCRIPT + AppConstants.CLOSE_BODY , mimeType, encoding, "") 

Interesting Posts