【问题标题】:Progress Bar on webview during page load页面加载期间网页视图上的进度条
【发布时间】:2017-09-30 20:45:55
【问题描述】:

我试图在加载网页期间在 webview 中显示加载进度条,并且在加载网页后我希望进度条消失 我尝试了许多在线可用的示例,但没有一个对我有用 这是我的代码

import android.app.Activity;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);




        webView = (WebView) findViewById(R.id.webView);
        webView.setWebViewClient(new myWebClient());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("http://www.notherstore.in");

    }

    public class myWebClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            view.loadUrl(url);
            return true;

        }
    }

    @Override
    // This method is used to detect back button
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            // Let the system handle the back button
            super.onBackPressed();
        }
    }
}

这是我的 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="in.notherstore.notherstore.MainActivity">

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

    <ProgressBar

        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:indeterminate="false"
        android:layout_gravity="center" />
</RelativeLayout>

我们将不胜感激任何帮助

【问题讨论】:

  • 您在寻找onPageFinished 吗?

标签: javascript android webview


【解决方案1】:

你可以试试onPageFinished 方法。该方法在您的页面完全加载时运行。

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

            }catch(Exception exception){
                exception.printStackTrace();
            }
        }

【讨论】:

    【解决方案2】:
    webView.setWebViewClient(new WebViewClient() {
    
                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    progressBar.setVisibility(View.VISIBLE);
                    progressBar.bringToFront();
                }
    
                @Override
                public void onPageFinished(WebView view, String url) {
                    loadingBar.setVisibility(View.GONE);
                }
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      相关资源
      最近更新 更多