当我在Webview中动态添加html代码时,页面会上升到顶部,但我想将它保持在相同的位置

时间:2017-07-24 12:08:46

标签: android

package shuresnepali.webviewscroll;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends Activity {
    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                int height = (int) Math.floor(webView.getContentHeight() * webView.getScale());
                int webViewHeight = webView.getMeasuredHeight();
                if (webView.getScrollY() + webViewHeight >= height) {
                    Log.i("THE END", "reached");
                }
            }
        });
        webView = (WebView) findViewById(R.id.webview);
        webView.loadUrl("http://google.com/");
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                return super.shouldOverrideUrlLoading(view, url);
            }

        });

    }

当我点击底部时,google.com完全更新了下面给出的文本(hello android编程),但问题是当google.com更新时它会进入页面顶部

但我想做的是它应该从google.com页面的末尾开始,但是如何,请帮助我。

 public void load(View view) {
        Toast.makeText(this, "you clicked", Toast.LENGTH_SHORT).show();
        webView.loadUrl("javascript:document.body.innerHTML +='hello android programming'");
    }
}

0 个答案:

没有答案