在加载webview之前显示活动圈 - Android

时间:2014-03-01 14:48:51

标签: android webview android-progressbar

我正在使用webview在我的应用中显示网页。网页加载大约需要2-3秒。 在网页加载之前,我想显示一个活动圈子进度条,如显示here的那个。
这就是活动圈子的样子:

我怎样才能完成它?

我在网上搜索过但无法找到令人满意的正确解释。谢谢!

WebViewPage.java

public class Webpage extends Activity {

WebView web;
ProgressBar progressBar;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.web_page);

    Intent startwebpage = getIntent();

    web = (WebView) findViewById(R.id.webView);
    web.setWebViewClient(new myWebClient());

    web.getSettings().setJavaScriptEnabled(true);
    web.loadUrl("http://www.google.com");
}

public class myWebClient extends WebViewClient
{
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // TODO Auto-generated method stub

        view.loadUrl(url);
        return true;

    }

    @Override
    public void onPageFinished(WebView view, String url) {
        // TODO Auto-generated method stub
        super.onPageFinished(view, url);
    }
}
}

2 个答案:

答案 0 :(得分:4)

progressBar方法中显示onPageStarted并在onPageFinished中将其关闭。

    private ProgressDialog progressBar;
  1. 在onPageStarted中显示progressBar:

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        // TODO Auto-generated method stub
        super.onPageStarted(view, url, favicon);
    
         // prepare for a progress bar dialog
            progressBar = new ProgressDialog(v.getContext());
            progressBar.setCancelable(true);
            progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);            
            progressBar.show();
    }
    
  2. 2.在onPageFinished中删除它:

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);
    
            progressBar.dismiss();
        }
    

    编辑:使用ProgressBar代替ProgressDialog

    1. 在布局中使用以下代码。 使用您选择的drawable“@ drawable / progress”

       <ProgressBar
          android:id="@+id/progress_bar"
          style="?android:attr/progressBarStyleLarge"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerHorizontal="true"
          android:layout_centerVertical="true"
          android:visibility="gone"
          android:indeterminateDrawable="@drawable/progress" >
      </ProgressBar>
      
    2. 在您的活动中充气。使其在onPageStarted中可见并进入onPageFinished。

      private ProgressBar prgrsBar;
      
      @Override
      public void onPageStarted(WebView view, String url, Bitmap favicon) {
          // TODO Auto-generated method stub
          super.onPageStarted(view, url, favicon);
      
          prgrsBar.setVisibility(View.VISIBLE);
      }
      
       @Override
      public void onPageFinished(WebView view, String url) {
          // TODO Auto-generated method stub
          super.onPageFinished(view, url);
      
          prgrsBar.setVisibility(View.GONE);
      }
      

答案 1 :(得分:0)

这对我有用:     ProgressDialog pb = null;

        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);

            // prepare for a progress bar dialog
            if (pb == null) {
                pb = new ProgressDialog(Quiz.this);
                pb.setCancelable(true);
                pb.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                pb.show();
            }
        }

        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);

            pb.dismiss();
        }