2013-12-11 54 views
0

我有一些麻煩理解和使用片段我有3個按鈕和1個片段視圖使用按鈕作爲選項卡我試圖給片段一個特定的類,當點擊一個按鈕什麼我的確是這樣的:使用一個片段視圖替換多個片段類

<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/linearLayout1" 
    android:layout_alignTop="@+id/linearLayout1" > 

    <Button 
     android:id="@+id/Tab1" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="100dp" 
     android:layout_height="50dp" 
     android:text="Tab 1" android:background="@drawable/btn"/> 

    <Button 
     android:id="@+id/Tab2" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="100dp" 
     android:layout_height="50dp" 
     android:text="Tab 2" android:onClick="switchToTab2" /> 

    <Button 
     android:id="@+id/Tab3" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="100dp" 
     android:layout_height="50dp" 
     android:text="Tab 3" android:onClick="switchToTab3" /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/linearLayout1" 
    android:layout_alignParentBottom="true" 
    android:layout_alignRight="@+id/linearLayout1" 
    android:layout_below="@+id/linearLayout1" > 

    <fragment 
     android:id="@+id/fragment1" 
     android:name="com.example.newproject.fragmentclass" 
     android:layout_width="match_parent" 
     android:layout_height="394dp" /> 



</LinearLayout> 

java類:

fragmentclass FRT1;
fragmentclass2 frt2;
fragment3class frt3;
FragmentTransaction ft;

@Override 
protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_fragment_tabs_exp); 

     ft = getSupportFragmentManager().beginTransaction(); 
     frt1 = new fragmentclass();     
     frt2 = new fragmentclass2(); 
     frt3 = new fragment3class(); 

     //ft.add(R.id.fragment1,frt1); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.fragment_tabs_exp, menu); 
    return true; 
} 
public void switchToTab2 (View v){ 
    ft.replace(R.id.fragment1, frt2).commit(); 
} 

public void switchToTab3 (View v) { 
    ft.replace(R.id.fragment1, frt3).commit(); 
} 

當我點擊按鈕2片段的Class2是成功可見,除了默認類的片段仍然可見以及反正是有隱藏它,使新的片段類活躍

回答

1

在您的xml文件中添加片段將使您的應用程序變爲靜態,並且您無法動態更改它。

如果您想要動態添加碎片,那麼您需要在您的xml文件中添加FrameLayout,並在該佈局中動態加載所有碎片,並在碎片逐個加載時相互替換。

下面做改變你的佈局:

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/linearLayout1" 
    android:layout_alignParentBottom="true" 
    android:layout_alignRight="@+id/linearLayout1" 
    android:layout_below="@+id/linearLayout1" > 

    <FrameLayout 
     android:id="@+id/fragment1" 
     android:layout_width="match_parent" 
     android:layout_height="394dp" /> 

</LinearLayout> 
+0

我點擊按鈕時遇到了麻煩,出現錯誤提示說:無法執行活動的方法。當我點擊標籤1一切都沒問題,因爲frameLayout是空的,但當我點擊tab2錯誤提出任何想法? – Sora

+0

請在您的問題中發佈您的整個logcat錯誤。 – GrIsHu

1

刪除fragment1從你的XML文件(因此在佈局創建時默認容器是空的),並從代碼中(即在父代片段的onCreateView()中或在Activity的onCreate()中)添加它,因爲如果片段是XML佈局的一部分,則不能從片段中移除片段文件。

如果您打算只有一個孩子,我還會將容器更改爲FrameLayout而不是LinearLayout

+0

所以我應該從XML動態地從代碼,而不是創建片段? – Sora

+1

+1因爲比那個更快一點。 @索拉這個答案是正確的答案。不要在XML中添加片段,而只能通過代碼添加片段,以便替換有效。 – HpTerm