我可以先将一个片段添加到视图中,然后"分离"它,然后将其"重新附加"到另一个视图中吗?
在代码中,我想:
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();
Run Code Online (Sandbox Code Playgroud)
但它会引发错误:
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
Run Code Online (Sandbox Code Playgroud)
感谢帮助!
android android-layout android-fragments android-fragmentactivity fragmenttransaction
以下是我希望我的应用程序在平板电脑上执行的操作.Fragment(0)有一些菜单会显示片段(1)...(n),如下所示:
-----------------
| | | | |
| | | | |
|(0)| X | X | X |
| | | | |
| | | | |
-----------------
becomes
-----------------
| | | | |
| | | | |
|(0)|(1)| X | X |
| | | | |
| | | | |
-----------------
and then
-----------------
| | | | |
| | | | |
|(0)|(2)|(1)| X |
| | | | |
| …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我有一个活动,我在运行时添加两个片段.我需要同时交换这两个片段.片段1包含一个按钮,我希望当我点击该按钮时,片段1移动到屏幕的右侧,而其他片段移动到活动的左侧.
在按钮的onClick方法我试过这样的事情
@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();
}
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误
java.lang.IllegalStateException: Can't change container ID of fragment PageA{40653da0 #0 id=0x7f060001}: was 2131099649 now 2131099650
Run Code Online (Sandbox Code Playgroud)
当我点击页面A上的按钮然后页面A和页面B的位置应相互交换时,我想要这样的东西.

我遇到了不同布局和设备配置更改的问题.
应用程序有3个feeds_activity布局(默认,横向和平板电脑).
默认:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/feeds_fragment_container">
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
景观:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="?android:dividerHorizontal"
android:baselineAligned="false">
<FrameLayout
android:id="@+id/feeds_fragment_container"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2" />
<FrameLayout
android:id="@+id/subnodes_fragment_container"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
片剂:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="?android:dividerHorizontal"
android:baselineAligned="false">
<FrameLayout
android:id="@+id/feeds_fragment_container"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2" />
<FrameLayout
android:id="@+id/subnodes_fragment_container"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
这是一个onCreate方法的活动:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.feeds_activity);
if (findViewById(R.id.subnodes_fragment_container) != null) {
SubNodesFragment subNodesFragment = (SubNodesFragment) getSupportFragmentManager()
.findFragmentByTag(getString(R.string.subnodes_fragment_tag)); …Run Code Online (Sandbox Code Playgroud) android android-layout android-fragments android-orientation