Android Studio 中 WebView 内部链接的进度条 [英] Progress bar for internal links of a WebView in Android Studio

查看:28
本文介绍了Android Studio 中 WebView 内部链接的进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 Android Studio 开发的 WebView.我想实现一个进度条以显示在 WebView 的每个内部页面上.

I have a WebView developed with Android Studio. I want to implement a progress bar to appear on every internal page of the WebView.

我给你看代码.虽然我必须澄清一下,该应用程序已经有一个进度条,它实现了启动画面的功能.

I show you the code. Although I must clarify that the app already has a progress bar, which fulfills the function of Splash Screen.

我想要的是,当在 WebView 中打开链接时,会出现一个进度条,指示页面正在加载,因为打开链接时用户的屏幕会瘫痪几秒钟,直到它完全加载,这可能很烦人肉眼可见.

What I want is that when opening a link within the WebView a progress bar appears indicating that the page is loading, since when opening a link the user's screen is paralyzed for a few seconds until it loads completely, which It can be annoying to the naked eye.

初步代码:

主活动

package com.example.app;

import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ProgressBar;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    String ShowOrHideWebViewInitialUse = "show";
    private ProgressBar spinner;

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

        WebView webview = (WebView) findViewById(R.id.webView);
        spinner = (ProgressBar) findViewById(R.id.progressBar1);
        webview.setWebViewClient(new CustomWebViewClient());

        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setDomStorageEnabled(true);
        webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
        webview.loadUrl("https://www.example.com");
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        WebView miVisorWeb;
        miVisorWeb = (WebView) findViewById(R.id.webView);
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_BACK:
                    if (miVisorWeb.canGoBack()) {
                        miVisorWeb.goBack();
                    } else {
                        finish();
                    }
                    return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    private class CustomWebViewClient extends WebViewClient {

        @Override
        public void onPageStarted(WebView webview, String url, Bitmap favicon) {

            if (ShowOrHideWebViewInitialUse.equals("show")) {
                webview.setVisibility(View.INVISIBLE);
            }
        }

        @Override
        public void onPageFinished(WebView view, String url) {

            ShowOrHideWebViewInitialUse = "hide";
            spinner.setVisibility(View.GONE);

            view.setVisibility(View.VISIBLE);
            super.onPageFinished(view, url);

        }
    }
}

如果你能实现另一个不填满整个屏幕的进度条,它会很有帮助,当加载 WebView 中的链接时显示,加载完成后消失.

It would be a great help if you could implement another progress bar that does not fill the entire screen, that is displayed when loading a link within the WebView and that then disappears when the loading is complete.

非常感谢!

推荐答案

除了 Spinner 进度条,您还可以通过改变样式使用水平条.

Instead of a Spinner progress bar, you can use horizontal bar by changing the style.

<ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        --- other attributes here ---
         />

将这个水平进度条放在 webview 的顶部,类似于下图.

Place this horizontal progress bar on the top of the webview similar to the image below.

另外,通过将 chromeWebClient 设置为 webview 并覆盖 onProgressChanged 方法,我们可以获得页面加载的进度,可以设置为水平进度条.>

Additionally, by setting chromeWebClient to the webview and overriding onProgressChanged method we can get the progress of the page loading which can be set to the horizontal progress bar.

webView.setWebChromeClient(new WebChromeClient() {
    public void onProgressChanged(WebView view, int newProgress) {
       progressBar.setProgressCompat(newProgress, true)
    }
});

这篇关于Android Studio 中 WebView 内部链接的进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆