2014-10-10 50 views
0

所有佈局我有很多的* .xml佈局項ViewFlipper(108是精確的)我的活動佈局內的問題是,當我啓動應用程序,所有的佈局中ViewFlipper被分配。的Android ViewFlipper - 防止分配內部ViewFlipper

有沒有辦法阻止在啓動時分配所有ViewFlipper項目,並只在viewflipper項目被加載時才分配它?

我main_activity.xml代碼

<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" 
    android:background="#ffececec" 
    tools:context=".MyActivity"> 

    <ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/flipper" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

     <include android:id="@+id/pageMainWin" layout="@layout/main_win" /> 
     // Other ViewFlipper items 

    </ViewFlipper> 

</RelativeLayout> 
+0

爲什麼你有108個佈局項目? – ianhanniballake 2014-10-10 20:32:54

+0

你可以在主版面項目主要設置爲'layout.setVisiblity(View.GONE)'趁其不使用,然後提供一個'View.VISIBLE'當你再次需要它,如果你真的需要。這將不會消耗資源,直到他們需要。比如當你向左或向右平移時。 – 2014-10-10 20:35:21

回答

0

嘗試在你的佈局使用viewStub XML標籤:

<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/flipper" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <ViewStub 
      android:inflatedId="@+id/pageMainWin" 
      android:layout="@layout/main_win" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

</ViewFlipper> 
在你的代碼

然後,編程膨脹額外的XML佈局資源

ViewStub stub = (ViewStub) findViewById(R.id.pageMainWin); 
View inflated = stub.inflate(); 

更多信息在這裏viewstubs:http://developer.android.com/reference/android/view/ViewStub.html