Android progressbar on webview appearing twice

Deadly 提交于 2020-01-07 06:17:09

问题


I'm trying to create a simple app for android with progress bar. Everything works fine. But, here two issues

1) When the application is launched i can see progress loading twice.

2) How can i disable the progress bar after the initial page is finished loading. I dont want to show the progressbar on each click..

Here is my code

package com.mycom.jquery;
import android.app.Activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class HelloWebView extends Activity {

WebView webview;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getWindow().requestFeature(Window.FEATURE_PROGRESS);

    setContentView(R.layout.main);
    getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
            Window.PROGRESS_VISIBILITY_ON);

    webview = (WebView) findViewById(R.id.webview);
    webview.getSettings().setJavaScriptEnabled(true);

    webview.setWebViewClient(new WebViewClient() {

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

    });
    final Activity activity = this;

    final ProgressDialog progressDialog = new ProgressDialog(activity);
    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    progressDialog.setMessage("Loading...Jquery.com");
    progressDialog.setCancelable(true);

    webview.loadUrl("http://www.jquery.com");

    // WebChromeClient give progress etc info
    webview.setWebChromeClient(new WebChromeClient() {

        public void onProgressChanged(WebView view, int progress) {
            progressDialog.show();
            progressDialog.setProgress(0);
            activity.setProgress(progress * 1000);

            progressDialog.incrementProgressBy(progress);

            if (progress == 100 && progressDialog.isShowing())
                progressDialog.dismiss();
        }
    });

}

}

回答1:


Set a flag when progress becomes 100, and then if that flag is true immediately return in your onProgressChanged implementation.




回答2:


Try calling

progressDialog.show();

outside of

onProgressChanged(WebView view, int progress)



回答3:


this.getWindow().requestFeature(Window.FEATURE_PROGRESS);

try removing this. from your code.



来源:https://stackoverflow.com/questions/9867189/android-progressbar-on-webview-appearing-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!