同时交换两个片段片段、两个

2023-09-06 03:54:55 作者:头猪是的念来过倒

在我的应用程序有一个活动,我将在运行再寄一次两个片段需要同时交换这两个片段。片段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.

请参阅http://stackoverflow.com/a/14951987/599614.

 
精彩推荐
图片推荐