我需要在Fragment Android上显示progressDialog

时间:2014-08-12 20:04:13

标签: android fragment progressdialog

如何在单击每个链接时在Fragment中的webView中显示进度对话框?

根据我的尝试,只显示了第一个加载progessdialog,然后当我点击WebView中的另一个链接时,progressDialog不再工作了。

这里有我的代码:

public class Google extends SherlockFragment {
WebView mWebView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragmentberanda, container, false);
    mWebView = (WebView) rootView.findViewById(R.id.my_webview);
    final ProgressDialog pd = ProgressDialog.show(getActivity(), "Loading...", "Please wait...", true);
    pd.setCancelable(false);


    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setSupportZoom(true);
    mWebView.getSettings().setBuiltInZoomControls(true);
    mWebView.setWebViewClient(new WebViewClient() {

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

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

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            //show your progressdialog here
            super.onPageStarted(view, url, favicon);
        }
    });
    mWebView.loadUrl("http://googele.com");

    return rootView;
}

我的代码有什么问题吗?

2 个答案:

答案 0 :(得分:0)

将pd.show放入onPageFinished(WebView view, String url)

类似的东西:

@Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
        pd.show();
            super.onPageStarted(view, url, favicon);
        }

答案 1 :(得分:0)

你应该试试这个

public class About27Fragment extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View rootView = inflater.inflate(R.layout.about_27, container, false);

    final WebView webView = (WebView) rootView.findViewById(R.id.webView_about27);
    final ProgressDialog dialog = ProgressDialog.show(getActivity(), "", "Please wait, Loading Page...", true);


    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.getSettings().setBuiltInZoomControls(true);

    webView.setWebViewClient(new WebViewClient() {

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {}

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon){
            dialog.show();
        }

        @Override
        public void onPageFinished(WebView view, String url){
            dialog.dismiss();
            String webUrl = webView.getUrl();
        }


    });

    webView.loadUrl("https://www.google.com.mx");

    return rootView;
}

}