【问题标题】:How can i only show a webview Fragment once loading has complete加载完成后如何仅显示 webview 片段
【发布时间】:2016-05-18 09:38:19
【问题描述】:

加载完成后是否只显示我的 webview?目前我的主屏幕上有一个小的 webview 片段,但需要一段时间才能加载。我只想在加载完成后显示,因为目前它是一个白屏,直到加载完成。这看起来不是最好的。我尝试添加进度条,但由于某种原因,这在我的片段中也不起作用。

我尝试添加作为答案发布的代码,但没有任何区别。我认为这是因为它是一个片段,我在我的主要活动 webviews 中工作时遇到了问题。

public class WebViewFragment extends Fragment {

    WebView wv;
    private String NEWS = "mywebsite goes here";

    @Override
    public void onSaveInstanceState(Bundle outState) {
        wv.saveState(outState);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Toast.makeText(getContext(), "landscape", Toast.LENGTH_SHORT).show();
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            Toast.makeText(getContext(), "portrait", Toast.LENGTH_SHORT).show();
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final ProgressBar Pbar;
        View  view = inflater.inflate(R.layout.webviewfrag, null, false);

        if (savedInstanceState == null) {
            ((WebView )view.findViewById(R.id.NewsFeedWbview)).restoreState(savedInstanceState);
            wv = (WebView) view.findViewById(R.id.NewsFeedWbview);
            wv.setScrollbarFadingEnabled(false);
            Pbar = (ProgressBar) view.findViewById(R.id.pBwvf);

            final TextView tv = (TextView) view.findViewById(R.id.tV69);

            wv.setWebChromeClient(new WebChromeClient() {
                public void onProgressChanged(WebView view, int progress) {
                    if (progress < 100 && Pbar.getVisibility() == ProgressBar.GONE) {
                        Pbar.setVisibility(ProgressBar.VISIBLE);
                        tv.setVisibility(View.VISIBLE);
                    }
                    Pbar.setProgress(progress);
                    if (progress == 100) {
                        Pbar.setVisibility(ProgressBar.GONE);
                        tv.setVisibility(View.GONE);
                    }
                }
            });
            wv.getSettings().setJavaScriptEnabled(true);
            wv.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
            wv.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
            wv.getSettings().setLoadsImagesAutomatically(true);
            wv.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);

            if (Build.VERSION.SDK_INT >= 19) {
                // chromium, enable hardware acceleration
                wv.setLayerType(View.LAYER_TYPE_HARDWARE, null);
            } else {
                // older android version, disable hardware acceleration
                wv.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
            }
            wv.canGoBackOrForward(5);
            wv.getSettings().setLoadWithOverviewMode(true);
            wv.getSettings().setUseWideViewPort(true);
            wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
            wv.setPadding(0, 0, 0, 0);
            wv.loadUrl(NEWS);
        }
        return view;
    }
}

【问题讨论】:

    标签: java android webview fragment


    【解决方案1】:

    这是我的代码:

    活动:

    public class WebViewActivity extends Activity {
        private WebView webView;
        private EditText urlEditText;
        private ProgressBar progress;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_web_view);
    
            urlEditText = (EditText) findViewById(R.id.urlField);
            webView = (WebView) findViewById(R.id.webView);
            webView.setWebChromeClient(new MyWebViewClient());
    
            progress = (ProgressBar) findViewById(R.id.progressBar);
            progress.setMax(100);
    
            Button openUrl = (Button) findViewById(R.id.goButton);
            openUrl.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View view) {
                    String url = urlEditText.getText().toString();
                    if (validateUrl(url)) {
                        webView.getSettings().setJavaScriptEnabled(true);
                        webView.loadUrl(url);
    
                        WebViewActivity.this.progress.setProgress(0);
                    }
                }
    
                private boolean validateUrl(String url) {
                    return true;
                }
            });
    
        }
    
        private class MyWebViewClient extends WebChromeClient { 
            @Override
            public void onProgressChanged(WebView view, int newProgress) {          
                WebViewActivity.this.setValue(newProgress);
                super.onProgressChanged(view, newProgress);
            }
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.web_view, menu);
            return true;
        }
    
        public void setValue(int progress) {
            this.progress.setProgress(progress);        
        }
    }
    

    布局:

    <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=".WebViewActivity" >
    
        <LinearLayout
            android:id="@+id/urlContainer"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <EditText
                android:id="@+id/urlField"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:hint="Enter URL to open" />
    
            <Button
                android:id="@+id/goButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Open" />
        </LinearLayout>
    
        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/urlContainer" />
    
        <WebView
            android:id="@+id/webView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@id/progressBar" />
    
    </RelativeLayout>
    

    【讨论】:

    • 这没什么区别,谢谢,但我认为这是因为我调用的是片段而不是活动
    【解决方案2】:

    使用下面的代码

    wv.setWebViewClient(new WebViewClient() {
    
       public void onPageFinished(WebView view, String url) {
            // do your stuff here
        }
    });
    

    【讨论】:

    • 我的 Webclient 有一个单独的类
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    • 2014-05-23
    • 2016-07-24
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    相关资源
    最近更新 更多