当Webview在后台加载时显示启动画面

时间:2018-02-06 16:09:45

标签: java android android-layout webview

我正在使用下面的代码用Webview填充我的主要活动,但因为加载页面需要一定的时间,所以它显示为空白。所以我想在页面加载完成之前为我的Webview显示启动画面。我没有使用布局活动中的任何webview。

package com.faraksoch.sagar.facebook;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends Activity {

private WebView mWebview = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    mWebview = new WebView(this);

    mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript

    final Activity activity = this;

    mWebview.setWebViewClient(new WebViewClient() {
        public void onPageFinished(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
        }
    });


    mWebview.loadUrl("http://www.google.com//");
    setContentView(mWebview);

}

}

我知道这段代码应该可行,但是当我将它们组合起来时......它将不起作用

  WebView wv = (WebView) findViewById(R.id.webView1);
    wv.getSettings().setJavaScriptEnabled(true);
    wv.setWebViewClient(new WebViewClient() {

        ...

        @Override
        public void onPageFinished(WebView view, String url) {
            //hide loading image
            findViewById(R.id.imageLoading1).setVisibility(View.GONE);
            //show webview
            findViewById(R.id.webView1).setVisibility(View.VISIBLE);
        }


    });     
    wv.loadUrl("http://yoururlhere.com");

任何帮助将不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分:0)

确保webview在加载之前的可见性不是GONE。

请参阅以下代码。

public class WebViewActivity extends AppCompatActivity {

    private WebView mWebView;
    private ImageView mSplashView;

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

        mWebView = (WebView) findViewById(R.id.webview);
        mSplashView = (ImageView) findViewById(R.id.splash_view);
        mWebView.getSettings().setJavaScriptEnabled(true); // enable javascript

        mWebView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                mSplashView.setVisibility(View.GONE);
                mWebView.setVisibility(View.VISIBLE);
                Toast.makeText(getBaseContext(), "Page Loaded.", Toast.LENGTH_SHORT).show();
            }
        });

        mWebView.loadUrl("http://yoururlhere.com");
        mWebView.setVisibility(View.GONE);
        mSplashView.setVisibility(View.VISIBLE);
    }
}

<强>布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    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"
        android:visibility="visible"
        />

    <ImageView
        android:id="@+id/splash_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@mipmap/ic_launcher"
        android:visibility="gone"
        />


</android.support.constraint.ConstraintLayout>