2016-07-22 42 views
4

我有以下代碼:的WebView膨脹太慢

public View onCreateView(LayoutInflater _inflater, ViewGroup _group, Bundle _savedInstanceState) { 
    Log.w(getClass().getName(), "start"); 
    View view = _inflater.inflate(R.layout.myLayout, _group, false); 
    Log.w(getClass().getName(), "stop"); 
    return view; 
} 

案例1:

//myLayout.xml 
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <android.support.v7.widget.RecyclerView 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"/> 
</FrameLayout> 

11:50:36.370 onCreateView:啓動
11:50:36.410 onCreateView :停止
執行時間:40ms

然後我添加的WebView,不碰任何其他代碼

案例2:

//myLayout.xml 
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <android.support.v7.widget.RecyclerView 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:layout_width="match_parent" 
     android:layout_height="match_parent"/> 
</FrameLayout> 

11:51:16.645 onCreateView:啓動
11:51:16.985 onCreateView :停止
執行時間:340ms

正如你所見,巨大的差異,我看到我的界面在第二種情況下滯後。
1)這是一個預期的行爲?
2)如果是這樣,我怎麼能擺脫我的UI中的滯後?
預先感謝
編輯:
完整的日誌第二種情況:

11:51:16.645 onCreateView: start 
11:51:16.710 I/WebViewFactory: Loading com.google.android.webview version 51.0.2704.81 (code 270408100) 
11:51:16.740 W/art: Suspending all threads took: 11.333ms 
11:51:16.755 I/cr_LibraryLoader: Time to load native libraries: 2 ms (timestamps 5137-5139) 
11:51:16.755 I/cr_LibraryLoader: Expected native library version number "51.0.2704.81", actual native library version number "51.0.2704.81" 
11:51:16.765 V/WebViewChromiumFactoryProvider: Binding Chromium to main looper Looper (main, tid 1) {1d66229a} 
11:51:16.765 I/cr_LibraryLoader: Expected native library version number "51.0.2704.81", actual native library version number "51.0.2704.81" 
11:51:16.765 I/chromium: [INFO:library_loader_hooks.cc(143)] Chromium logging enabled: level = 0, default verbosity = 0 
11:51:16.795 I/cr_BrowserStartup: Initializing chromium process, singleProcess=true 
11:51:16.805 E/ApkAssets: Error while loading asset assets/natives_blob_64.bin: java.io.FileNotFoundException: assets/natives_blob_64.bin 
11:51:16.805 E/ApkAssets: Error while loading asset assets/snapshot_blob_64.bin: java.io.FileNotFoundException: assets/snapshot_blob_64.bin 
11:51:16.870 W/cr_media: Requires BLUETOOTH permission 
11:51:16.885 I/art: Rejecting re-init on previously-failed class java.lang.Class<com.android.webview.chromium.WebViewContentsClientAdapter$WebResourceErrorImpl> 
11:51:16.885 I/art: Rejecting re-init on previously-failed class java.lang.Class<com.android.webview.chromium.WebViewContentsClientAdapter$WebResourceErrorImpl> 
11:51:16.920 D/ConnectivityManager.CallbackHandler: CM callback handler got msg 524290 
11:51:16.935 11557-11588/? W/FlurryAgent: Flurry session ended 
11:51:16.945 I/art: Rejecting re-init on previously-failed class java.lang.Class<org.chromium.content.browser.FloatingWebActionModeCallback> 
11:51:16.945 I/art: Rejecting re-init on previously-failed class java.lang.Class<org.chromium.content.browser.FloatingWebActionModeCallback> 
11:51:16.970 D/cr_Ime: [InputMethodManagerWrapper.java:30] Constructor 
11:51:16.980 W/cr_AwContents: onDetachedFromWindow called when already detached. Ignoring 
11:51:16.980 D/cr_Ime: [InputMethodManagerWrapper.java:59] isActive: false 
11:51:16.985 onCreateView: stop 
+0

它似乎正在加載網頁 –

+0

嘗試加載www.google.com在您的webview並驗證加載的時間。如果加載速度更快,那麼另一個URL可能很慢 – Stallion

+0

@JoeRichard請參閱日誌,它看起來像在做某種瀏覽器初始化,但我沒有更改代碼,也沒有在任何地方調用WebView.loadUrl() –

回答

1

不要使用XML(設計)創建它在後臺線程務實創建並加載頁面時,再加入它到一個佈局。

這裏是很好Example在相對佈局中以編程方式創建它。

更新:

當我們在添加任何佈局控件,直到我們在Java代碼中使用,我們沒有做任何事情它dosent手段。

<WebView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/webView" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" /> 

當你在當時通貨膨脹上面寫的代碼在你的XML佈局,當你setContentView()WebView將其給定的屬性執行。

的WebView是一流的,將執行它的構造與給定attributes set

/** 
    * Constructs a new WebView with a Context object. 
    * 
    * @param context a Context object used to access application assets 
    */ 
    public WebView(Context context) { 
     this(context, null); 
    } 

    /** 
    * Constructs a new WebView with layout parameters. 
    * 
    * @param context a Context object used to access application assets 
    * @param attrs an AttributeSet passed to our parent 
    */ 
    public WebView(Context context, AttributeSet attrs) { 
     this(context, attrs, com.android.internal.R.attr.webViewStyle); 
    } 

    /** 
    * Constructs a new WebView with layout parameters and a default style. 
    * 
    * @param context a Context object used to access application assets 
    * @param attrs an AttributeSet passed to our parent 
    * @param defStyleAttr an attribute in the current theme that contains a 
    *  reference to a style resource that supplies default values for 
    *  the view. Can be 0 to not look for defaults. 
    */ 
    public WebView(Context context, AttributeSet attrs, int defStyleAttr) { 
     this(context, attrs, defStyleAttr, 0); 
    } 

    /** 
    * Constructs a new WebView with layout parameters and a default style. 
    * 
    * @param context a Context object used to access application assets 
    * @param attrs an AttributeSet passed to our parent 
    * @param defStyleAttr an attribute in the current theme that contains a 
    *  reference to a style resource that supplies default values for 
    *  the view. Can be 0 to not look for defaults. 
    * @param defStyleRes a resource identifier of a style resource that 
    *  supplies default values for the view, used only if 
    *  defStyleAttr is 0 or can not be found in the theme. Can be 0 
    *  to not look for defaults. 
    */ 
    public WebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     this(context, attrs, defStyleAttr, defStyleRes, null, false); 
    } 

    /** 
    * Constructs a new WebView with layout parameters and a default style. 
    * 
    * @param context a Context object used to access application assets 
    * @param attrs an AttributeSet passed to our parent 
    * @param defStyleAttr an attribute in the current theme that contains a 
    *  reference to a style resource that supplies default values for 
    *  the view. Can be 0 to not look for defaults. 
    * @param privateBrowsing whether this WebView will be initialized in 
    *      private mode 
    * 
    * @deprecated Private browsing is no longer supported directly via 
    * WebView and will be removed in a future release. Prefer using 
    * {@link WebSettings}, {@link WebViewDatabase}, {@link CookieManager} 
    * and {@link WebStorage} for fine-grained control of privacy data. 
    */ 
    @Deprecated 
    public WebView(Context context, AttributeSet attrs, int defStyleAttr, 
      boolean privateBrowsing) { 
     this(context, attrs, defStyleAttr, 0, null, privateBrowsing); 
    } 

    /** 
    * Constructs a new WebView with layout parameters, a default style and a set 
    * of custom Javscript interfaces to be added to this WebView at initialization 
    * time. This guarantees that these interfaces will be available when the JS 
    * context is initialized. 
    * 
    * @param context a Context object used to access application assets 
    * @param attrs an AttributeSet passed to our parent 
    * @param defStyleAttr an attribute in the current theme that contains a 
    *  reference to a style resource that supplies default values for 
    *  the view. Can be 0 to not look for defaults. 
    * @param javaScriptInterfaces a Map of interface names, as keys, and 
    *        object implementing those interfaces, as 
    *        values 
    * @param privateBrowsing whether this WebView will be initialized in 
    *      private mode 
    * @hide This is used internally by dumprendertree, as it requires the javaScript interfaces to 
    *  be added synchronously, before a subsequent loadUrl call takes effect. 
    */ 
    protected WebView(Context context, AttributeSet attrs, int defStyleAttr, 
      Map<String, Object> javaScriptInterfaces, boolean privateBrowsing) { 
     this(context, attrs, defStyleAttr, 0, javaScriptInterfaces, privateBrowsing); 
    } 

    /** 
    * @hide 
    */ 
    @SuppressWarnings("deprecation") // for super() call into deprecated base class constructor. 
    protected WebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes, 
      Map<String, Object> javaScriptInterfaces, boolean privateBrowsing) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
     if (context == null) { 
      throw new IllegalArgumentException("Invalid context argument"); 
     } 
     sEnforceThreadChecking = context.getApplicationInfo().targetSdkVersion >= 
       Build.VERSION_CODES.JELLY_BEAN_MR2; 
     checkThread(); 

     ensureProviderCreated(); 
     mProvider.init(javaScriptInterfaces, privateBrowsing); 
     // Post condition of creating a webview is the CookieSyncManager.getInstance() is allowed. 
     CookieSyncManager.setGetInstanceIsAllowed(); 
    } 

所有上面的代碼仍然只在你的XML佈局添加的WebView執行。

這就是爲什麼它的需要時間。

希望你能理解並在WebView類中添加斷點並檢查你的理解。

+0

thnks爲鏈接,我會稍後研究它。但問題仍然存在,爲什麼佈局變化會導致某種沉重的瀏覽器初始化工作? –

+0

是的,事實證明這是一個'WebView'消耗寶貴的時間。現在我使用懶惰實例化,所以用戶認爲它是一個網頁需要它的時間來加載:-) –