Android 以编程方式在一个活动中的两个框架布局之间传输一个片段

Android 以编程方式在一个活动中的两个框架布局之间传输一个片段,android,transactions,fragment,Android,Transactions,Fragment,在我的应用程序中(当处于横向时),在同一活动中有两个框架布局,一个在左侧,一个在右侧 左边是一个列表视图(菜单),右边应该显示列表视图(“表格”)或表单片段 现在,如果从右侧frameLayout中的“table”中选择了一个项目,则该“table”片段应转移到左侧frameLayout中,而不是该“table”片段,现在是一个新片段 因此,我的问题是如何在同一活动中以编程方式将一个片段从一个frameLayout转移到另一个frameLayout 基本上,问题是如何在一个活动内将一个片段从一个

在我的应用程序中(当处于横向时),在同一活动中有两个框架布局,一个在左侧,一个在右侧

左边是一个列表视图(菜单),右边应该显示列表视图(“表格”)或表单片段

现在,如果从右侧frameLayout中的“table”中选择了一个项目,则该“table”片段应转移到左侧frameLayout中,而不是该“table”片段,现在是一个新片段

因此,我的问题是如何在同一活动中以编程方式将一个片段从一个frameLayout转移到另一个frameLayout

基本上,问题是如何在一个活动内将一个片段从一个视图转移到另一个视图

我已尝试删除/替换为片段事务,但无法执行,因为右侧frameLayout中的片段具有该frameLayout的ID,这与右侧frameLayout不同

应用程序的流程应该类似于以所描述的方式将所有其他片段从一个框架布局切换到另一个框架布局

所以,如果有人知道怎么做,我会很感激


提前感谢。

在“表”片段中定义一个
接口
,该接口应如下所示:

public interface FragmentReplacerListerner{

    void replace();

}
在活动中实现此接口

现在,在“table”片段的onAttach()方法中,将您的活动转换为替换项。当需要替换时,在“table”片段中的侦听器上调用
replace()
方法

在活动中实现这样的回调

void replace(){
   //replace your left FrameLayout with right fragment
   // inflate the right FrameLayout with new Fragment
}

下面是一个示例应用程序的实现步骤

Ok,这就是替换方法中的内容:

@Override
public void replace() {
    TableFragmentNew tableFragmentNew = (TableFragmentNew)fragManager.findFragmentById(frameLayoutLand.getId());
    fragTransaction.remove(tableFragmentNew).commit();
    fragManager.executePendingTransactions();
    fragTransaction = fragManager.beginTransaction();
    fragTransaction.add(frameLayout.getId(), tableFragmentNew);
    fragTransaction.addToBackStack(null)
    fragTransaction.commit();
}

这样行吗?

行,经过数小时的测试,我想出了这个解决方案,解决了将碎片从一个容器移动到另一个容器的问题。这是两个
FrameLayout
视图的情况

//First create New fragment which should replace the one in the RIGHT FrameLayout
MyFragmentForRightFrameLayout myFragmentForRightFrameLayout = new MyFragmentForRightFrameLayout;
Bundle bundle = new Bundle();
//Set whatever arguments to a Bundle
bundle.set.... ;
//Find last fragment on BackStack, or find it by ID of the container in which the FragmentToMove resides, and get it so you can get its Bundle arguments  
Fragment fragmentToMove = fragManager.findFragmentByTag(fragManager.getBackStackEntryAt(fragManager.getBackStackEntryCount() - 1).getName());
//Instantiate the same type of fragment as the one you want to remove
YourFragment fragmentToAdd = new YourFragment();
//Set arguments to a new Fragment FROM the fragment you found on a backstack    
fragmentToAdd.setArguments(fragmentToMove.getArguments());
fragmentTransacion = fragmentManager.beginTransaction();
//Replace the fragment in right FrameLayout with new fragment (whichever type)
fragTransaction.replace(frameLayoutRight.getId(), myFragmentForRightFrameLayout, "Tag for this Fragment");
//And finaly replace the left FrameLayout fragment with the newly created fragment of the same type as the one you wish to move to the left FrameLayout
fragTransaction.replace(frameLayoutLeft.getId(), fragmentToAdd, "Tag for this fragment");
fragTransaction.addToBackStack("Tag for transaction" or null);
fragTransaction.commit();
好的,所以问题是当你试图将一个片段从一个容器(在我的例子中是FrameLayout)移动到另一个容器时,你不能直接通过找到一个片段并简单地替换它来完成,因为它有一个正确的FrameLayout的ID,这是片段添加到正确的FrameLayout时得到的。无法删除片段的ID,也无法设置新ID

因此,您需要创建一个相同类型的新片段,该片段没有ID,并将要删除/替换的片段中的一个包设置到该片段中

@Ekundayo福佑Funminiyi给出的解决方案的主要问题是不可能回到以前的状态。因此,这个解决方案可以导航回以前的屏幕(片段组合)


就这样。快乐编码。我希望这将对将来的人有所帮助。

谢谢您的回答,我尝试了您的解决方案,但它不起作用,我得到了与以前相同的错误:“java.lang.IllegalStateException:无法更改fragment TableFragmentNew的容器ID{cfdfb17#2 ID=0x3ea}:was 1002 now 1001”问题是该片段获取了它之前所在的frameLayout的ID,如果我尝试将其放入具有不同ID的“左侧frameLayout”中,它会引发异常…在将其添加到左侧frameLayout之前,您需要首先将其从右侧frameLayout中移除。请参阅下面我的方法replace()的实现,如果你能评论一下。。谢谢,谢谢你的例子。我试过这个例子,它说什么就行,但是,我忘了提。。。我需要将事务放回backback,因为当用户按back(在工具栏/操作栏中)时,事务必须反转。另一个问题是,当我有表“在左边”时,在从表中选择了几条记录后,当按下后退键时,它需要返回到“主菜单-表”状态,而不是返回到以前打开的所有记录。这是一个棘手的应用程序流…因此,在景观流程如下:1。左-主菜单,右-空2。(选择菜单中的某些内容)-左-主菜单、右表或窗体。3.(如果从表中选择了一条记录)-左表、右表和记录详细信息。4.(如果从表中选择了另一条记录)-左表,右表,以及记录详细信息。5.(如果按back)-左主菜单,右表。我将在今天的午餐期间为您编写一个示例代码。希望到时候不会太晚。提前谢谢。我实现了完全不同的方法,只需在一个容器中添加和删除片段。但是我想在一个活动中看到两个不同框架布局的解决方案。没问题……我会的。很好。很高兴你终于找到了一个有效的解决方案。快乐编码!