在WebView加载时在Action Bar上添加加载微调器?

时间:2013-08-23 16:12:47

标签: android webview

我正在开发一个Android应用程序。这是使用webview呈现html页面,但在页面加载时它保持空白。我想在操作栏中添加一个加载微调器,以便用户知道某些内容正在加载而且不是空白。

任何人都可以指导我如何做到这一点。

我现有的代码 -

package com.pranavsethi.dpsrkp;

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.DownloadListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class Achievements extends Activity{

    private WebView mWebView; 

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webvew_client_layout);
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);


        //For web view 
        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true); //enable javascript
        mWebView.getSettings().setBuiltInZoomControls(true);  //enable zoom
        mWebView.getSettings().setDisplayZoomControls(false);
        mWebView.getSettings().setLoadWithOverviewMode(true);  
        mWebView.getSettings().setUseWideViewPort(true);   
        mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);   
        mWebView.setScrollbarFadingEnabled(true);   
        mWebView.setDownloadListener(new DownloadListener() {
            @Override
            public void onDownloadStart(String url, String userAgent,
                    String contentDisposition, String mimetype,
                    long contentLength) {
                // handle download, here we use brower to download, also you can try other approach.
                Uri uri = Uri.parse(url);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
            }
        });
        mWebView.loadUrl("http://pranavsethi.tk");  // name of website to load
        mWebView.setWebViewClient(new HelloWebViewClient());   //removing loading progressbar


}


    private class HelloWebViewClient extends WebViewClient{    //our web client
         @Override
            public void onReceivedError(WebView view, int errorCode,                //Code for checking the internet connection and return the error if fails
                    String description, String failingUrl) {
             Intent intent = new Intent(Achievements.this, MainActivity.class);
               intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                   return;

             }

             @Override
        public boolean shouldOverrideUrlLoading(WebView webview, String url)
        {
            webview.loadUrl(url);
            return true;

        }
    }

    @Override
    public boolean onKeyDown(int KeyCode, KeyEvent event)
    {
        if ((KeyCode)== KeyEvent.KEYCODE_BACK && mWebView.canGoBack())   
        {
            mWebView.goBack();
            return true;

        }
        return super.onKeyDown(KeyCode, event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) 

    {
       switch (item.getItemId()) 
       {
         case android.R.id.home:
            Intent intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            overridePendingTransition  (R.anim.right_slide_in, R.anim.fade_out);
            break;







       default:
            return super.onOptionsItemSelected(item);


       }
    return true;


    }

    @Override
    public void onBackPressed() 
    {
        this.finish();
        overridePendingTransition  (0, R.anim.right_slide_out);
        return;
    }
    }

1 个答案:

答案 0 :(得分:20)

<强>解

添加以下内容 -

requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setProgressBarIndeterminateVisibility(true); 

之后 -

super.onCreate(savedInstanceState);

这在你的WebViewClient

private class HelloWebViewClient extends WebViewClient{  
     @Override
    public boolean shouldOverrideUrlLoading(WebView webview, String url){
            webview.loadUrl(url);
            setProgressBarIndeterminateVisibility(true);
                            return true;

        }
             @Override
                public void onPageFinished(WebView webview, String url){
                    super.onPageFinished(webview, url);
                    setProgressBarIndeterminateVisibility(false);
                 }
    }