Shadman Adman
Shadman Adman

Reputation: 431

Android CollapsingToolbarLayout with two snapping point

I'm trying to have a collapsing toolbar view with two snapping point. Is there any way to do this. I'm trying for two days and couldn't find a proper solution. I want something like this:

**

Default look:

**

enter image description here

**

First snapping point:

**

enter image description here

**

Second snapping point:

**

enter image description here

Upvotes: 1

Views: 783

Answers (2)

Shadman Adman
Shadman Adman

Reputation: 431

So after reading a lot of stack question's and find out their solution has a lot of complex code(and I'm so lazy) for this simple task. I fond a simple solution:

this is my collapsing tollbar layout:

  <com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appBar"
    android:layout_width="match_parent"
    android:layout_height="258dp"
    android:background="@android:color/transparent"
    android:fitsSystemWindows="true"
    android:minHeight="60dp"
    app:elevation="0dp"
    app:layout_insetEdge="top">

    <com.google.android.material.appbar.CollapsingToolbarLayout
        android:id="@+id/collapsingToolbarLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="@android:color/transparent"
        app:layout_scrollFlags="scroll|enterAlways|snap">

        **/**** First snapping point ***************/**
        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/toolbarOne"
            android:layout_width="match_parent"
            android:layout_height="70dp"
            app:layout_collapseMode="pin" />


    </com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>

for the first snapping point you can use a toolbar with layout_collapseMode="pin" and set the size that you want for the snap position.

now for the second snapping point in your content layout witch have a nested scroll view or whatever, you can use a transparent view with the size you want for second snap position. this will avoid the app bar to rich the scroll rang and you have second snap point:

<androidx.core.widget.NestedScrollView 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:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/fragment_calendar">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        **/**** second snapping point ***************/**
    <ImageView
        android:id="@+id/daily"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.constraintlayout.widget.ConstraintLayout
        style="@style/PageBackground.White"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/daily"
        android:background="@drawable/background_top_corner_calendar">
        your content.....
         </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>

Upvotes: 0

Ole Pannier
Ole Pannier

Reputation: 3673

I think you know how to set up a Collapsing Toolbar right? If you have done that you can get two snapping points with just setting the scroll flags as follows:

<android.support.design.widget.CollapsingToolbarLayout
    ...stuff...
    app:layout_scrollFlags="scroll|enterAlways|snap">

If that doesn't work for you, I found a great post that provides a custom scrolling behaviour. Cheers

Upvotes: 1

Related Questions