当前位置 : 首页 » 互动问答 » 正文

从ViewPager中的缓存集加载webview数据

分类 : 互动问答 | 发布时间 : 2018-04-27 17:51:10 | 评论 : 2 | 浏览 : 28 | 喜欢 : 0

我正在研究一个应用程序,我需要将webView设置为ViewPager的一个项目。我这样做是通过每次创建 newInstace()片段作为URL的arrayList的大小。 但是,我有超过20个URL加载,所以如果我设置 mPager.setOffscreenPageLimit(myArray.size()),它会花费太多的负载。如果我使用 mPager.setOffscreenPageLimit(1),则每次都会重新加载。那么,有什么办法可以防止每次重新加载网页?

 public static class PlaceholderFragment extends BaseFragment {

    活动活动;
    private int media_position = 0;
    private String media_url =“”;

    private static final String KEY_CONTENT =“CategoriesFragment:Content”;
    private String mContent =“???”;

    public static PlaceholderFragment newInstance(int media_position,String media_url){

        PlaceholderFragment fragment = new PlaceholderFragment();
        fragment.media_position = media_position;
        fragment.media_url = media_url;
        返回片段;
    }

    @覆盖
    public void onSaveInstanceState(Bundle outState){
        super.onSaveInstanceState(outState);
        outState.putString(KEY_CONTENT,mContent);
    }

    @覆盖
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        if((savedInstanceState!= null)&& savedInstanceState.containsKey(KEY_CONTENT)){
            mContent = savedInstanceState.getString(KEY_CONTENT);
        }
    }

    @覆盖
    public View onCreateView(LayoutInflater inflater,ViewGroup容器,Bundle savedInstanceState){
        web1 = inflater.inflate(R.layout.fragment_main,container,false);
        activity = getActivity();

        mWebView1 =(WebView)web1.findViewById(R.id.webview);

        mWebView1.setWebViewClient(新的WebViewClient());
        mWebView1.getSettings()setJavaScriptEnabled(真)。
        。mWebView1.getSettings()setJavaScriptCanOpenWindowsAutomatically(真);
        。mWebView1.getSettings()setSupportMultipleWindows(真);
        。mWebView1.getSettings()setSupportZoom(真);
        。mWebView1.getSettings()setBuiltInZoomControls(假);
        。mWebView1.getSettings()setAllowFileAccess(真);
        mWebView1.getSettings()setAppCacheEnabled(真)。
        mWebView1.getSettings()setDomStorageEnabled(真)。
        。mWebView1.getSettings()setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

        mWebView1.setWebViewClient(新的WebViewClient(){
            @覆盖
            public void onPageStarted(WebView视图,String url,位图图标){
                super.onPageStarted(view,url,favicon);

                boolean isLoaded = false;

                如果(Utils.LoadedUrlList.containsKey(Utils.currentUrl)){
                    Log.e(“匹配键”,“media_position  - >”+ media_position +“currentUrl  - >”+ Utils.currentUrl);
                    isLoaded = true;
                } else {
                    Log.e(“Not Matched”,“media_position  - >”+ media_position +“media_url  - >”+ media_url);
                    isLoaded = false;
                }

                Log.e(“onPageStarted”,“onPageStarted”);

                如果(!isLoaded)
                    如果(media_position == 0)
                        ShowProgressDialog(activity,activity.getString(R.string.loading));
            }

            @覆盖
            public void onPageFinished(WebView view,String url){
                super.onPageFinished(view,url);

                Log.e(“onPageFinished”,“onPageFinished”);
                Log.e(“onPageFinished”,“ - >”+ url);

                hideProgressDialog();
                CookieSyncManager.getInstance()同步();

                for(int i = 0; i <Utils.SocialURLList.size(); i ++){

                    if(url.equals(Utils.SocialURLList.get(i))){
                        Utils.LoadedUrlList.put(Utils.SocialURLList.get(0),true);
                    }
                }
            }
        });

        mWebView1.loadUrl(MEDIA_URL);
        返回web1;
    }

}

回答(2)

  • 1楼
  • public void onPageFinished(WebView view,String url){
        CookieSyncManager.getInstance()同步();
    }
    

    调用WebView并调用onPageFinished后,可以将数据保存到Cookie中。 还要添加 webView.getSettings()。setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

  • 2楼
  • 我必须做同样的事情。我发现将数据保存在 onSaveInstanceState 中并没有很好地扩展(我没有尝试@Ameer建议的cookie方法)。

    相反,在下载HTML之后,可以将HTML保存为如下文件:

     try {
            OutputStreamWriter outputStreamWriter =
                    新的OutputStreamWriter(context.openFileOutput(email_id +“.txt”,Context.MODE_PRIVATE));
            outputStreamWriter.write(EMAIL_BODY);
            outputStreamWriter.close();
        } catch(IOException e){
            Log.e(“Exception”,“File write failed:”+ e.toString());
        }
    

    并将email_id与当前时间保存在 SharedPreferences .

    中然后再次加载片段时,检查email_id exists in SharedPreferences 是否存在。如果没有,或者与它一起保存的时间超过了你想要的缓存长度,那么(重新)从网页加载页面。否则,调用缓存的email_body like this

     public String getCachedHTML(String email_id){
        String ret =“”;
        尝试{
            InputStream inputStream = context.openFileInput(email_id +“.txt”);
    
            ret = readFile(ret,inputStream);
        } catch(FileNotFoundException e){
            Log.e(“login activity”,“Email cache not found:”+ e.toString());
        } catch(IOException e){
            Log.e(“登录活动”,“无法读取电子邮件缓存:”+ e.toString());
        }
        返回ret;
    }`
    

    您可以像这样在webview上设置HTML:

     webView.loadData(email_body,“text / html; charset = UTF-8”,null);

相关阅读: