2015-09-25 31 views
1

我想獲得一個進度條,顯示直到網頁加載。如果用戶沒有網絡連接而不是文本或吐司顯示...但我無法得到它的工作..搜索整個網站。我認爲它很容易添加進度條類,消費活動...... 這裏是我的課堂和佈局進度條和webview中沒有互聯網連接片段

package com.nirav.rpta.fragments; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import com.nirav.rpta.R; 
public class OneFragment extends Fragment{ 

public OneFragment() { 
    // Required empty public constructor 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View rootView = inflater.inflate(R.layout.fragment_one, container, false); 
    WebView heroespage = (WebView) rootView.findViewById(R.id.webview); 
    WebSettings webSettings = heroespage.getSettings(); 
    webSettings.setJavaScriptEnabled(true); 
    heroespage.loadUrl("http://google.co.in/"); 

    return rootView; 
}} 

和我的佈局

<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="com.nirav.rpta.fragments.OneFragment"> 
<WebView 
android:id="@+id/webview" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
    android:orientation="vertical" 
/></RelativeLayout> 

回答

0

添加一個進度條到你的佈局文件代碼:

View rootView = inflater.inflate(R.layout.fragment_one, container, false); 
WebView heroespage = (WebView) rootView.findViewById(R.id.webview); 
ProgressBar loading = (ProgressBar) rootView.findViewById(R.id.progress); 

heroespage.setWebViewClient(new WebViewClient() { 
      @Override 
      public void onPageFinished(WebView view, String url) { 
       loading.setVisibility(View.GONE); 
       heroespage.setVisibility(View.VISIBLE); 
      } 
      @Override 
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
       // Show error toast 
      } 

      @Override 
      public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) { 
       // Show error toast 
      } 
     }); 

heroespage.loadUrl("http://google.co.in/"); 
+0

工作謝謝.....進度條現在可見...但沒有錯誤信息,當沒有互聯網連接..解決這也請 –

+0

你添加代碼,我寫道:「/ /顯示錯誤敬酒「? – marmor

0

請檢查下面的代碼爲你想要的東西。

您的佈局

<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="com.nirav.rpta.fragments.OneFragment"> 

     <WebView 
      android:id="@+id/webview" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"/> 

     <ProgressBar 
      android:id="@+id/progressBar2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
</RelativeLayout> 

你的Java文件應該象下面這樣:在

<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="com.nirav.rpta.fragments.OneFragment"> 
    <WebView 
     android:id="@+id/webview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 
    <ProgressBar 
     android:id="@+id/progress" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:indeterminate="true"/> 
</RelativeLayout> 

現在:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View rootView = inflater.inflate(R.layout.fragment_one, container, false); 
    WebView heroespage = (WebView) rootView.findViewById(R.id.webview); 
    // Resources 
    progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar2); 

    WebSettings settings = heroespage.getSettings(); 
    settings.setJavaScriptEnabled(true); 
    heroespage.setWebViewClient(new WebViewClient() { 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     // Log.i(TAG, "Processing webview url click..."); 
      view.loadUrl(url); 
      return true; 
     } 
     public void onPageFinished(WebView view, String url) { 
      // Log.i(TAG, "Finished loading URL: " +url); 
      if (progressBar.getVisibility() == View.VISIBLE) { 
       progressBar.setVisibility(View.GONE); 
      } 
     } 

     public void onReceivedError(WebView view, int errorCode, 
       String description, String failingUrl) { 
      // Log.e(TAG, "Error: " + description); 
      Toast.makeText(WebViewScreen.this, "You are offline.", 
        Toast.LENGTH_SHORT).show(); 
       progressBar.setVisibility(View.VISIBLE); 
     } 
    }); 
    heroespage.loadUrl(URL); 

    return rootView; 
}} 
0
You must update your layout 
<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="com.nirav.rpta.fragments.OneFragment"> 
    <WebView 
     android:id="@+id/webview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     /> 
    <ProgressBar 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:id="@+id/progbar"/> 
</RelativeLayout> 


And then find id of progressbar ProgressBar prog = view.findViewById(R.id.progbar); 

to check internet connection use this method 

public boolean isConnectingToInternet(Context _context){ 
     ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE); 
      if (connectivity != null) 
      { 
       NetworkInfo[] info = connectivity.getAllNetworkInfo(); 
       if (info != null) 
        for (int i = 0; i < info.length; i++) 
         if (info[i].getState() == NetworkInfo.State.CONNECTED) 
         { 
          return true; 
         } 

      } 
      return false; 
    } 

add this code in your oncreateview 

heroespage.setWebViewClient(new WebViewCLient(){ 
    public void onPageLoadFinished(){ 
    progbar.setvisibility(View.Gone); 
}); 
if(isconnectingInternet(getActivity()){ 

    heroespage.loadUrl("http://google.co.in/"); 
}else{ 
// show your toast here 
} 
0

隱藏/顯示可以使用setWebViewClient其提供的onpage strated和onPageFinished方法

webView.setWebViewClient(new WebViewClient(){ 
     @Override 
     public void onPageStarted(WebView view, String url, Bitmap favicon) { 
      // Show Progress bar 
      super.onPageStarted(view, url, favicon); 
     } 

     @Override 
     public void onPageFinished(WebView view, String url) { 
      super.onPageFinished(view, url); 
      // hide Progress bar 
     } 
    }); 

要檢查沒有互聯網連接的進度條,您可以使用下面的代碼

public static boolean isNetConnected() { 
    try { 
     ConnectivityManager cm = (ConnectivityManager) getSystemService(
       Context.CONNECTIVITY_SERVICE); 

     NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); 
     return activeNetwork != null && activeNetwork.isConnectedOrConnecting(); 
    } catch (Exception e) { 

    } 
    return false; 
} 

加載url前使用上述方法

if(isNetConnected()) { 
    heroespage.loadUrl("http://google.co.in/"); 
} else { 
    Toast.makeText(getActivity() ,"No internet connection",Toast.LENGTH_SHORT).show(); 
}