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

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

问题描述

因此,我已经在我的应用中使用ViewPager实现了Tab布局.每个选项卡显示一个片段.现在,我想从选项卡布局中的这些片段之一(称为A)中打开一个新的片段(称为B).因此,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容器用全新的片段(B).由于活动"不包含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

活动结构应如下所示-

<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天全站免登陆