【问题标题】:Why can't nested Fragments use XML?为什么嵌套片段不能使用 XML?
【发布时间】:2013-07-19 02:06:24
【问题描述】:

谷歌搜索我看到许多声明嵌套片段不能使用 XML。现在我是 Android 新手,但我的应用程序使用带有嵌套片段的 XML。我还没有让监听器和接口工作(也许这就是人们说你不能使用 XML 的原因),但是 GUI 可以工作。

我的问题:我读到的关于不使用 XML 进行嵌套片段的 cmets 是什么意思?

这里a link 声明 XML 不能与嵌套片段一起使用:

下面的代码创建了 3 个无线电组(每个在一个片段中)水平排列在顶部(在另一个片段中),在它们下方有一个列表视图(在另一个片段中)。片段可以很好地控制不同显示类型的外观。

这是我的代码:

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;
}     
}

【问题讨论】:

    标签: android-fragments android-nested-fragment android-radiobutton


    【解决方案1】:

    为了嵌套 Fragment,您必须使用 Fragment 的 getChildFragmentManager 方法。 XML 中片段的默认 XML 实现使用导致错误的 Activity 的 getFragmentManager。由于 childFragmentManager 只能通过 Java 代码访问,这就是为什么不能使用 XML 嵌套片段的原因

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

    【讨论】:

    • TL:DR 注意:当布局包含 时,您不能将布局扩展为片段。只有在动态添加到片段时才支持嵌套片段。
    【解决方案2】:

    这个Duplicate id bug 是您不应该在 xml 中声明嵌套片段的原因。

    在 xml 中声明嵌套片段在 android 中是不被禁止的,只要您不破坏片段并尝试重新创建它(这发生在 fragmentManager.replace 内部),它就可以工作。例如,如果您有一个导航抽屉和执行以下操作:

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

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

    第三次替换可能会因重复 id 错误而崩溃。我不知道为什么会发生这种情况,但我可以向您保证确实如此,这非常令人沮丧!我个人希望嵌套片段在 xml 中完全失败,而不是部分工作!

    【讨论】:

    • 谢谢!我对这种“在 XML 中没有嵌套片段”的禁令持怀疑态度,尤其是因为它似乎工作得很好。但果然,在进行了一些方向更改测试后,我看到了“重复 ID”崩溃。使我免于大量寻找错误。
    【解决方案3】:

    我的问题:我读到的关于不将 XML 用于嵌套片段的 cmets 是什么意思?

    回答:我不知道。你还没有告诉我们你在哪里找到了这些 cmets,所以我们没有什么可继续的。也许 cmets 的作者定义了“嵌套片段”一词的含义,也许他没有。这不是标准的 XML 术语,我无法从您的问题中猜出它的含义。我在您的 XML 示例中看到称为“片段”的元素,但它们不是嵌套的。

    也许该问题是针对 android 的,在这种情况下,您不应该将其标记为一般 XML 问题。

    【讨论】:

    • 谢谢!我删除了 xml 标签并添加了一个链接。
    • 也许混淆是我对“嵌套”的定义。对我来说,嵌套是当一个片段中包含另一个片段时,这就是我所拥有的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-08
    相关资源
    最近更新 更多