Search code examples
androidandroid-layoutandroid-coordinatorlayoutandroid-nestedscrollview

NestedScrollView and AppBarLayout in a CoordinatorLayout: inability to scroll until the end of NestedScrollView


I have an activity layout that consists of:

  • A sticky non-collapsing Toolbar on the top of the page
  • A NestedScrollView that contains two headers for two RecyclerViews
  • A ConstraintLayout to position the items inside NestedScrollView
  • Two RecyclerViews

The XML file is below:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorWhite"
            app:contentInsetEnd="0dp"
            app:contentInsetStart="0dp"
            app:layout_scrollFlags="enterAlways"
            app:popupTheme="@style/Theme.AppCompat.Light">

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

                <ImageView
                    android:id="@+id/backButton"
                    style="@style/Icon"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="16dp"
                    android:layout_marginBottom="16dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:srcCompat="@drawable/ic_2" />

                <View
                    android:id="@+id/view13"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="16dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginBottom="16dp"
                    android:background="@drawable/bg_white_ellipse_with_border"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toEndOf="@+id/backButton"
                    app:layout_constraintTop_toTopOf="parent" />

                <ImageView
                    android:id="@+id/imageView21"
                    style="@style/Icon"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="16dp"
                    android:layout_marginBottom="16dp"
                    app:layout_constraintBottom_toBottomOf="@+id/view13"
                    app:layout_constraintStart_toStartOf="@+id/view13"
                    app:layout_constraintTop_toTopOf="@+id/view13"
                    app:srcCompat="@drawable/ic_1" />

                <EditText
                    android:id="@+id/searchField"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="16dp"
                    android:background="@color/fui_transparent"
                    android:ems="10"
                    android:hint="Type something"
                    android:imeOptions="actionSearch"
                    android:inputType="text"
                    android:maxLines="1"
                    app:layout_constraintBottom_toBottomOf="@+id/view13"
                    app:layout_constraintStart_toEndOf="@+id/imageView21"
                    app:layout_constraintTop_toTopOf="@+id/view13" />

            </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.appcompat.widget.Toolbar>

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

    <androidx.core.widget.NestedScrollView
        android:id="@+id/scrollView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="fill_vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

            <View
                android:id="@+id/view8"
                android:layout_width="match_parent"
                android:layout_height="5dp"
                android:background="@color/colorLightGray"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <View
                android:id="@+id/view9"
                android:layout_width="match_parent"
                android:layout_height="5dp"
                android:background="@color/colorLightGray"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="1.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/rv1" />

            <View
                android:id="@+id/view12"
                android:layout_width="match_parent"
                android:layout_height="5dp"
                android:layout_marginBottom="16dp"
                android:background="@color/colorLightGray"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/rv2" />

            <TextView
                android:id="@+id/textView21"
                style="@style/HeadingText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginTop="16dp"
                android:text="Dishes"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/view8" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rv1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:nestedScrollingEnabled="false"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView21" />

            <TextView
                android:id="@+id/textView8"
                style="@style/HeadingText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="16dp"
                android:layout_marginTop="16dp"
                android:text="Restaurants"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/view9" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rv2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="@+id/view12"
                app:layout_constraintTop_toBottomOf="@+id/textView8" />

        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

The toolbar is fixed at the top of the page as I wanted. Everything scrolls correctly. However, I can't scroll until the end of the NestedScrollView, i.e. the item was cut off and I cannot scroll to the last item in the second RecyclerView. How do I fix this?


Solution

  • For some reason, this attribute android:fillViewport="true" on CoordinatorLayout caused it to break. Removing that attribute corrects everything.