如何在Android studio Webview上加载网页时添加进度条

时间:2016-11-11 05:15:18

标签: java android webview

嗨,我是新手,也是Android开发,我想在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();
    }
}

3 个答案:

答案 0 :(得分: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 On 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();
            }
        }
    });

答案 1 :(得分: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();
   }
}

答案 2 :(得分: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);


}
}