如何在滚动时获取webview垂直滚动位置

时间:2013-01-10 11:00:57

标签: javascript android webview scroll android-webview

我在webView中制作某种书籍阅读器。我使用了JavaScript,它通过for循环动态创建<img>标记。查看代码的for循环,每个img标记都加载了与URL不同的图像。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    wv = (WebView) findViewById(R.id.webView1);
    wv.getSettings().setJavaScriptEnabled(true);

    progressDialog = ProgressDialog.show(MainActivity.this,
            "Loading Book...!", "Please Wait");

    String htnlString = "<!DOCTYPE html><html><body style = \"text-align:center\"><script>var out = '';for (var counter = 1; counter <= 100; counter++){    out += '<img src=\"http://shiaislamicbooks.com/books_snaps/UR335/'+counter+'.jpg\"alt=\"Page No:'+counter+'\" width=\"100%\" />';}document.write(out);</script></body></html>";

    wv.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageFinished(WebView view, String url) {
            progressDialog.dismiss();
            Toast.makeText(MainActivity.this, "Completed",
                    Toast.LENGTH_SHORT).show();
            super.onPageFinished(view, url);
        }

    });

    wv.loadDataWithBaseURL(null, htnlString, "text/html", "UTF-8", null);
}

查看htnlString

  • 现在我想在textView中显示书页信息。这意味着,在滚动webview时,textView应该更新txtPage。
  • scrollTo(x,y)是有用的,但我想要webview的滚动监听器。

1 个答案:

答案 0 :(得分:1)

WebView webview;
yPos = webview.getScrollY();
xPos=webview.getScrollX();