2011-12-21 93 views
5

感谢您试图解决我的问题!XML初始屏幕 - >显示Webview

目前我的应用程序基本上使用Android Web视图加载网页。缺点是有时(连接不好)网页需要10秒以上才能加载。有什么办法可以在网页加载时创建某种形式的XML“加载”屏幕?

看看我的代码...

package com.ect; 

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

public class SomeActivity extends Activity { 

    /** Called when the activity is first created. */ 

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

     //THIS IS WHAT I WANT: setContentView(R.layout.main); 

     CookieSyncManager.createInstance(getBaseContext()); 

     // Let's display the progress in the activity title bar, like the 
     // browser app does. 


     getWindow().requestFeature(Window.FEATURE_PROGRESS); 

     WebView webview = new WebView(this); 
     setContentView(webview); 


     webview.getSettings().setJavaScriptEnabled(true); 

     final Activity activity = this; 
     webview.setWebChromeClient(new WebChromeClient() { 
     public void onProgressChanged(WebView view, int progress) { 
      // Activities and WebViews measure progress with different scales. 
      // The progress meter will automatically disappear when we reach 100% 
      activity.setProgress(progress * 1000); 
     } 
     }); 

webview.setWebViewClient(new WebViewClient() { 

public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
     //Users will be notified in case there's an error (i.e. no internet connection) 
     Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show(); 
} 

public void onPageFinished(WebView view, String url) { 
    CookieSyncManager.getInstance().sync(); 
} 
}); 

CookieSyncManager.getInstance().startSync(); 
CookieSyncManager.getInstance().sync(); 

    //This will load the webpage that we want to see 
     webview.loadUrl("http://www.web.com/"); 

    } 
} 

这是我的代码。如果仔细观察,我发表了一条评论:“这是我想要的内容:...”

该评论会调用要显示的main.xml文件,但如果我使用该评论,main.xml文件将显示,但webview从不出现...

如何显示main.xml文件,然后(一旦web视图加载)显示网页?

回答

3

您需要创建WebViewClient对象并覆盖onPageFinished()方法。然后使用webView.setWebViewClient()将上面创建的WebViewClient对象设置为WebView。现在,您可以在onPageFinished()(如setContentView())中运行一些代码,以便在加载完成后将内容视图从加载屏幕更改为网页视图。

本教程可能会对您有所帮助,虽然它不完全符合您的需求。 http://www.giantflyingsaucer.com/blog/?p=1331

4

look here,我相信这已经被回答了。它基本上覆盖了WebViewClient类的一些方法。

3

您必须使用事件onPageFinished()。

下面是官方参考Clickme

你的主要活动必须的setContentView(R.layout.main);和函数onPageFinished()必须setContentView(webview);

+0

感谢Piperoman!我完全不熟悉用Android进行开发......有什么办法可以给我一些代码吗? – Joe 2011-12-21 17:06:20

3

你可以这样做,在你的布局根框架布局(或相对布局)做这样的事情。

<Framelayout android:layout_height="fill_parent" android:layout_width="fill_parent" 
....> 

    <Webview android:layout_height="fill_parent" android:layout_width="fill_parent"/> 

    <include android:layout_height="fill_parent" android:layout_width="fill_parent" 
    layout="@loadingLayout"/> 

</Framelayout> 

当Web视图完成加载后,只需将包含的组件可见性更改为Gone即可。

我希望它有助于..

+0

你能给我所有的代码吗? – Joe 2011-12-21 21:24:02

+0

我很抱歉,但我没有所有的代码,但我会尝试并发布一个更完整的例子,当我可以。 – 2011-12-22 17:28:21