WebView滚动位置?

时间:2016-10-14 21:25:37

标签: android android-intent scroll webview position

我有这段代码::

x = rep(c("a","b"), 50)
ifelse(x == "a", sample(length(x), replace = TRUE), 0)

我想让我的webView自动滚动到最后一个滚动位置?但它不起作用??

修改 这是我现在的完整活动::::::::::::::::::::::::::::::::::::::::::::::::::::::: p>

    //Scroll position from last time
    String scrollPos = intent.getExtras().getString("SCROLL");
    //chapter position from last time (this is a book app)
    String chapterPos = intent.getExtras().getString("EXTRA");
    //this methode changes the webView content according to last chapter visited (it works fine)
    doItNow(chapterPos);

    // PROBLEM IS HERE !!!!
    if(scrollPos!=null) {

        float webviewsize = webView.getContentHeight() - webView.getTop();
        float positionInWV = webviewsize * (Float.parseFloat(scrollPos));
        int positionY = Math.round(webView.getTop() + positionInWV);
        webView.scrollTo(0, positionY);
    }

1 个答案:

答案 0 :(得分:0)

请粘贴您的完整代码以确定问题,但我猜测您在webview完成加载页面之前调用了scrollTo,因此在这种情况下您必须使用

yourWebView.setWebViewClient(new WebViewClient(){

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            yourWebView.scrollTo(xVal, yVal);
        }

    }
    );

如果您的网页包含图片,则可以使用此

yourWebView.setPictureListener(new PictureListener() {

        @Override
        public void onNewPicture(WebView view, Picture picture) {
             yourWebView.scrollTo(xVal, yVal);

        }
    });