App bar scrolling with Flexible space not working

I am working on an application in which I have to show a list of content. Plus I have to add App bar scrolling with Flexible space. Everything is working fine but the scroll behavior is not working. Already search the internet but nothing works.

Here is my xml:

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

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="200dp">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/sky_blue"
            app:expandedTitleGravity="top"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="70dp"
                app:layout_collapseMode="pin">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="35dp"
                    android:layout_marginStart="50dp"
                    android:layout_marginTop="30dp"
                    android:layout_marginEnd="50dp"
                    android:background="@drawable/rounded_edittext"
                    android:gravity="center_vertical"
                    android:orientation="horizontal">

                    <ImageView
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:layout_marginStart="20dp"
                        android:src="@drawable/ic_search" />

                    <com.google.android.material.textfield.TextInputEditText
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_gravity="center_vertical"
                        android:layout_marginStart="10dp"
                        android:layout_marginEnd="20dp"
                        android:background="@null"
                        android:fontFamily="@font/opensans_regular"
                        android:hint="Search"
                        android:inputType="text"
                        android:maxLines="1"
                        android:textColor="@color/white"
                        android:textColorHint="@color/white"
                        android:textSize="15sp" />
                </LinearLayout>
            </androidx.appcompat.widget.Toolbar>
        </com.google.android.material.appbar.CollapsingToolbarLayout>
    </com.google.android.material.appbar.AppBarLayout>

<androidx.recyclerview.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listView"
    tools:listitem="@layout/custom_movie_row"
    android:layout_marginTop="200dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    /></androidx.constraintlayout.widget.ConstraintLayout>

This is what my activity looks like with the above code. Activity

The Recycler view is working fine but it does not collapse the AppBar.

This is the behavior I want in my Activity

enter image description here

Answer

Take your parent viewgroup as Coordinator layout instead of Constraint layout.