Android的碎片 - 从一个视图移动到另一个?视图、碎片、Android

由网友(趁现在)分享简介:我可以先添加一个片段视图,然后分离,然后重新连接到另一个视图?Can i first add a Fragment to a View, then "detach" it, and then "re-attach" it to another View?在code,我想:fragOne one = new fra...

我可以先添加一个片段视图,然后分离,然后重新连接到另一个视图?

Can i first add a Fragment to a View, then "detach" it, and then "re-attach" it to another View?

在code,我想:

fragOne one = new fragOne();
getSupportFragmentManager().beginTransaction()
        .add(R.id.left, one, "tag").commit();

getSupportFragmentManager().beginTransaction()
        .detach(one).commit();      // or .remove(), or .addToBackStack(null).remove()

getSupportFragmentManager().executePendingTransactions();

getSupportFragmentManager().beginTransaction()
        .add(R.id.right, one).commit();

但它引发的错误:

But it throws error:

04-05 13:28:03.492: E/AndroidRuntime(7195): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.trybackstack/com.example.trybackstack.MainActivity}: java.lang.IllegalStateException: Can't change container ID of fragment fragOne{40523130 #0 id=0x7f080000 tag}: was 2131230720 now 2131230721

感谢您的帮助!

Thanks for help!

推荐答案

我想你有这个想通了,但我dont't看到任何满意的答复。 所以,我张贴这种为别人谁可参照本在未来。

I guess you would have this figured out by now, but i dont't see any satisfactory answer. So, I'm posting this for others who may refer to this in the future.

如果你想要一个片段移动从一个视图到另一个你做到以下几点:

If you want to move a fragment from one view to another you do the following:

android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(fragment1);
fragmentTransaction.commit();
fragmentManager.executePendingTransactions();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.containerToMoveTo, fragment1);
fragmentTransaction.commit();

这样,您就不必重复片段。

This way you do not have to duplicate the fragment.

阅读全文

相关推荐

最新文章