问题 WebView第二次不加载我的HTML?


我创建了一个小的Activity,它能够在webview中加载两个不同的HTML字符串。当我运行Activity时,它从page_1变量加载String开始。到现在为止还挺好。该页面按预期显示。 我添加了一个onFling监听器,它应该使活动加载page_2变量的内容。 问题是,即使调用onFling并调用loadUrl,webview也不会更新?

我的活动如下:

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.webkit.WebView;

public class Test extends Activity {
private GestureDetector mGestureDetector;
private WebView mWebView;
private int mPageIndex;

private static final String page_1 = "<html><body>Hello page 1</body></html>";
private static final String page_2 = "<html><body>Hello page 2</body></html>";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.test);
  mWebView = (WebView) findViewById(R.id.webview);
  mWebView.loadData(page_1, "text/html", "utf-8");
  setupGestureDetection();
  mPageIndex = 0;
}

private void setupGestureDetection() {
  mGestureDetector = new GestureDetector(new MyGestureDetector());
  mWebView.setOnTouchListener(new OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
      return mGestureDetector.onTouchEvent(event);
    }
  });
}

class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {
  private static final int SWIPE_DISTANCE_THRESHOLD = 120;
  private static final int SWIPE_VELOCITY_THRESHOLD = 200;

  private boolean isHorizontalSwipe(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    if (Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
      if (Math.abs(e1.getX() - e2.getX()) > SWIPE_DISTANCE_THRESHOLD) {
        return true;
      }
    }
    return false;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
  if (isHorizontalSwipe(e1, e2, velocityX, velocityY)) {
    if (e1.getX() > e2.getX()) {
      // Right to left
      if (++mPageIndex % 2 == 0) {
        mWebView.loadData(page_1, "text/html", "utf-8");
      } else {
        mWebView.loadData(page_2, "text/html", "utf-8");
      }
      return true;
    }
  }
  return false;
}
}
}

我的布局看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/webview"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" />

我希望有一个人可以帮助我! :-)

最好的祝福

Stig Andersen


10381
2018-01-25 08:40


起源

如果添加a,则可以从Web控制台获取错误输出 WebChromeClient。 mWebView.setWebChromeClient(new WebChromeClient()); 应该这样做。 - dokkaebi
另外,您如何知道正在进行loadData调用?你是否正在使用调试器?打印日志消息? - dokkaebi


答案:


首先,尽量避免 WebView#loadData(String, String, String) 像瘟疫一样 - 这是一个有缺陷的p.o.s.使用 WebView#loadDataWithBaseURL(String, String, String, String, String) 代替。

此外,这将解决您的问题。反直觉我知道,但是,嘿,这是Android的方式。

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        if (isHorizontalSwipe(e1, e2, velocityX, velocityY)) {
            if (e1.getX() > e2.getX()) {
                // Right to left
                if (++mPageIndex % 2 == 0) {
                    mWebView.loadDataWithBaseURL(null, page_1, null, "utf-8", null);
                } else {
                    mWebView.loadDataWithBaseURL(null, page_2, null, "utf-8", null);
                }
                // Seriously. You must return false for the loadDataWithBaseURL to work. Not kidding. So you could skip this return.
                return false;
            }
        }
        return false;
    }

9
2018-01-25 09:31



嗨Jens,你是我的英雄!谢谢你帮助我! :-) - Stig Andersen
@Jens:嗨,我遇到了与Sting Andersen相同的问题并尝试了你的解决方案。但是,问题是第二次没有加载内容?你能告诉我,除此之外还有什么可以解决的问题? - Hiral
谢谢,它有效。 - Yasin


这些解决方案对我不起作用。最终工作的是在加载数据之前清除缓存和历史记录(在这种情况下loadData工作):

webView.clearCache(true);
webView.clearHistory();
webView.loadData(html, "text/html", "UTF-8");

希望这会有所帮助。


4
2017-11-07 10:28



webView.loadDataWithBaseURL(null,html,null,“utf-8”,null); - Yasin
只要使用任一加载方法重新加载Web视图,就需要在某些操作系统级别(例如4.1.1,而不是4.4.2)上清除缓存和历史记录。即使对于一个全新的Web视图 - 底层实现似乎保留了“旧”内容并使用相同的URL(在我的情况下为null)来键入不再存在的旧内容,并且它们预先显示空白。两天,这让我挽救了我两天后,我的头撞在墙上,不同的设备有不同的行为。现在继承我自己的Web视图,以免再次忘记这一点。 - Colin


那些仍然有问题的人我找到了一个快速解决方案,只需使用一个处理程序

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        webView.loadDataWithBaseURL("", html, "text/html", "UTF-8", null);
    }
}, 10) ;

3
2018-05-11 13:46