【发布时间】:2021-03-29 20:30:33
【问题描述】:
我有 3 个简单的片段(用于测试目的)
片段1
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/frameLHaupt"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".pkgActivity.FragmentHaupt">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Toolbar
android:id="@+id/toolbar3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme" />
</com.google.android.material.appbar.AppBarLayout>
</LinearLayout>
</FrameLayout>
片段2:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frameLActionBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".pkgActivity.FragmentActionBar">
<!-- TODO: Update blank fragment layout -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="149dp"
tools:layout_editor_absoluteY="174dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/nextFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="next fragment" />
<Button
android:id="@+id/lastFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="last fragment" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Bro welcome custom actionbar" />
</LinearLayout>
</RelativeLayout>
和片段3:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frameLayoutViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".pkgActivity.FragmentViewPager">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</FrameLayout>
调用 Fragment1 -> Fragment2:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.addToBackStack(null);
fragmentTransaction.replace(R.id.frameLHaupt, fragment2);
fragmentTransaction.commit();
=> 这段代码工作得很好,fragment1 框架布局被片段的框架布局取代
但是当我调用 Fragment2 -> Fragment3 时:
FragmentViewPager fragment2 = new FragmentViewPager();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.addToBackStack(null);
fragmentTransaction.replace(R.id.frameLActionBar, fragment2);
fragmentTransaction.commit();
=> Fragment3 不会被替换,而是添加到 fragment2 之上。 W
我尝试使用getActivity().getSuportFragmentManager(),但结果保持不变,fragment3 被添加到 fragment2 之上。
可能是什么原因?我错过了什么吗?
【问题讨论】:
-
只是检查:
R.id.frameLActionBar在您的视图树中是唯一的 id 吗?例如,它不会替换恰好位于顶部的其他布局? -
也可以试试
parentFragmentManager什么的... -
您似乎在使用两个不同的容器,
R.id.frameLHaupt和R.id.frameLActionBar? -
@EdwardVanRaak 是的,frameLActionbar 是独一无二的,仅在我的片段 2 中使用
-
所以通过使用不同的容器,
replace没有替换任何东西,因此片段没有被替换?还是我误解了你的问题?
标签: android android-fragments android-fragmentactivity android-framelayout