2012-04-03 19 views
5

Hi,viewpager + webview, come interrompere la visualizzazione web loading

Ho creato un'applicazione che utilizza ViewPager e tutte le pagine contengono WebViews. Ogni Webview contiene immagini di server, video, mp3 ...

Le impostazioni WebView sono

 public Object instantiateItem(View collection, int position) { 
      . 
      . 
      . 

      final RelativeLayout imageLayout = (RelativeLayout)   
      inflater.inflate(R.layout.omb_webview_layout, null); 
      WebView webview = (WebView) imageLayout.findViewById(R.id.video_view); 
      ProgressBar pbImage = (ProgressBar) imageLayout.findViewById(R.id.progressBar1); 

      webview.requestFocus(View.FOCUS_DOWN); 
      webview.getSettings().setBuiltInZoomControls(false); 
      webview.getSettings().setSupportZoom(true); 
      webview.getSettings().setUseWideViewPort(true); 
      webview.getSettings().setJavaScriptEnabled(true); 
      webview.getSettings().setLoadWithOverviewMode(true); 
      webview.getSettings().setUseWideViewPort(true); 
      webview.setWebViewClient(new mWebViewClient()); 
      webview.getSettings().setPluginsEnabled(true); 
      webview.clearCache(true); 
      webview.clearHistory(); 
      if(Pbar != null) { 
       webview.setWebChromeClient(new WebChromeClient() { 
       public void onProgressChanged(WebView view, int progress)  { 
        Log.i(TAG, "webview loading progress : "+ progress); 
        if(progress == 100) { 
         try { 
          Pbar.setVisibility(ProgressBar.GONE); 
         } catch (Exception e) { 
         Log.e(TAG, "timer progress GONE : "+ progress,e); 
         } 
         } 
      }); 
      } 

Il problema è quando le pagine sono fregato poi anche questa linea

Log.i(TAG, "webview loading progress : "+ progress); 

è di esecuzione che significa che le visualizzazioni web si stanno caricando in background. Se faccio scorrere 4 o più pagine, tutte le immagini web si stanno caricando.

 destroyItem(View collection, int position, Object o) { 
    Log.d("DESTROY", "destroying view at position sheetal " + position); 

      RelativeLayout view = (RelativeLayout)o; 

        WebView tmp = webviewDictionary.get(position);; 

      tmp.getSettings().setJavaScriptEnabled(false); 
          tmp.stopLoading(); 
     if(tmp != null){ 
         Log.i(TAG, "sheetal webview is destroyed " +position); 
         try { 
          tmp.onPause(); 

          tmp.loadDataWithBaseURL("about:blank", "<html></html>","text/html", "UTF-8", null); 
          tmp=null; 
          // tmp.destroy(); 
         } 
         catch (Exception e) { 
          Log.i(TAG, "webview destroyed Exception 0" ,e); 
         } 
        } 

Come interrompere il caricamento in background se le pagine non sono visibili? setOffscreenPageLimit (1) (significa 3 pagine prev, cur, next),

In Viewpager, 2 pagine (significa pagina 1 posizione) non si carica correttamente. Mostra una pagina bianca vuota, come posso risolverlo?

Grazie in anticipo

+0

ho usato StopLoading(), ma dose non fermano il caricamento in background, ogni volta che destroyItem (.. .) si chiama webview interrompere il caricamento giusto? ma non smettere di caricare. –

+0

Non completamente sicuro, ma non è necessario impostare OffscreenPageLimit su 0? – THelper

+0

ho provato pager.OffscreenPageLimit (0) ma non funziona. –

risposta

2

questo ha risolto il mio problema.

ho creato un dizionario, contiene 3 webview con posizione chiave.

qui sto aggiungendo visualizzazioni nel dizionario

public Object instantiateItem(View collection, int position) { 
    . 
    . 
    . 
    webviewDictionary.put(position, webView); 
    . 
    . 
    . 
} 

visualizzazioni di pulizia è da dizionario

public void cleanAllWebViews() { 

    try { 

     if(webviewDictionary != null){ 

      Iterator<Integer> webViewIterator = webviewDictionary.keySet().iterator(); 

      while(webViewIterator.hasNext()) { 

       Integer key=(Integer)webViewIterator.next(); 

       WebView webView = (WebView) webviewDictionary.get(key); 


       webView.stopLoading(); 
       //     webView.pauseTimers(); 

       webView.setWebChromeClient(null); 
       webView.setWebViewClient(null); 


       webView.loadDataWithBaseURL("about:blank", "<html></html>","text/html", "UTF-8", null); 
       webViewIterator.remove(); 

      } 
     } 

     webviewDictionary = null; 


    }catch (Exception e) { 
     Log.e(TAG, "ombook last call onPauseAllWebviews Exception",e ); 
    } 
} 

    and 

    @Override 
public void onBackPressed() { 

    cleanAllWebViews(); 

    System.gc(); 

    super.onBackPressed(); 
} 
1

Hai provato "OffscreenPageLimit" impostato a 1? 0 è un valore non valido per questo post: ViewPager.setOffscreenPageLimit(0) doesn't work as expected

anche, hai provato a gestire alcuni eventi del passaggio tra le pagine, in modo che ogni volta che si accende una pagina, WebView della pagina corrente si fermerà il carico, o di avvio caricare un file html vuoto?

+1

sì, navigando le pagine ho messo in pausa la webview (webview.onPause()) –

+1

ha funzionato? In tal caso, puoi pubblicare il codice pertinente? –

Problemi correlati