In CoordinatorLayout, the NestedScrollView is placed above the AppBarLayout

24 Views Asked by At

As the title, In CoordinatorLayout, the NestedScrollView is positioned above the AppBarLayout despite setting app:layout_behavior="@string/appbar_scrolling_view_behavior" for the NestedScrollView" as shown in the code snippet below.

What should I do to prevent the NestedScrollView from overlapping the AppBarLayout?

enter image description here

build.gradle.kts(project)

plugins {
    id("com.android.application") version "8.2.1" apply false
    id("org.jetbrains.kotlin.android") version "1.9.22" apply false
}

build.gradle.kts(app)

compileSdk = 34

    defaultConfig {
        applicationId = "com.example.myapp3"
        minSdk = 21
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }
dependencies {
    implementation("androidx.core:core-ktx:1.12.0")
    implementation("androidx.appcompat:appcompat:1.6.1")
    implementation("com.google.android.material:material:1.11.0")
    implementation("androidx.constraintlayout:constraintlayout:2.1.4")
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.1.5")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}

Here is layout

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

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

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true">

            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:id="@+id/toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:collapsedTitleTextAppearance="@style/AppbarTitle2"
                app:contentScrim="@color/colorPrimary"
                app:expandedTitleMarginStart="16dp"
                app:expandedTitleTextAppearance="@style/AppbarTitle1"
                app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
                app:title="@string/label_cart">

                <View
                    android:layout_width="match_parent"
                    android:layout_height="150dp"
                    android:background="@android:color/red" />

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

        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_anchor="@id/app_bar"
            app:layout_anchorGravity="bottom"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/holo_blue_dark"
                android:text="Here is text in NestedScrollView"/>

        </androidx.core.widget.NestedScrollView>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>
0

There are 0 best solutions below