2015-11-03 77 views
0

我試圖替換tabLayout片段內的嵌套片段。片段佈局不完全膨脹FragmentTransaction.add /替換

我的應用程序結構遵循這種模式。

enter image description here

下面是標籤片段2

<FrameLayout 
    android:id="@+id/scene_root" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context=".Home" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <FrameLayout 
     android:id="@+id/shows_fragment_list" 
     android:tag="list" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

    <FrameLayout 
     android:id="@+id/shows_fragment_details" 
     android:tag="details" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

    <FrameLayout 
     android:id="@+id/shows_fragment_info" 
     android:tag="info" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

</FrameLayout> 

的XML我使用這個代碼的NestedFragment添加到TabFragment的FrameLayout

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{ 
    View root = inflater.inflate(R.layout.shows_fragment_layout, container, false); 

    ShowDetailsFragment newNestedFragment = new ShowDetailsFragment(); 

    android.support.v4.app.FragmentManager fragmentManager = getChildFragmentManager(); 
    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
    fragmentTransaction 
      .add(R.id.show_fragment_details, newNestedFragment) 
      .addToBackStack("null") 
      .commit(); 

    return root; 
} 

但是,當我加入將Nested Fragment嵌入到Tab Fragment中,只有我的佈局的最後部分出現,在這種情況下,它是一個列表視圖(用紅色突出顯示),但它似乎總是當佈局被相應的Java文件膨脹時,哪個元素被聲明爲最後一個元素。

enter image description here

當我吹這些佈局,因爲有自己的活動,不存在通貨膨脹的問題,所以它必須與我的實現片段的,我要去的地方錯了什麼想法?

+0

您是否想要在第一個onCreateView內添加第二個片段? – Sevle

+0

當我調用TabFragment 2的onCreateView時,那就是當我嘗試做Fragment.Transaction.add NestedFragment ..作爲Tab Fragment 2只是一組嵌套片段的shell –

回答

0

一個好主意總是使用Android設備監視器來調試您的佈局。

我還會在片段的onCreateView上添加一些日誌記錄。

+0

我的onCreateView中的所有代碼正在被讀取,我相信佈局的所有元素都是相互重疊的。 –

+1

scene_root佈局是一個FrameLayout。這意味着只顯示三個孩子佈局中的一個。你必須設置可見性去其他兩個孩子,以確保總是看到你期望的片段。 – ctarabusi

+0

ctarabusi - 雖然這當然是很好的建議,但不幸的是,這不是我的問題的原因,無論如何,謝謝。 –

0

好吧,所以我的問題是,在我的ShowDetailsFragment(嵌套片段)的XML佈局中,我有一個FrameLayout作爲根,但有兩個單獨的RelativeLayouts作爲子節點,這意味着只顯示最後一個元素,通過將這兩個相對佈局嵌套在另一個相對佈局中解決。

道具去ctarabusi指向我在正確的方向,但我仍然不確定爲什麼這隻會導致一個問題時,作爲一個片段而不是作爲一個活動充氣。