片段中的片段片段

由网友(逢场作戏而已ベ)分享简介:我不知道这是否真的是Android API中的错误:我有一个设置,如下所示:┌----┬---------┐| | || 1 | 2 || |┌-------┐|| || ||| || 3 ||└----┴┴-------┴┘是一个菜单,它加载在右侧窗格中的片段#2(搜索画面)。是它含有的片段#3,这是一...

我不知道这是否真的是Android API中的错误:

我有一个设置,如下所示:

 ┌----┬---------┐
| | |
| 1 | 2 |
| |┌-------┐|
| || ||
| || 3 ||
└----┴┴-------┴┘
 

是一个菜单,它加载在右侧窗格中的片段#2(搜索画面)。 是它含有的片段#3,这是一个结果列表中的搜索屏幕。 在结果列表用于在几个地方(包括在它自己的权利运作的高层次片段)。

这个功能很完善的手机(其中,1和2和3是 ActivityFragment S)。

然而,当我用这个code:

  FragmentTransaction交易= getSupportFragmentManager()的BeginTransaction()。
    片段FRAG =新FragmentNumber2();
    如果(!toLoad = NULL)frag.setArguments(toLoad);
    transaction.replace(R.id.rightPane,FRAG);
    器transaction.commit();
 
电影中的感人片段

其中, R.id.leftPane R.id.rightPane <片断> S在一个水平线性布局

这是我的理解是,上述code删除这是居民的片段,然后用一个新片段替换它。辉煌......显然,这是不是会发生什么,因为当这code运行你会得到下面的异常,第二次:

  07-27 15:22:55.940:ERROR / AndroidRuntime(8105):java.lang.IllegalArgumentException异常:二进制XML文件中的行#57:重复ID 0x7f080024,标签空所致或父ID为0x0与另一个片段为FragmentNumber3
 

这是由于,因为该容器为FragmentNumber3已被复制,它不再具有唯一的ID。最初的片段并没有被破坏(?)之前,新添加的是(在我的脑海,这意味着它没有得到的替换的)。

谁能告诉我,如果这是可能的(this答案表明它不是),或者是一个错误?

解决方案

目前不支持嵌套的片段。试图把片段另一个片段的UI中会导致不确定的,并可能打破的行为。

  

更新:嵌套片段支持作为的Andr​​oid 4.2(和Android支持库修订版11):http://developer.android.com/about/versions/android-4.2.html#NestedFragments

注意(按本文档): 的注意:不能夸大布局成一个片段时,该布局包括<段>在动态添加到一个片段嵌套片段只支持

I'm wondering if this is actually a bug in the Android API:

I have a setup like so:

┌----┬---------┐
|    |         |
|  1 |    2    |
|    |┌-------┐|
|    ||       ||
|    ||   3   ||
└----┴┴-------┴┘

Is a menu which loads fragment #2 (A search screen) in the right pane. Is a search screen which contains fragment #3, which is a result list. The result list is used in several places (including as a functioning high level fragment in it's own right).

This functionality works perfectly well on a phone (Where 1 & 2 and 3 are ActivityFragments).

However, when I used this code:

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();       
    Fragment frag = new FragmentNumber2();
    if(toLoad != null) frag.setArguments(toLoad);
    transaction.replace(R.id.rightPane, frag);      
    transaction.commit();

Where R.id.leftPane and R.id.rightPane are <fragment>s in a horizontal linear layout.

It is my understanding that the above code removes the fragment which is resident and then replaces it with a new fragment. Brilliant... Obviously that isn't what happens because when this code runs the second time you get the following exception:

07-27 15:22:55.940: ERROR/AndroidRuntime(8105): Caused by: java.lang.IllegalArgumentException: Binary XML file line #57: Duplicate id 0x7f080024, tag null, or parent id 0x0 with another fragment for FragmentNumber3

This is caused because the the container for FragmentNumber3 has been duplicated and it no longer has a unique ID. The initial Fragment hasn't been destroyed (?) before the new one is added (in my mind that means it hasn't been replaced).

Can someone tell me if this is possible (this answer suggests it isn't) or is it a bug?

解决方案

Nested fragments are not currently supported. Trying to put a fragment within the UI of another fragment will result in undefined and likely broken behavior.

Update: Nested fragments are supported as of Android 4.2 (and Android Support Library rev 11) : http://developer.android.com/about/versions/android-4.2.html#NestedFragments

NOTE (as per this docs): "Note: You cannot inflate a layout into a fragment when that layout includes a <fragment>. Nested fragments are only supported when added to a fragment dynamically."

阅读全文

相关推荐

最新文章