简体   繁体   中英

How to collapse the main fragment toolbar and the main toolbar?

I have a collapsing toolbar inside a fragment, so the toolbar collapses but it looks awkward because the fragment toolbar and the main toolbar still exists. How so I collapse that too when I swipe up? I have considered shifting my code to an activity instead of a fragment but that would spoil the whole thing.

This is my fragment XML code

<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:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

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

        <ImageView
            android:src="@drawable/profilepic"
            android:id="@+id/profilePic"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop"
            app:layout_collapseMode="parallax"
            app:layout_collapseParallaxMultiplier="0.7" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin">


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


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

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

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/a"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <TextView
            android:id="@+id/name"
            android:layout_width="70dp"
            android:layout_height="30dp"
            android:layout_marginTop="12dp"
            android:text="Name"
            android:textColor="@color/white"
            android:textSize="20dp" />

        <TextView
            android:id="@+id/status"
            android:layout_width="70dp"
            android:layout_height="30dp"
            android:layout_marginTop="45dp"
            android:text="Status"
            android:textColor="@color/silver"
            android:textSize="20dp" />

        <TextView
            android:id="@+id/email"
            android:layout_width="70dp"
            android:layout_height="30dp"
            android:layout_marginTop="21dp"
            android:text="Email"
            android:textSize="20dp" />

        <TextView
            android:id="@+id/quote"
            android:layout_width="70dp"
            android:layout_height="30dp"
            android:layout_marginTop="27dp"
            android:text="Quote"
            android:textSize="20dp" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Number"
            android:textSize="20dp" />

    </LinearLayout>

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

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

Screenshot - http://ibb.co/euoqwn

I want the toolbar which says MESSAGE+ and the fragment pager to collapse

To hide main activity toolbar and show only collapsing toolbar you need to set below themes to activity in manifest:-

<activity
    android:name=".MainActivity"
    android:theme="@style/Theme.AppCompat.Translucent" >
</activity>

In style.xml file create theme:- Theme.Translucent

<style name="Theme.AppCompat.Translucent">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowAnimationStyle">@android:style/Animation</item>
</style>

You can also use Theme.NoActionBar as alternative

 <style name = "NoActionBar" parent = "@android:style/Theme.Holo.Light">
        <item name = "android:windowActionBar">false</item>
        <item name = "android:windowNoTitle">true</item>
    </style>

I hope it help you.

Paste this code to your fragment class

@Override
public void onResume() {
    super.onResume();
    ((AppCompatActivity)getActivity()).getSupportActionBar().hide();
}
@Override
public void onStop() {
    super.onStop();
    ((AppCompatActivity)getActivity()).getSupportActionBar().show();
}

For your main Activity, set a theme with a toolbar. For your fragments set a style/theme with no toolbar. Except for the one fragment you DO want to have one: set that one to a style/theme with a toolbar.

Then programmatically control that fragment's toolbar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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