Keep current page rendered until the next page is loaded

核能气质少年 提交于 2021-01-28 01:51:58

问题


I have a simple app that is based on WebView.loadUrl("http://www.example.com").

When a user clicks on a URL, the default behavior is to immediately show a blank page, wait until the page is loaded, and then show this page.

I managed to show a splash screen in lieu of the blank page. However, is it possible to keep the current page rendered instead, and render the next page only when it is fully loaded?


回答1:


This is, I feel, a hacky solution. I offer it as a temporary fix until someone posts the "real" way to do this.

MainActivity class:

public class MainActivity extends Activity
{
    WebView webView1, webView2;
    EditText editText;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        editText = (EditText) findViewById(R.id.edit_url);
        webView1 = (WebView) findViewById(R.id.webView_1);
        webView2 = (WebView) findViewById(R.id.webView_2);

        webView1.setWebViewClient(client);
        webView2.setWebViewClient(client);

        webView1.loadUrl(editText.getText().toString());
    }

    private WebViewClient client = new WebViewClient()
    {
        public void onPageFinished(WebView webView, String url)
        {
            if (webView == webView1)
                webView2.setVisibility(View.GONE);
            else
                webView1.setVisibility(View.GONE);

            webView.setVisibility(View.VISIBLE);
            Toast.makeText(MainActivity.this, "WebView " + (webView == webView1 ? "One" : "Two") + " is showing", 0).show();
        }
    };

    private void loadUrl(String url)
    {
        if (webView1.getVisibility() == View.GONE)
            webView1.loadUrl(url);
        else
            webView2.loadUrl(url);
    }

    public void onClick(View v)
    {
        loadUrl(editText.getText().toString());
    }
}

main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <android.webkit.WebView android:id="@+id/webView_1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <android.webkit.WebView android:id="@+id/webView_2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:visibility="gone" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <EditText android:id="@+id/edit_url"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:layout_gravity="center_vertical"
            android:text="http://www.example.com" />

        <Button android:id="@+id/button_load"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onClick"
            android:text="Load" />

    </LinearLayout>

</LinearLayout>


来源:https://stackoverflow.com/questions/24945962/keep-current-page-rendered-until-the-next-page-is-loaded

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!