1

I am currently using CollapsingToolbar Layout in the upper portion of my layout. I want to collpase a particular portion (LinearLayout only) and keep the toolbar fixed on the top. I was able to fix the Toolbar position on top in another project and keep it from collapsing. However, for some reason, I am not able to fix the position of my toolbar. Any help will be appreciated. Here is my the XML of the layout:

<?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="wrap_content"
android:id="@+id/profile_coordinate_layout"
>

<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:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|enterAlways"
        android:layout_gravity="fill_vertical"
        android:fitsSystemWindows="true"
        app:layout_collapseMode="pin"
        app:expandedTitleMarginStart="?actionBarSize"
        app:contentScrim="?colorPrimary"
        >

        <!--|||||||||||||||||||||||||||||||||||||||||||||||||-->
        <!--|||||||||||||||Collection Profile and about||||||-->
        <!--|||||||||||||||||||||||||||||||||||||||||||||||||-->
        <LinearLayout
            android:id="@+id/ll_header"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="?actionBarSize"
            android:orientation="vertical"
            app:layout_collapseMode="pin"
            android:background="@drawable/gradient_feed_bg"
            >
            <include
                layout="@layout/item_collection_profile" />
        </LinearLayout>


        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            android:background="@drawable/dark_primary_no_radius"
            app:layout_collapseMode="pin"
            android:elevation="@dimen/margin_4dp"
            >
            <include layout="@layout/action_bar_collection_profile"/>
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>


<android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    ></android.support.v4.view.ViewPager>
</android.support.design.widget.CoordinatorLayout>

I am loading another fragment on the ViewPager. and I am using the following code to load the toolbar.

findViewById(R.id.toolbar). setBackgroundResource(R.drawable.dark_primary_no_radius);
setSupportActionBar((android.support.v7.widget.Toolbar)findViewById(R.id.toolbar));

And here is UPDATED the result:

enter image description here

As you can see, in the second image, toolbar collapsed on the top. Any help will be highly appreciated.

Thank you.

O_o
  • 1,103
  • 11
  • 36

1 Answers1

1

You need to use the app:layout_collapseMode="pin" in Toolbar only. Remove this from other components like the LinearLayout and CollapsingToolbarLayout.

<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:layout_width="match_parent"
    android:layout_height="match_parent"
  app:layout_scrollFlags="scroll|exitUntilCollapsed"
    android:layout_gravity="fill_vertical"
    android:fitsSystemWindows="true"
    app:expandedTitleMarginStart="?actionBarSize"
    app:contentScrim="?colorPrimary"
    >

    <!--|||||||||||||||||||||||||||||||||||||||||||||||||-->
    <!--|||||||||||||||Collection Profile and about||||||-->
    <!--|||||||||||||||||||||||||||||||||||||||||||||||||-->
    <LinearLayout
        android:id="@+id/ll_header"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?actionBarSize"
        android:orientation="vertical"
      app:layout_collapseMode="parallax"
        android:background="@drawable/gradient_feed_bg"
        >
        <include
            layout="@layout/item_collection_profile" />
    </LinearLayout>


    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:background="@drawable/dark_primary_no_radius"
        app:layout_collapseMode="pin"
        android:elevation="@dimen/margin_4dp"
        >
        <include layout="@layout/action_bar_collection_profile"/>
    </android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>

Darish
  • 11,032
  • 5
  • 50
  • 70
  • I did. However, The toolbar is still collapsing. – O_o Apr 01 '17 at 13:23
  • thanks man, it solved my problem, toolbar is pinned on the top however, there is a Toolbar sized empty blank gap on the bottom of the layout. How can I fix that? – O_o Apr 01 '17 at 13:32
  • 1
    if I use app:layout_scrollFlags="scroll|exitUntilCollapsed", then I have an empty place at the bottom, if I use app:layout_scrollFlags="scroll|enterAlways" then toolbar is not fixed on the top. – O_o Apr 01 '17 at 13:40
  • if your toolbar problem got fixed, feel free to accept the answer :) @aa_oo – Darish Apr 01 '17 at 14:18
  • Is it possible to fix the toolbar with app:layout_scrollFlags="scroll|enterAlways"? I want the functionality of "enterAlways" with the toolbar always fixed. – Michal Vician Aug 01 '19 at 10:40
  • @MichalVician could you please post it as a question with your code and screenshots so that we can understand your requirements better. – Darish Aug 01 '19 at 11:48
  • @Darish > Sure, I described the question on separate post: https://stackoverflow.com/questions/57308572/collapsingtoolbarlayout-with-fixed-pinned-toolbar-and-functionality-of-enteralw – Michal Vician Aug 01 '19 at 12:01