移动的Andr​​oid片段不同的容器无法更改片段的容器ID [英] move Android fragment to a different container Can't change container ID of fragment

查看:192
本文介绍了移动的Andr​​oid片段不同的容器无法更改片段的容器ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我想我的应用程序做在平板电脑上。
片段(0)有一些菜单将显示片段(1)...(N)是这样的:

Here is what I would like my application to do on a tablet. Fragment (0) has some menu that would display fragments (1)...(n) like this:

-----------------
|   |   |   |   |
|   |   |   |   |
|(0)| X | X | X |
|   |   |   |   |
|   |   |   |   |
-----------------
 becomes
-----------------
|   |   |   |   |
|   |   |   |   |
|(0)|(1)| X | X |
|   |   |   |   |
|   |   |   |   |
-----------------
 and then
-----------------
|   |   |   |   |
|   |   |   |   |
|(0)|(2)|(1)| X |
|   |   |   |   |
|   |   |   |   |
-----------------
 etc... 

Fragment0从不移动时,其他的被移位到右侧。碎片边缘会关闭的权利将被销毁。

Fragment0 never moves, the other ones are shifted to the right. Fragments going off the edge to the right will be destroyed.

所以我设置我有一个水平的LinearLayout XML布局和含4的FrameLayout用正确的ID(fragment0 ... fragment3)

So I setup my XML layout with a horizontal LinearLayout and containing 4 FrameLayout with the proper IDs (fragment0... fragment3)

我可以实例化和显示fragment0然后片段1,但是我不能后它向右移动,我得到:

I can instantiate and display fragment0 and then fragment1, but I am not able to shift it to the right after, I get:

ERROR/AndroidRuntime(343): FATAL EXCEPTION: main
java.lang.IllegalStateException: Can't change container ID of fragment ...

我发现的唯一有关的问题是<一个href=\"http://stackoverflow.com/questions/9906254/illegalstateexception-cant-change-container-id-of-fragment\">this 之一和一个,想尽一切没有运气提供了不同的解决方案

The only related questions I have found are this one and that one, tried all the different solutions offered with no luck.

尝试 FragmentTransaction.remove()然后按。新增(),试图。替换(),尝试过它们以不同的顺序,并通过(甚至试图提交两倍有人建议)提交中途,试着拨打 addToBackStack() ...仍然没有运气。

Tried FragmentTransaction.remove() followed by .add(), tried .replace(), tried them in different orders and to commit half-way through (even trying to commit twice as somebody suggested), tried to call addToBackStack() ... still no luck.

问题是,是否有可能用FragmentTransaction移动片段是这样的。如果是的话,我究竟做错了(和奖金,是否有可能进行动画吗?)。如果没有,这将是实现这一正确的方法是什么?

Question is whether it is possible to move the fragments like this with a FragmentTransaction. If yes, what am I doing wrong (and bonus, is it possible to animate that?). If no, what would be the proper way to implement this?

请注意,我不希望每次(每个做一些查询网站上,可以采取一些时间)重新实例化片段。这是可能的所有数据中提取回活动重新之一,但我宁愿不这样做,如果可能的...

Note that I don't want to re-instantiate the fragments every time (each do some queries on the web that can take some time). It's possible to extract all the data back to the activity to recreate one, but I'd rather not do that if possible...

推荐答案

我不知道这是不是你想要很什么,但我做了关于你的问题有点样本。基本上,你会做使用布局文件移权,其为每个移动片段的包装容器。在code样品是有点大的一个答案,所以我已经张贴在进入一个要点,你可以找到这里。在该样本preSS每个 ListFragment 的项目(片段1 - >片段2 - >片段3)。看到的行为

I don't know if this is quite what you want but I made a little sample regarding your problem. Basically you'll be doing the right shifting using the layout file, having a wrapper container for each of those shifting fragments. The code sample is a little to big for an answer so I've posted it into a gist that you can find here. In that sample press each of the ListFragment's items(Fragment 1 -> Fragment 2 -> fragment 3) to see the behavior.

这篇关于移动的Andr​​oid片段不同的容器无法更改片段的容器ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆