1

谷歌搜索我看到很多聲明嵌套片段不能使用XML。現在我是Android的新手,但我的應用程序使用嵌套片段的XML。我還沒有聽衆和接口工作(也許這就是爲什麼人們說你不能使用XML),但GUI的工作原理。爲什麼無法嵌套的片段使用XML?

我的問題:什麼是我讀過關於不使用XML嵌套的碎片的意見是什麼意思?

這裏a link一個說明XML不能與嵌套片段一起使用:

下面的代碼創建3個radiogroups(每個片段)水平地佈置在頂部(在另一個片段)與一個ListView(在另一個片段)在他們下面。這些片段可以很好地控制不同顯示類型的外觀。

這裏是我的代碼:

public class SetupNew extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.ngs); 
}} 

ngs.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical"> 

<fragment android:id="@+id/frag_options_all" 
      android:layout_height="250dp"    
      android:layout_width="fill_parent" 
      android:layout_marginLeft="5dp" 
      android:layout_marginRight="5dp" 
      android:name="com.EXAMPLE.frag_class_options_all"/> 
<fragment android:id="@+id/frag_select_opponent" 
      android:layout_height="fill_parent" 
      android:layout_marginLeft="5dp" 
      android:layout_marginTop="10dp" 
      android:layout_width="fill_parent" 
      android:name="com.EXAMPLE.frag_class_opponents"/> 
</LinearLayout> 

frag_class_options_all.java

public class frag_class_options_all extends Fragment { 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.options_all, container, false); 
     return view; 
    }} 

frag_class_opponents.java

public class frag_class_opponents extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> { 
    //working contact listview 
} 

options_all.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" > 

<fragment android:id="@+id/frag_options" 
      android:layout_height="fill_parent"    
      android:layout_width="80dp" 
      android:layout_marginRight="15dp" 
      android:name="com.EXAMPLE.frag_class_options"/> 
<fragment android:id="@+id/frag_ship_limit" 
      android:layout_height="fill_parent"    
      android:layout_width="75dp" 
      android:layout_marginRight="15dp" 
      android:name="com.EXAMPLE.frag_class_limit_options"/> 
<fragment android:id="@+id/frag_allowable_ship" 
      android:layout_height="fill_parent"    
      android:layout_width="fill_parent" 
      android:name="com.EXAMPLE.frag_class_allow"/> 
</LinearLayout> 

frag_class_optionsfrag_class_limit_optionsfrag_class_allow都遵循這樣的事情:

public class frag_class_options extends Fragment{ 
RadioGroup radioGroup; 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.options_m, container, false); 

    radioGroup = (RadioGroup) view.findViewById(R.id.rg_limit);  
    return view; 
}  
} 

回答

-1

我的問題:我讀過關於不使用XML嵌套片段的評論意味着什麼?

答:我不知道。你沒有告訴我們你在哪裏找到這些評論,所以我們沒有什麼可繼續。也許評論的作者定義了他所說的「嵌套片段」的含義,也許他沒有。這不是一個標準的XML術語,我也猜不出你的問題意味着什麼。我在您的XML示例中看到稱爲「片段」的元素,但它們不是嵌套的。

也許這個問題是非常具體的機器人,在這種情況下,你不應該標記它作爲一般的XML問題。

+0

謝謝!我刪除了XML標籤並添加了一個鏈接。 – user2383867

+0

也許混淆是我對「嵌套」的定義。對我來說,嵌套是當一個片段中有另一個片段時,這就是我所擁有的。 – user2383867

3

爲了巢碎片,則必須使用一個片段的getChildFragmentManager方法。 XML中片段的默認XML實現使用導致錯誤的Activity的getFragmentManager。由於childFragmentManager只能通過Java代碼來訪問,這就是爲什麼你不能使用XML嵌套片段

查看更多信息:http://developer.android.com/about/versions/android-4.2.html#NestedFragments

+2

TL:DR注意:當佈局包含時,您無法將佈局充氣爲碎片。只有在動態添加到片段時才支持嵌套片段。 –

1

Duplicate id bug就是爲什麼你不應該在XML聲明你的嵌套片段。

在xml中聲明嵌套的片段在android中是不會被禁止的,只要你不銷燬片段並嘗試重新創建它(這發生在fragmentManager.replace裏面)所以例如如果你有一個導航抽屜然後執行以下操作:

fragManager.replace(containerId, new fragmentOne()); fragManager.replace(containerId, new fragmentTwo());

fragManager.replace(containerId, new fragmentOne());

第三替代可能會與副本ID錯誤崩潰。我不知道爲什麼會發生這種情況,但我可以向你保證,確實很令人沮喪!就我個人而言,我希望嵌套片段完全失敗,而不是部分工作!