2

Need to disable the cache on WebViews inflated through Proteus.

Are there any attributes on the WebView which can be used to disable it?

We could find the view normally would if it was inflated using precompiled XML layouts using findViewById(R.id.something) and call the following methods on it.

WebView wv = parent.findViewById(R.id.webview);
WebSettings ws = wv.getSettings();

ws.setAppCacheEnabled(false); 
ws.setCacheMode(WebSettings.LOAD_NO_CACHE)

But since proteus inflates layouts using JSON from the server I cannot find the view like this and the solution would not scale for multiple WeViews.

4

1 回答 1

2

您可以为 WebView 注册一个自定义属性处理程序disableCache并解决您的问题。

ProteusBuilder builder;
builder.register("WebView, "disableCache", new BooleanAttributeProcessor<WebView>() {
     @Override
     public void setBoolean(WebView view, boolean value) {
         WebSettings ws = view.getSettings();
         if (value) {
             ws.setAppCacheEnabled(value); 
             ws.setCacheMode(WebSettings.LOAD_NO_CACHE);
         }
     }
});
Proteus proteus = builder.build();
ProteusContext context = proteus.createContextBuilder(context).build();

layoutInflater = context.getInflater();

用于layoutInflater膨胀您的布局,然后在您的布局中将属性设置为truefalse随心所欲。

{
  "type": "WebView",
  "disableCache": "true"
}

您可以使用您的实例注册自定义属性处理程序,Proteus它将开始工作。好处是您可以根据您的要求切换标志,并避免为所有通过 proteus 膨胀的 Web 视图禁用硬编码缓存。

于 2017-02-23T07:40:54.627 回答