简体   繁体   English

隐藏viewpager中特定片段的工具栏和tablayout

[英]Hiding toolbar and tablayout from a particular fragment in a viewpager

I use a single activity with four Fragments . 我使用一个包含四个Fragments活动。 Fragments 1 , 2 and 3 are Viewpager Fragments and fragment 4 is a Detail Fragment that replaces fragment 3 when a list item is clicked. 片段1,23Viewpager Fragments和片段4Detail Fragment被点击列表项时,它取代片段3。 Unfortunately when the detail fragment replaces the master fragment it adds a Toolbar and back button in addition to the existing toolbar and Tablayout . 不幸的是,当细节片段替换主片段时,除了现有工具栏和Tablayout之外,它还添加了Toolbar和后退按钮。 However, I'd like to replace the existing toolbar and tablayout with this instead. 但是,我想用这个替换现有的工具栏和tablayout。

QUESTION : How would I achieve this without creating a new activity? 问题 :如果不创建新活动,我将如何实现这一目标?

Master fragment. 主片段。 Has desired toolbar and tablayout. 有所需的工具栏和tablayout。

在此输入图像描述

Details fragment. 细节片段。 Has both new toolbar (desired) and original toolbar and tablayout (undesired) 有新工具栏(所需)和原始工具栏和tablayout(不需要)

在此输入图像描述

MainActivity.XML MainActivity.XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"/>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:background="@color/black_process_2"/>

</android.support.design.widget.AppBarLayout>

DetailFragment.XML DetailFragment.XML

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.allison.viewpagermasterdetail.ItemDetailActivity"
tools:ignore="MergeRootFrame">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/app_bar_height"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:toolbarId="@+id/toolbar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/detail_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" >

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
    android:id="@+id/item_detail_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <TextView
        android:id="@+id/item_detail"
        style="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="16dp"
        android:textIsSelectable="true"/>

</android.support.v4.widget.NestedScrollView>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|start"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/stat_notify_chat"
    app:layout_anchor="@+id/item_detail_container"
    app:layout_anchorGravity="top|end" />

When you open details fragment invoke: 当您打开详细信息片段调用时:

toolbar.setVisibility(View.GONE);
tabLayout.setVisibility(View.GONE);

and

@Override
public void onBackPressed() {
    if (currentFragment instanceOf DetailFragment) {
      toolbar.setVisibility(View.VISIBILE);
      tabLayout.setVisibility(View.VISIBLE);
    }
    ...
}

Well i personally think that the best solution is to open the detail in a separate activity with it's own toolbar. 我个人认为最好的解决方案是使用它自己的工具栏在单独的活动中打开细节。 This solution would be the easiest and the cleanest in my opinion. 在我看来,这个解决方案将是最简单,最干净的。

If you would go for the detail fragment i would suggest you to set toolbar in the activity via setSupportActionBar() and modify it within the fragment with getSupportActionBar() method. 如果您要查看详细信息片段,我建议您通过setSupportActionBar()在活动中设置工具栏,并使用getSupportActionBar()方法在片段内修改它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM