开发者

Changes to a webpage through javascript not appearing in WebView. Is there a way to force the WebView to redraw its content?

Here are the steps involved:

  • Load a local html file with WebView.loadUrl().
  • This local html file has several javascript methods that modify the page. Such as adding an image, or changing the text size.
  • Call one of these methods using WebView.loadUrl("javascript:somefunction()");

This works just fine. The changes show up nicely.

However, if the pag开发者_JAVA百科e is being scrolled while this is called, something really weird happens. The javascript is called and by looking at logging, the DOM changes and everything went as expected on that level. However, the WebView doesn't show the update. If you then do something to force it to redraw itself, like rotate the device or call one of the javascript methods again (but not scrolling this time) the previous changes finally appear.

So there seems to be some weird bug where changes to the page by javascript are not updated in the WebView UI if the page was being touched or scrolled at the time.

This is really reproducible on a Nexus One 2.2 and we have seen it on other devices. Anyone know whats going on here? Is there a invalidate like method that forces a redraw of the web content?

Thanks.


Here is how I forced my webview to update, please see the code at: //WEBVIEW

package com.example.scroll;
// philip r brenan at gmail.com, www.appaapps.com 
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;

public class MainActivity extends Activity
 {protected void onCreate(Bundle savedInstanceState)
   {super.onCreate(savedInstanceState);
    setContentView(new MyWebView(this)); 
   }
  class MyWebView extends WebView 
   {MyWebView(Context Context)
     {super(Context);
      getSettings().setJavaScriptEnabled(true);
      addJavascriptInterface(this, "Android");   
      new Thread()
       {public void run()
         {for(int j = 0; j < 100; ++j)
           {post(new Runnable()
             {public void run()
               {loadData(content(), "text/html", "utf-8"); // Display in browser
               }
             });    
            try {Thread.sleep(5000);} catch(Exception e) {}
           }  
         }
       }.start();
     } 
    int c = 0, C = 1;
    String content() 
     {final StringBuilder s = new StringBuilder();
      //s.append("<html id="+(C++)+"><body>"); // WEBVIEW REFRESHES CORRECTLY *************** 
      s.append("<html><body>");              // WEBVIEW DOES NOT REFRESH ******************

      s.append("<h1 id=11>1111</h1>");
      s.append("<script>location.href = '#22';</script>");
      for(int i = 0; i < 10; ++i) s.append("<p>"+c+c+c); ++c;

      s.append("<h1 id=22>2222</h1>");
      for(int i = 0; i < 10; ++i) s.append("<p>"+c+c+c); ++c;
      Log.e("AAAAAA", "content="+s.toString());
      s.append("</body></html>");
      return s.toString();
     }
   } 
 } 


This is probably a caching issue, I had the same problem and fixed it by adding this to the setup of the webview:

webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webview.getSettings().setAppCacheEnabled(false);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜