且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-06-07 07:20:19

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

I think a better way would be to remove the TabLayout and ViewPager from the Activity and put them inside a top level level Fragment. Inside this top level fragment use the ChildFragmentManager to populate the ViewPager.

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

This way whenever you want to launch a new Fragment(B in your case) from any of the existing fragments then you can use your Activity's FrameLayout container to replace this whole ViewPager and TabLayout containing Fragment(A in your case) with a brand new Fragment(B). As the Activity does not contain the ViewPager and the Tablayout, they wont be visible in your new Fragment.

看看这个答案的类似方法 - https://***.com/a/29315649/4440874

Have a look at this answer for a similar approach - https://***.com/a/29315649/4440874

Activity结构应该是这样的——

Activity structure should be like this -

<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" />

片段 A 布局 -

<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"/>