Search code examples
androidandroid-coordinatorlayout

How to place a view between two other views in a veritcal arrangement in CoordinatorLayout?


I have a layout file in which I'm using a CollapsingToolbarLayout together with a RecyclerView and works fine. Now I'm trying to place a ConstraintLayout between my AppBarLayout and the RecylerView. This is what I have tried so far:

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="272dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/collapsing_toolbar_layout"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <!--Some views-->
            </RelativeLayout>

            <androidx.appcompat.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:id="@+id/toolbar"
                app:layout_collapseMode="pin"/>
        </com.google.android.material.appbar.CollapsingToolbarLayout>
    </com.google.android.material.appbar.AppBarLayout>

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

            <!--Some views-->
    </androidx.constraintlayout.widget.ConstraintLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/beers_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

The problem is, that it remains at the bottom of the activity even if I have places it between AppBarLayout and the RecylerView. Can anyone please help me place the ConstraintLayout view between them? Thanks


Solution

  • Why dont you try put your RecyclerView into ConstraintLayout?

    Below CollapsingToolbarLayout:

    <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
            <ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
          <!--Your views here with constraints-->
    
            <androidx.recyclerview.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toBottomOf="your last view"
                     />
        </ConstraintLayout>