Android的碎片 - 从一个视图移动到另一个? [英] Android Fragment - move from one View to another?
本文介绍了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 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.
这篇关于Android的碎片 - 从一个视图移动到另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文