2014-07-22 42 views
1

XML代碼,其被替換替換不替換內容的片段?含的FrameLayout

<FrameLayout 
    android:id="@+id/content_frame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="110dp" 
     android:layout_marginTop="30dp" 
     android:src="@drawable/icon_new" 
     android:onClick="new_booking" /> 

    <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="40dp" 
     android:layout_marginLeft="110dp"   
     android:src="@drawable/icon_history" 
     android:onClick="click_history" /> 
    <LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    <ImageView 
     android:id="@+id/imageView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="40dp" 
     android:layout_marginTop="30dp" 
     android:src="@drawable/icon_rates" 
     android:onClick="click_rates"/> 

    <ImageView 
     android:id="@+id/imageView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="30dp" 
     android:layout_marginLeft="80dp"   
     android:src="@drawable/icon_customer_care" 
     android:onClick="click_customer" /> 
    </LinearLayout> 
</LinearLayout> 
</FrameLayout> 

代碼用於與另一幀ContactFragment取代上述幀。

android.support.v4.app.FragmentManager fragmentManager =  getSupportFragmentManager(); 
    android.support.v4.app.FragmentTransaction fragmentTransaction =  fragmentManager.beginTransaction(); 
    ContactFragment fragment = new ContactFragment(); 
    fragmentTransaction.replace(R.id.content_frame, fragment); 
    fragmentTransaction.commit(); 
    mDrawerList.setItemChecked(5, true); 
    mDrawerLayout.closeDrawer(mDrawerList);  

內容框架沒有被替換,而重寫......即我能夠看到它們包含在contentframe圖片...交易被稱爲後還是一樣。

+0

你沒有更換任何的FrameLayout內容,無論是什麼你在做的是加載另一個片段,如果你想這樣做有兩種方法:1)使默認佈局內容爲一個片段,並加載另一個片段時,只需刪除第一個片段。 2)在將片段加載到此幀佈局之前刪除所有視圖。 –

+0

是iam用另一個片段(Conatct Fragment)替換整個片段(內容片段)..但內容片段的內容也是可見的。根據我它應該被替換 – Yatin

+0

你不刪除以前加載的片段,這就是爲什麼前一個片段的內容顯示,試試這個:'transaction.remove(removeFragment); transaction.add(R.id.content_frame,newFragment);'。讓我知道它是否工作。 –

回答

0

我不確定這是否是正確的做法..但它的工作。 你必須做以下操作 在java文件中創建FrameLayout的實例。

FrameLayout frameLayout = (FrameLayout) findViewById(R.id.content_frame); 

然後以此爲folllows:

android.support.v4.app.FragmentManager fragmentManager =  getSupportFragmentManager(); 
    android.support.v4.app.FragmentTransaction fragmentTransaction =  fragmentManager.beginTransaction(); 
    frameLayout.removeAllViews(); 
    ContactFragment fragment = new ContactFragment(); 
    fragmentTransaction.replace(R.id.content_frame, fragment); 
    fragmentTransaction.commit(); 
    mDrawerList.setItemChecked(5, true); 
    mDrawerLayout.closeDrawer(mDrawerList); 

frameLayout.removeAllViews()這條線將刪除哪些,還有在FrameLayout裏的所有XML組件