从选项卡布局中的片段启动新片段不会隐藏选项卡

Launching new fragment from fragment in tab layout doesn't hide the tabs

所以我在我的应用程序中使用 ViewPager 实现了 Tab 布局。每个选项卡显示一个片段。现在我想从选项卡布局(我们称之为 A)中的这些片段之一打开一个新片段(我们称之为 B)。所以 A 在选项卡布局中,但我希望 B 占据整个屏幕。我什至可以做到,但我面临的问题很小。新片段不会占据整个屏幕。相反,它只是替换了之前的片段,看起来它是选项卡布局的一部分。

这是片段 A 的布局(我从中启动新片段)。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/parent">

        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:dividerHeight="0dp" />
    </FrameLayout>

</LinearLayout>

在片段 A 的 class 中,我使用这些行启动了新片段,

Bundle bundle = new Bundle();
FragmentTransaction fragmentTransaction = getActivity()
           .getSupportFragmentManager()
           .beginTransaction();
Fragment fragmentB = new FragmentB();
fragmentB.setArguments(bundle);
fragmentTransaction.replace(R.id.parent, fragmentB);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

也是我的主要 activity,这些片段附加到的那个使用 viewPager 实现了 tavLayout。

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/tab_layout"/>

我最终看到了一个新片段(片段 B),但它是选项卡布局的一部分,而我想隐藏选项卡布局。请帮助我,我经历了很多 SO 问题,但我觉得我做错了什么,因为它们对我不起作用。谢谢!!

我假设您能够成功地从 FragA 替换为 FragB。

我的建议是 -> 将 TabLayout 放入 AppBarLayout,设置 AppBarLayout 的属性] 作为 android:animateLayoutChanges="true".

稍后在从 A->B 替换您的片段时,使用 setVisibility(View.GONE) 以编程方式隐藏您的 TabLayout

我认为更好的方法是从 Activity 中删除 TabLayout 和 ViewPager 并将它们放在顶级 Fragment 中。在这个顶级片段中使用 ChildFragmentManager 来填充 ViewPager。

这样,每当您想从任何现有片段启动新片段(在您的情况下为 B)时,您就可以使用 Activity 的 FrameLayout 容器来替换包含片段的整个 ViewPager 和 TabLayout (在您的情况下为 A)带有全新的 Fragment(B)。由于 Activity 不包含 ViewPager 和 Tablayout,因此它们在您的新 Fragment 中不可见。

查看类似方法的答案 -

Activity 结构应该是这样的 -

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

<FrameLayout 
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

片段布局 -

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/tab_layout"/>