【问题标题】:Launching new fragment from fragment in tab layout doesn't hide the tabs从选项卡布局中的片段启动新片段不会隐藏选项卡
【发布时间】:2016-01-30 14:59:38
【问题描述】:

所以我在我的应用程序中使用 ViewPager 实现了选项卡布局。每个选项卡显示一个片段。现在我想从选项卡布局中的这些片段之一打开一个新片段(我们称之为 B)(我们称之为 A)。所以 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 的类中,我使用这些行启动新片段,

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();

也是我的主要活动,这些片段附加到使用 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 问题,但我觉得我做错了一些事情,因为它们对我不起作用。谢谢!!

【问题讨论】:

  • 子片段如何比父片段占用更多空间??

标签: android android-fragments android-viewpager android-tablayout


【解决方案1】:

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

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

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

【讨论】:

  • 非常感谢您的回答。虽然这有效,这对我来说是一个很大的解脱,但标签的删除是可见的,即当我导航到新片段时,我可以看到 tabLayout 被删除。有什么想法吗?
  • 如何从 A -> B 替换片段,因为我想显示 tablayout 但对于第一个选项卡 fragmentA 加载然后 recyclerview 单击将再次更改 fragmentA -> fragmentB 此场景仅适用于第一个选项卡。我该怎么做?
【解决方案2】:

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

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

查看此答案以了解类似方法 - https://stackoverflow.com/a/29315649/4440874

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

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

【讨论】:

  • 非常感谢您的回答,我没有尝试过,但我觉得它会工作。但是,这不是需要更多的代码更改来处理这件事吗?我的意思是我的应用程序中有很多片段。我想我必须在所有这些片段中编写选项卡布局代码,而不是单个活动。我认为应该有一个更整洁的方法。
  • 嘿,你搞错了。您只需在一个 Fragment 中编写 TabLayout 和 Viewpager 代码。这将是您的顶级片段。现在将此片段视为您的活动。你的活动只包含这个片段,现在这个片段将包含你所有的 ViewPager 片段(子片段)而不是你的活动。恕我直言,这是一种不那么老套的方式。
  • 哦,对不起,我第一次没有得到你。嗯,看起来应该可以,让我试一试。谢谢!!
  • 这将工作 100%。如果您遇到任何问题,请告诉我。只需将所有 Activity 代码复制到此顶级 Fragment 中。您可能需要将某些函数(如 FragmentManager)更改为 ChildFragmentManager。检查我的答案中的上述链接,以了解有关在 Fragment 中实现 ViewPager 的一些想法。
  • 你是一个真正的救世主,我的朋友,你为我节省了很多时间!!上帝保佑你!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-05-31
  • 1970-01-01
  • 1970-01-01
  • 2021-11-20
  • 2020-09-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多