2016-11-11 75 views
-1

您好我是新手,也是爲了android開發,我想在webview加載頁面時加載進度條,加載後需要自動隱藏進度,我沒有知道該怎麼做,幫我傢伙如何在Android Studio上加載網頁時添加進度條Webview

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="0dp" 
    android:paddingLeft="0dp" 
    android:paddingRight="0dp" 
    android:paddingTop="0dp" 
    tools:context="com.example.tamil.stagingsite.MainActivity"> 

    <ProgressBar 
     android:id="@+id/progressBar" 
     style="@android:style/Widget.Material.Light.ProgressBar.Large" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" /> 

    <WebView 
     android:id="@+id/activity_main_webview" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentTop="true" 
     android:animationCache="true" 
     android:background="@android:color/white"> 

    </WebView> 
</RelativeLayout> 

MainActivity.java

package com.example.tamil.stagingsite; 

import android.app.Activity; 
import android.graphics.Bitmap; 
import android.net.Uri; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.Window; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.ProgressBar; 

import com.google.android.gms.appindexing.Action; 
import com.google.android.gms.appindexing.AppIndex; 
import com.google.android.gms.appindexing.Thing; 
import com.google.android.gms.common.api.GoogleApiClient; 

public class MainActivity extends Activity { 


    private WebView mWebView; 
    /** 
    * ATTENTION: This was auto-generated to implement the App Indexing API. 
    * See https://g.co/AppIndexing/AndroidStudio for more information. 
    */ 
    private GoogleApiClient client; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_main); 

     mWebView = (WebView) findViewById(R.id.activity_main_webview); 

     WebSettings webSettings = mWebView.getSettings(); 
     webSettings.setJavaScriptEnabled(true); 
     mWebView.loadUrl("http://census-staging.herokuapp.com/"); 
     mWebView.setWebViewClient(new WebViewClient()); 
     mWebView.setWebViewClient(new MyAppWebViewClient()); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
    } 


    @Override 
    public void onBackPressed() { 
     if (mWebView.canGoBack()) { 
      mWebView.goBack(); 
     } else { 
      super.onBackPressed(); 
     } 
    } 


    /** 
    * ATTENTION: This was auto-generated to implement the App Indexing API. 
    * See https://g.co/AppIndexing/AndroidStudio for more information. 
    */ 
    public Action getIndexApiAction() { 
     Thing object = new Thing.Builder() 
       .setName("Main Page") // TODO: Define a title for the content shown. 
       // TODO: Make sure this auto-generated URL is correct. 
       .setUrl(Uri.parse("http:staging.herokuapp.com/")) 
       .build(); 
     return new Action.Builder(Action.TYPE_VIEW) 
       .setObject(object) 
       .setActionStatus(Action.STATUS_TYPE_COMPLETED) 
       .build(); 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     client.connect(); 
     AppIndex.AppIndexApi.start(client, getIndexApiAction()); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 

     // ATTENTION: This was auto-generated to implement the App Indexing API. 
     // See https://g.co/AppIndexing/AndroidStudio for more information. 
     AppIndex.AppIndexApi.end(client, getIndexApiAction()); 
     client.disconnect(); 
    } 
} 
+1

的可能的複製[的Android的WebView的進度條(http://stackoverflow.com/questions/2537454/android-webview-progress-bar) –

+0

很多問題與此存在.. –

回答

0

顯示在onPageStarted方法你的進度和關閉它在PageFinished方法。

mWebView.setWebViewClient(new WebViewClient() { 

    @Override 
    public void onPageStarted(WebView view, String url, Bitmap facIcon) { 
     //showProgressBar(); 
    } 

    @Override 
    public void onPageFinished(WebView view, String url) { 
    // hideProgressBar(); 
    } 
} 
0

初始化主要活動的進度條和

使可見您webviewclient onPageFinished方法

mWebView.setWebViewClient(new MyAppWebViewClient(this)); 

private class MyAppWebViewClient extends WebViewClient { 

private Context mContext; 


public MyAppWebViewClient(Context context) { 
    this.mContext = context; 
} 


@Override 
public void onPageFinished(final WebView view, String url) { 


    progressbar.setVisibility(View.GONE); 


} 
} 
1

要添加進度對話框進度欄加載在Webview上添加下面的代碼,不需要添加XML中的Progressbar。

//Intialize the progress dialog: 
    WebView webview; 
    ProgressDialog progressDialog; 

//Write the following code in OnCreate: 
    webview=(WebView)findViewById(R.id.webview); 
    progressDialog=new ProgressDialog(this); 
    progressDialog.setMessage("Loading"); 
    progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
    progressDialog.show(); 

加載網頁後,你必須駁回ProgressDialog.For在OnPageFinish辭退的progressDialog。

webview.setWebViewClient(new WebViewClient() 
    { 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      view.loadUrl("YOUR URL"); 
      return true; 
     } 

     @Override 
     public void onPageFinished(WebView view, String url) { 
      super.onPageFinished(view, url); 
      if (progressDialog.isShowing()) { 
       progressDialog.dismiss(); 
      } 
     } 
    });