【问题标题】:WebView doesn't load my HTML the second time?WebView 第二次没有加载我的 HTML?
【发布时间】:2012-01-25 08:40:07
【问题描述】:

我创建了一个小型 Activity,它能够在 web 视图中加载两个不同的 HTML 字符串。当我运行 Activity 时,它首先从 page_1 变量加载字符串。到目前为止,一切都很好。该页面按预期显示。 我添加了一个 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" />

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

最好的问候

斯蒂格·安徒生

【问题讨论】:

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

标签: android webview


【解决方案1】:

首先,尽量避免 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;
    }

【讨论】:

  • 嗨 Jens,你是我的英雄!感谢您帮助我! :-)
  • @Jens:您好,我遇到了和 Sting Andersen 相同的问题,并尝试了您的解决方案。但是,是否存在第二次无法加载内容的问题?你能告诉我,除此之外还有什么解决办法?
【解决方案2】:

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

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

希望这会有所帮助。

【讨论】:

  • webView.loadDataWithBaseURL(null, html, null, "utf-8", null);
  • 每当使用任一加载方法重新加载 Web 视图时,都需要在某些操作系统级别(例如 4.1.1,但不是 4.4.2)上清除缓存和历史记录。即使对于一个全新的 Web 视图 - 底层实现似乎保留“旧”内容并使用相同的 URL(在我的情况下为 null)来键入不再存在的旧内容,嘿presto 一个空显示。两天,这只是在用不同的设备将我的头撞到墙上两天后拯救了我。现在将我自己的 web 视图子类化,以免再次忘记这一点。
【解决方案3】:

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

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

【讨论】:

  • 感谢您的评论!这让我发疯 :) Kotlin 中的解决方案:val handler = Handler() handler.postDelayed(Runnable { myWebview.loadUrl("example.com") }, 10)。所有其他解决方案都失败了(清除缓存、destory()、...),对我来说只有您的解决方案有效!类似问题:stackoverflow.com/questions/55399418/…
猜你喜欢
  • 2019-08-19
  • 2015-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-14
相关资源
最近更新 更多