java - 我怎样才能在加载完成后只显示 webview fragment

标签 java android webview fragment

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

我尝试添加作为答案发布的代码,但没有什么区别。我认为这是因为它是一个 fragment ,我在主要 Activity WebView 中的进度工作中遇到了问题。

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;
    }
}

最佳答案

这是我的代码:

Activity :

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>

关于java - 我怎样才能在加载完成后只显示 webview fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37295729/

相关文章:

java - 使用 sax 解析器实现的设计问题

java - 使对象不可变需要遵循哪些步骤?

android - 如何压缩Camera.takepicture拍摄的照片

ios - uiwebview swift 3 页面不滚动

java - 从扩展 RTP 数据包头解码 unix 时间戳以计算延迟

android - 如何在android中从字符串创建UUID

Android - getCurrentFocus().clearFocus() 时会发生什么

java - 如何从 WebView 中获取文本

html - 在 lion 中禁用 webview 的橡皮筋滚动

java - 比较两个整数 : why is == true?