Android:如何在将数据加载到 WebView 时显示进度条?

发布于 2025-01-07 05:24:49 字数 715 浏览 0 评论 0原文

将数据加载到 web 视图时如何显示进度条? 我的代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.requestWindowFeature(Window.FEATURE_PROGRESS);
    getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON); 
    this.setProgressBarVisibility(true);
    setContentView(R.layout.layout_article);
    WebView webview = (WebView) findViewById(R.id.webView);
    final Activity activity = this;
    webview.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
            activity.setProgress(progress * 100);
        }
    });

    webView.loadUrl("http://google.com");
}

How can I show the progress bar while loading data into my webview?
My code :

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.requestWindowFeature(Window.FEATURE_PROGRESS);
    getWindow().setFeatureInt( Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON); 
    this.setProgressBarVisibility(true);
    setContentView(R.layout.layout_article);
    WebView webview = (WebView) findViewById(R.id.webView);
    final Activity activity = this;
    webview.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
            activity.setProgress(progress * 100);
        }
    });

    webView.loadUrl("http://google.com");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

星星的軌跡 2025-01-14 05:24:49

在调用 loadData() 函数之前,只需尝试显示 ProgressDialog 或将 ProgressBar 放入布局中。

WebViewClient内部,只需dismiss() 进度对话框或使进度条不可见。

例如:

// when finish loading page
public void onPageFinished(WebView view, String url) {
       if(mProgress.isShowing()) {
             mProgress.dismiss();
       }
}

仅供参考,仅在完成 ​​Web 客户端设置后才调用 loadData() 或 loadURL()。

检查这个示例:使用 ProgressDialog 加载 WebView

Before calling loadData() function, just try to show ProgressDialog or put ProgressBar inside layout.

And inside the WebViewClient, just dismiss() the progressDialog or make the ProgressBar invisible.

for example:

// when finish loading page
public void onPageFinished(WebView view, String url) {
       if(mProgress.isShowing()) {
             mProgress.dismiss();
       }
}

FYI, call loadData() or loadURL() only after you are done with web client setting.

check this example: Load WebView with ProgressDialog

花辞树 2025-01-14 05:24:49

请尝试以下代码,

ProgressDialog progDailog = ProgressDialog.show( context,"Process ", "Loading Data...",true,true);

new Thread ( new Runnable()
{
     public void run()
     {
      // your data loading code goes here
     }
}).start();

 Handler progressHandler = new Handler() 
 {

     public void handleMessage(Message msg1) 
     {

         progDailog.dismiss();
         }
 }

Please try following code,

ProgressDialog progDailog = ProgressDialog.show( context,"Process ", "Loading Data...",true,true);

new Thread ( new Runnable()
{
     public void run()
     {
      // your data loading code goes here
     }
}).start();

 Handler progressHandler = new Handler() 
 {

     public void handleMessage(Message msg1) 
     {

         progDailog.dismiss();
         }
 }
遥远的绿洲 2025-01-14 05:24:49
public class MainActivity extends AppCompatActivity {

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

        final ProgressBar progress = (ProgressBar) findViewById(R.id.progress);

        WebView webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
                return false;
            }

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

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

        webView.loadUrl("http://google.com");
    }
}

和R.layout.activity_main:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent">

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

    <ProgressBar
        android:id="@+id/progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:layout_centerInParent="true"/>
</RelativeLayout>
public class MainActivity extends AppCompatActivity {

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

        final ProgressBar progress = (ProgressBar) findViewById(R.id.progress);

        WebView webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(final WebView view, final String url) {
                return false;
            }

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

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

        webView.loadUrl("http://google.com");
    }
}

And R.layout.activity_main:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent">

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

    <ProgressBar
        android:id="@+id/progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:layout_centerInParent="true"/>
</RelativeLayout>
合久必婚 2025-01-14 05:24:49

首先创建自定义 webview 类并在 xml 上使用它,如下所示

 <com.app.ui.views.CustomWebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

    </com.app.ui.views.CustomWebView>

,确保高度不应该固定,它应该是“wrap_content”

现在像这样创建自定义 webview 类,

 public class CustomWebView extends WebView {

    private ProgressBar progressBar;
    private Context context;
    private LinearLayout loaderLayout;

    public CustomWebView(Context context) {
    super(context);

    this.context = context;
    initProgressBar();

    loadUrlWithData();

}

public CustomWebView(Context context, AttributeSet attrs) {
    super(context, attrs);

    this.context = context;
    initProgressBar();

}

public CustomWebView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    this.context = context;

    initProgressBar();

}

private void initProgressBar() {
    progressBar = new ProgressBar(context);
    progressBar.setIndeterminate(false);

}

private void setLoading(boolean isLoading) {

  if (isLoading) {

   progressBar.setVisibility(View.VISIBLE);

   loaderLayout = new LinearLayout(context);
   loaderLayout.setGravity(Gravity.CENTER);
   loaderLayout.setOrientation(LinearLayout.HORIZONTAL);

    LinearLayout.LayoutParams parentLayoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        loaderLayout.setLayoutParams(parentLayoutParams);

        LinearLayout.LayoutParams progressBarLayoutParams = new 
        LinearLayout.LayoutParams(75, 75);
        progressBar.setLayoutParams(progressBarLayoutParams);

        if (progressBar.getParent() != null) {
            ((ViewGroup) progressBar.getParent()).removeView(progressBar);
        }
        loaderLayout.addView(progressBar);

        // add this loaderLayout to your parent view in which your webview is added

        else {

           progressBar.setVisibility(View.GONE);
        }

  }

  private void loadUrlWithData(){
     setLoading(true);
     //Load your HTML data with url here

  }

 //When the content will be loaded, the webview will change it's height
 @Override
protected void onSizeChanged(int w, int h, int ow, int oh) {
    super.onSizeChanged(w, h, ow, oh);

    if (h > 400) {
        setLoading(false);
    }
}

}

First make custom webview class and use it on xml like this,

 <com.app.ui.views.CustomWebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

    </com.app.ui.views.CustomWebView>

Make sure, the height should not be fixed, it should be "wrap_content"

Now make custom webview class like this,

 public class CustomWebView extends WebView {

    private ProgressBar progressBar;
    private Context context;
    private LinearLayout loaderLayout;

    public CustomWebView(Context context) {
    super(context);

    this.context = context;
    initProgressBar();

    loadUrlWithData();

}

public CustomWebView(Context context, AttributeSet attrs) {
    super(context, attrs);

    this.context = context;
    initProgressBar();

}

public CustomWebView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    this.context = context;

    initProgressBar();

}

private void initProgressBar() {
    progressBar = new ProgressBar(context);
    progressBar.setIndeterminate(false);

}

private void setLoading(boolean isLoading) {

  if (isLoading) {

   progressBar.setVisibility(View.VISIBLE);

   loaderLayout = new LinearLayout(context);
   loaderLayout.setGravity(Gravity.CENTER);
   loaderLayout.setOrientation(LinearLayout.HORIZONTAL);

    LinearLayout.LayoutParams parentLayoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        loaderLayout.setLayoutParams(parentLayoutParams);

        LinearLayout.LayoutParams progressBarLayoutParams = new 
        LinearLayout.LayoutParams(75, 75);
        progressBar.setLayoutParams(progressBarLayoutParams);

        if (progressBar.getParent() != null) {
            ((ViewGroup) progressBar.getParent()).removeView(progressBar);
        }
        loaderLayout.addView(progressBar);

        // add this loaderLayout to your parent view in which your webview is added

        else {

           progressBar.setVisibility(View.GONE);
        }

  }

  private void loadUrlWithData(){
     setLoading(true);
     //Load your HTML data with url here

  }

 //When the content will be loaded, the webview will change it's height
 @Override
protected void onSizeChanged(int w, int h, int ow, int oh) {
    super.onSizeChanged(w, h, ow, oh);

    if (h > 400) {
        setLoading(false);
    }
}

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文