2016-05-18 83 views
0

加載完成後,是否有隻顯示我的webview的?目前我的主屏幕上有一個小小的webview Fragment,但是需要一段時間才能加載。我只想在加載完成後才顯示,因爲此刻它的白色屏幕直到加載完成。這看起來並不是最好的。我試圖添加一個進度條,但由於某種原因,在我的片段中也不起作用。一旦加載完成,我如何才能顯示webview片段

我試着添加發布的代碼作爲答案,但沒有區別。我認爲它是因爲它是我在主要活動webviews中作爲進度參數的問題而具有問題的片段。

public class WebViewFragment extends Fragment { 

    WebView wv; 
    private String NEWS = "mywebsite goes here"; 

    @Override 
    public void onSaveInstanceState(Bundle outState) { 
     wv.saveState(outState); 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 

     // Checks the orientation of the screen 
     if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
      Toast.makeText(getContext(), "landscape", Toast.LENGTH_SHORT).show(); 
     } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { 
      Toast.makeText(getContext(), "portrait", Toast.LENGTH_SHORT).show(); 
     } 
    } 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     final ProgressBar Pbar; 
     View view = inflater.inflate(R.layout.webviewfrag, null, false); 

     if (savedInstanceState == null) { 
      ((WebView)view.findViewById(R.id.NewsFeedWbview)).restoreState(savedInstanceState); 
      wv = (WebView) view.findViewById(R.id.NewsFeedWbview); 
      wv.setScrollbarFadingEnabled(false); 
      Pbar = (ProgressBar) view.findViewById(R.id.pBwvf); 

      final TextView tv = (TextView) view.findViewById(R.id.tV69); 

      wv.setWebChromeClient(new WebChromeClient() { 
       public void onProgressChanged(WebView view, int progress) { 
        if (progress < 100 && Pbar.getVisibility() == ProgressBar.GONE) { 
         Pbar.setVisibility(ProgressBar.VISIBLE); 
         tv.setVisibility(View.VISIBLE); 
        } 
        Pbar.setProgress(progress); 
        if (progress == 100) { 
         Pbar.setVisibility(ProgressBar.GONE); 
         tv.setVisibility(View.GONE); 
        } 
       } 
      }); 
      wv.getSettings().setJavaScriptEnabled(true); 
      wv.setLayerType(View.LAYER_TYPE_SOFTWARE, null); 
      wv.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH); 
      wv.getSettings().setLoadsImagesAutomatically(true); 
      wv.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); 

      if (Build.VERSION.SDK_INT >= 19) { 
       // chromium, enable hardware acceleration 
       wv.setLayerType(View.LAYER_TYPE_HARDWARE, null); 
      } else { 
       // older android version, disable hardware acceleration 
       wv.setLayerType(View.LAYER_TYPE_SOFTWARE, null); 
      } 
      wv.canGoBackOrForward(5); 
      wv.getSettings().setLoadWithOverviewMode(true); 
      wv.getSettings().setUseWideViewPort(true); 
      wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); 
      wv.setPadding(0, 0, 0, 0); 
      wv.loadUrl(NEWS); 
     } 
     return view; 
    } 
} 

回答

0

下面的代碼使用

wv.setWebViewClient(new WebViewClient() { 

    public void onPageFinished(WebView view, String url) { 
     // do your stuff here 
    } 
}); 
+0

我有一個單獨的類爲我的Webclient –

1

這裏我的代碼:

活動:

public class WebViewActivity extends Activity { 
    private WebView webView; 
    private EditText urlEditText; 
    private ProgressBar progress; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_web_view); 

     urlEditText = (EditText) findViewById(R.id.urlField); 
     webView = (WebView) findViewById(R.id.webView); 
     webView.setWebChromeClient(new MyWebViewClient()); 

     progress = (ProgressBar) findViewById(R.id.progressBar); 
     progress.setMax(100); 

     Button openUrl = (Button) findViewById(R.id.goButton); 
     openUrl.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View view) { 
       String url = urlEditText.getText().toString(); 
       if (validateUrl(url)) { 
        webView.getSettings().setJavaScriptEnabled(true); 
        webView.loadUrl(url); 

        WebViewActivity.this.progress.setProgress(0); 
       } 
      } 

      private boolean validateUrl(String url) { 
       return true; 
      } 
     }); 

    } 

    private class MyWebViewClient extends WebChromeClient { 
     @Override 
     public void onProgressChanged(WebView view, int newProgress) {   
      WebViewActivity.this.setValue(newProgress); 
      super.onProgressChanged(view, newProgress); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.web_view, menu); 
     return true; 
    } 

    public void setValue(int progress) { 
     this.progress.setProgress(progress);   
    } 
} 

佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".WebViewActivity" > 

    <LinearLayout 
     android:id="@+id/urlContainer" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <EditText 
      android:id="@+id/urlField" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="3" 
      android:hint="Enter URL to open" /> 

     <Button 
      android:id="@+id/goButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="Open" /> 
    </LinearLayout> 

    <ProgressBar 
     android:id="@+id/progressBar" 
     style="?android:attr/progressBarStyleHorizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/urlContainer" /> 

    <WebView 
     android:id="@+id/webView" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_below="@id/progressBar" /> 

</RelativeLayout> 
+0

將檢查此現在謝謝@Ganesh –

+0

這沒有區別謝謝任何方式,但我認爲這是因爲即時通訊調用片段而不是活動 –

相關問題