从选项卡布局中的片段启动新片段不会隐藏选项卡 [英] Launching new fragment from fragment in tab layout doesn't hide the tabs

查看:38
本文介绍了从选项卡布局中的片段启动新片段不会隐藏选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I have implemented Tab layout with ViewPager in my app. Each tab shows a fragment. Now I want to open a new fragment(lets call it B) from one of these fragments in the tab layout(lets call it A). So A is in tab layout but I want B to take up whole screen. I am even able to do it but I am facing little problem. The new fragment doesn't take up the whole screen. Instead it just replaces the previous fragment, and it looks like it was a part of the tab layout.

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

This is the layout of the fragment A (from which I launch the new fragment).

<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 的类中,我使用这些行启动新片段,

In the class of fragment A, I launch new fragment using theses lines,

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.

Also my main activity, the one that these fragments are attached to implements tavLayout using viewPager.

<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 问题,但我觉得我做错了一些事情,因为它们对我不起作用.谢谢!!

I end up seeing a new fragment (the fragment B) but as a part of the Tab Layout while I want to hide the tab layout. Please help me, I went through many SO questions but I felt that I am doing some thing wrong as they didn't work for me. Thanks !!

推荐答案

我认为更好的方法是从 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://stackoverflow.com/a/29315649/4440874

Have a look at this answer for a similar approach - https://stackoverflow.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"/>

这篇关于从选项卡布局中的片段启动新片段不会隐藏选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆