如何在WebView中的每次单击链接时显示ProgressDialog

时间:2016-09-11 14:03:50

标签: android progressdialog

我正在使用带有ProgressDialog的webview。我的问题是在初始启动时,progressDialog正确显示abd加载,但每当我点击webview中的链接(发布链接)时,进度都没有显示。这是代码 -

    public class Xyz extends WebViewClient
{

    public void onLoadResource (WebView view , String url ) {
        if (progressDialog == null ) {
            progressDialog = new ProgressDialog(getActivity());
            progressDialog . setMessage( "Loading..." );
            progressDialog.setIndeterminate(true);
            progressDialog . show();
        }
    }

    public void onPageFinished(WebView view , String url ) {

        if (progressDialog . isShowing()) {
                progressDialog . dismiss();

            }
    }


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

        view.loadUrl(url);
        return true;

    }

}

2 个答案:

答案 0 :(得分:2)

你压倒错误的方法来实现你的目标。我在这里设置Activity,按预期工作。 我使用onPageStarted()代替onLoadResource()

活动MainActivity.java

public class MainActivity extends AppCompatActivity {

    ProgressDialog progressDialog;
    WebViewClient mClient;

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

        mClient = new WebViewClient() {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                if (progressDialog == null) {
                    progressDialog = new ProgressDialog(MainActivity.this);
                    progressDialog.setMessage("Loading...");
                    progressDialog.setIndeterminate(true);
                }
                if(!progressDialog.isShowing()) {
                    progressDialog.show();
                }
            }

            public void onLoadResource(WebView view, String url) {

            }

            public void onPageFinished(WebView view, String url) {
                if (progressDialog!=null && progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                view.loadUrl(url);
                return true;
            }
        };

        WebView webView = (WebView) findViewById(R.id.webview);
        webView.setWebViewClient(mClient);
        webView.loadUrl("http://www.google.com");
    }
}

布局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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.prasad.myapplication.MainActivity">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</RelativeLayout>

答案 1 :(得分:0)

尝试使用webChoromeClient跟踪webview加载进度,如下所示:

 webView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
    progressBar.setProgress(progress);
    if (progress == 100) {
        progressBar.setVisibility(View.GONE);

    } else {
        progressBar.setVisibility(View.VISIBLE);

    }
 }
});

并将您的webview客户端替换为以下内容

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