简体   繁体   English

如何为 webview 中的每个页面加载添加微调器加载

[英]How to add spinner loading for every pags loading in webview

I am a beginner and building webview app with splash screen in AIED.我是一名初学者,在 AIED 中构建带有闪屏的 webview 应用程序。

I want to add spinner loading for every page loading in webview.我想为 webview 中的每个页面加载添加微调器加载。 I mean when this webview loads after the splash screen, I can see home page of website when I open any other page.我的意思是当这个 webview 在启动画面后加载时,当我打开任何其他页面时,我可以看到网站的主页。 While loading that page I want to show spinner in the center of screen.在加载该页面时,我想在屏幕中央显示微调器。 When the page load the spinner should disappear.当页面加载时微调器应该消失。

I not know where to add the specific code.我不知道在哪里添加特定的代码。

Here is mainactivity.java这是mainactivity.java

package com.mystore.mominbaba;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

public class MainActivity extends Activity {

    //private WebView webView = null;
    private WebView mWebView;
    private ImageView mSplashView;

    private ValueCallback <Uri> mUploadMessage;
    public ValueCallback <Uri[]> uploadMessage;
    public static final int REQUEST_SELECT_FILE = 100;
    private final static int FILECHOOSER_RESULTCODE = 1;

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

        mWebView = (WebView) findViewById(R.id.webview);
        mSplashView = (ImageView) findViewById(R.id.splash);
        mWebView.getSettings().setJavaScriptEnabled(true); // enable javascript
        mWebView.getSettings().setLoadWithOverviewMode(true); //sets Overview to true
        mWebView.getSettings().setUseWideViewPort(true); //sets wideviewport deleting whitespsce
        mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        mWebView.getSettings().setAllowFileAccess(true);
        mWebView.setWebViewClient(new WebViewClient() {


            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                mSplashView.setVisibility(View.GONE);
                mWebView.setVisibility(View.VISIBLE);

            }
        });

        mWebView.loadUrl("https://www.mominbaba.com");
        mWebView.setVisibility(View.GONE);
        mSplashView.setVisibility(View.VISIBLE);


        mWebView.setWebChromeClient(new WebChromeClient() {
            // For 3.0+ Devices (Start)
            // onActivityResult attached before constructor
            protected void openFileChooser(ValueCallback uploadMsg, String acceptType) {
                mUploadMessage = uploadMsg;
                Intent i = new Intent(Intent.ACTION_GET_CONTENT);
                i.addCategory(Intent.CATEGORY_OPENABLE);
                i.setType("image/*");
                startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE);
            }


            // For Lollipop 5.0+ Devices
            public boolean onShowFileChooser(WebView mWebView, ValueCallback <Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
                if (uploadMessage != null) {
                    uploadMessage.onReceiveValue(null);
                    uploadMessage = null;
                }

                uploadMessage = filePathCallback;

                Intent intent = null;
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                    intent = fileChooserParams.createIntent();
                }
                try {
                    startActivityForResult(intent, REQUEST_SELECT_FILE);
                } catch (ActivityNotFoundException e) {
                    uploadMessage = null;
                    return false;
                }
                return true;
            }

            //For Android 4.1 only
            protected void openFileChooser(ValueCallback <Uri> uploadMsg, String acceptType, String capture) {
                mUploadMessage = uploadMsg;
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setType("image/*");
                startActivityForResult(Intent.createChooser(intent, "File Browser"), FILECHOOSER_RESULTCODE);
            }

            protected void openFileChooser(ValueCallback <Uri> uploadMsg) {
                mUploadMessage = uploadMsg;
                Intent i = new Intent(Intent.ACTION_GET_CONTENT);
                i.addCategory(Intent.CATEGORY_OPENABLE);
                i.setType("image/*");
                startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (requestCode == REQUEST_SELECT_FILE) {
                if (uploadMessage == null)
                    return;
                uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
                uploadMessage = null;
            }
        } else if (requestCode == FILECHOOSER_RESULTCODE) {
            if (null == mUploadMessage)
                return;
            // Use MainActivity.RESULT_OK if you're implementing WebView inside Fragment
            // Use RESULT_OK only if you're implementing WebView inside an Activity
            Uri result = intent == null || resultCode != MainActivity.RESULT_OK ? null : intent.getData();
            mUploadMessage.onReceiveValue(result);
            mUploadMessage = null;
        }
    }


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


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }

        return super.
        `onKeyDown` (keyCode, event);
    }
}

Here is activity main.xml这是活动 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"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/splash"
        android:scaleType="fitXY"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:visibility="gone"
        android:src="@drawable/image_1"/>

    <WebView
        android:id="@+id/webview"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible"/>

</RelativeLayout>

Big Caveat: This solution won't completely hide the white screen(loading) on websites that has to download and load.js files especially if they are big.大警告:此解决方案不会完全隐藏必须下载和 load.js 文件的网站上的白屏(加载),尤其是在它们很大的情况下。

First, you are using onPageFinished incorrectly.首先,您错误地使用onPageFinished onPageFinished is always going to be called after every page load has finished. onPageFinished总是会在每次页面加载完成后被调用。 This means if you are navigating on your website, it will call onPageFinished every after you navigate a URL and the webview has finished loading it.这意味着如果您在您的网站上导航,它会在您每次导航 URL 并且 webview 完成加载后调用onPageFinished

Here's an idea on how to do this:这是有关如何执行此操作的想法:

First create an HTML page and design on how you want your spinner would look like.首先创建一个 HTML 页面并设计您希望您的微调器的外观。 Save the HTML template as a string HTMLSpinner .将 HTML 模板保存为字符串HTMLSpinner

Then create a second webview and load the HTML template string to it然后创建第二个 webview 并将 HTML 模板字符串加载到它

public WebView spinner;
spinner.loadData(HTMLSpinner, "text/html", "UTF-8");

Then modify your main mWebView.setWebViewClient like this然后像这样修改你的主mWebView.setWebViewClient

        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageStarted(WebView view, final String url, Bitmap favicon) {
                mWebView.setVisibility(View.GONE);
                spinner.setVisibility(View.VISIBLE);

                // YOUR LANDING PAGE
                if(url.equals("https://www.mominbaba.com")){
                    mWebView.setVisibility(View.GONE);
                    mSplashView.setVisibility(View.VISIBLE);

                }

            }

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

                mWebView.setVisibility(View.VISIBLE);
                spinner.setVisibility(View.GONE);

                // YOUR LANDING PAGE
                if(url.equals("https://www.mominbaba.com")){

                    mWebView.setVisibility(View.GONE);
                    mSplashView.setVisibility(View.VISIBLE);


                } 

            }

        });

That should swap the current webview that you see from the main webview to the loader.这应该将您从主 webview 看到的当前 webview 交换到装载机。 I have chosen to use a second webview because the asset that was given to was an animated SVG.我选择使用第二个 webview,因为所提供的资产是动画 SVG。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM