【问题标题】:Third fragment wont get replaced, just added on top第三个片段不会被替换,只是添加在顶部
【发布时间】: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.frameLHauptR.id.frameLActionBar?
  • @EdwardVanRaak 是的,frameLActionbar 是独一无二的,仅在我的片段 2 中使用
  • 所以通过使用不同的容器,replace 没有替换任何东西,因此片段没有被替换?还是我误解了你的问题?

标签: android android-fragments android-fragmentactivity android-framelayout


【解决方案1】:

您似乎在为所有片段使用不同的容器。当您替换片段时,容器 ID 表示片段所在的容器,而不是您要替换的视图的 ID。因此,使用新的片段根作为片段容器会将每个片段放入前一个片段中。

【讨论】:

  • 所以我应该总是使用:fragmentTransaction.replace(MAINCONTAINERID, fragment);?
  • 是的,如果你想实际替换它。否则,您不会替换任何东西,而是将片段添加到以前的空容器中。
  • 谢谢,我回家后试试看,如果它有效,我会告诉你,所以我可以将你的答案标记为已接受。但是后来我怎么设法用fragment1替换了activtiy framelayout。然后用 fragment2 替换 fragment1 也可以。
  • 是的,我回家后会检查。据我所知,我一直使用 getFragmentManager();我会告诉你的:)
  • 它起作用了,问题是我没有使用相同的容器 ID。谢谢您的帮助。我接受了你的回答并投了赞成票。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-24
  • 2018-08-03
  • 2019-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多