同时交换两个片段 [英] Swap two fragment simultaneously

查看:127
本文介绍了同时交换两个片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序有一个活动,我将在运行再寄一次两个片段需要同时交换这两个片段。片段1由一个按钮,我希望当我点击该按钮片段1移动到屏幕右侧和其他片段的活性的左侧。

In my application i have one activity and i am adding two fragments at run time.I need to swap these two fragment simultaneously. Fragment 1 consist a button and i want when i click that button fragment 1 moves to right side of the screen and other fragment to the left side of the activity.

在按钮的onclick方法,我想这样的事情

In the onClick method of the button i tried something like this

    @Override
    public void onClick(View v) {
         FragmentTransaction ft = getFragmentManager().beginTransaction();
         Fragment newFragment = getFragmentManager().findFragmentById(R.id.pageA);
         ft.remove(newFragment);
         Fragment newFragmentB = getFragmentManager().findFragmentById(R.id.pageB);
         ft.remove(newFragmentB);
         ft.add(R.id.pageB, newFragment);
         ft.add(R.id.pageA, newFragmentB);
         ft.addToBackStack(null);
         ft.commit();
     }

但我收到以下错误

But i am getting the following error

 java.lang.IllegalStateException: Can't change container ID of fragment PageA{40653da0   #0 id=0x7f060001}: was 2131099649 now 2131099650

我想是这样的,当我点击页面A上的按钮,然后页面A和页面B的位置应相互交换。

I want something like this when i click the button on Page A then Position of Page A and PageB should swap with each other.

推荐答案

我贴了一个解决这个问题的一个类似的问题。我的方法是重新创建片段,而是先保存它,并重新把它应用到新的实例保持状态。

I posted a solution to this problem to a similar question. My approach is to re-create the fragment, but keeping the state by saving it first and re-applying it to the new instance.

请参阅<一href="http://stackoverflow.com/a/14951987/599614">http://stackoverflow.com/a/14951987/599614.

这篇关于同时交换两个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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