在xamarin android中加载webview时的Progressbar

时间:2018-04-10 08:36:29

标签: c# xamarin.android

我想在加载webview时显示进度条,并在webview完全加载时隐藏progessbar。

这是我的Activity.cs

  progress = new ProgressDialog(this);
        progress.Indeterminate = true;

        progress.SetProgressStyle(Android.App.ProgressDialogStyle.Spinner);
        progress.SetMessage("Loading... Please wait...");
        progress.SetCancelable(false);
        progress.Show();

        _faq = FindViewById<Android.Webkit.WebView>(Resource.Id.wv_FAQ);

     _web.AppWebViewClients(progress);
     _web.ShouldOverrideUrlLoading(_faq, _url);
     _web.OnPageFinished(_faq, _url);

这是我的ResourceWebView.cs

 public class ResourceWebView: WebViewClient
{
    Android.App.ProgressDialog progress;

    public void AppWebViewClients(ProgressDialog progressBar)
    {
        this.progress = progressBar;
        progress.Show();
    }

    public bool ShouldOverrideUrlLoading(WebView view, String url)
    {

        view.LoadUrl(url);
        progress.Show();
        return true;
    }

    public void OnPageFinished(WebView view, String url)
    {
        OnPageFinished(view, url);
        progress.Hide();
    }
}

但是这段代码对我不起作用..

1 个答案:

答案 0 :(得分:1)

试试这个

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);

        urlEditText = (EditText) findViewById(R.id.urlField);
        webView = (WebView) findViewById(R.id.webView);
        webView.setWebViewClient(new MyWebViewClient());

        progress = (ProgressBar) findViewById(R.id.progressBar);
        progress.setVisibility(View.GONE);
        Button openUrl = (Button) findViewById(R.id.goButton);
        openUrl.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                String url = urlEditText.getText().toString();
                if (validateUrl(url)) {
                    webView.getSettings().setJavaScriptEnabled(true);
                    webView.loadUrl(url);

                }
            }

            private boolean validateUrl(String url) {
                return true;
            }
        });

    }

    private class MyWebViewClient extends WebViewClient {   
         @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

         @Override
        public void onPageFinished(WebView view, String url) {
             progress.setVisibility(View.GONE);
                WebViewActivity.this.progress.setProgress(100);
            super.onPageFinished(view, url);
        }

         @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
             progress.setVisibility(View.VISIBLE);
            WebViewActivity.this.progress.setProgress(0);
            super.onPageStarted(view, url, favicon);
        }
    }

参考此内容以获取更多参考:http://stacktips.com/tutorials/android/progressbar-while-loading-webview