简体   繁体   English

滚动到webview的顶部(Android)

[英]scrolling to the top of a webview (Android)

I have a webview loading a big texts after the user has clicked an object. 在用户点击对象后,我有一个webview加载大文本。 I like the text to be shown from the top. 我喜欢从顶部显示的文字。 But if the user scrolled down in the previous text the next is already scrolled and not shown from the top. 但是如果用户在前一个文本中向下滚动,则下一个已经滚动并且不从顶部显示。

I am using webView.scrollTo(0,0); 我正在使用webView.scrollTo(0,0); in my onPageFinished event of the webview. 在我的webview的onPageFinished事件中。

This works fine in my emulator, but on my phone the text is scrolled in the middle. 这在我的模拟器中工作正常,但在我的手机上,文本在中间滚动。

when adding a pause with Thread.sleep it also works on my phone. 当使用Thread.sleep添加暂停时,它也适用于我的手机。 What am I doing wrong here ? 我在这做错了什么?

My Code: 我的代码:

webView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        // Pause is needed for my physical phone S7. In Emulator there I have no problem.
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        webView.scrollTo(0,0);
        super.onPageFinished(view, url);
    }
});

Try with this block of code - 试试这段代码 -

webView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url) {
        // Pause is needed for my physical phone S7. In Emulator there I have no problem.
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        view.scrollTo(0,0);
        super.onPageFinished(view, url);
    }
});

Actually, you're so close to the solution. 实际上,你是如此接近解决方案。 Just the point is you need to set scrollTo in the returned view of onPageFinished method. 关键是你需要在onPageFinished方法的返回视图中设置scrollTo。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM