Search code examples
androidandroid-recyclerviewandroid-coordinatorlayoutdrawerlayoutandroid-nestedscrollview

RecyclerView does not scroll inside CoordinatorLayout with DrawerLayout and bottom sheet


I have a layout containing Navigation drawer, toolbar and bottom sheet. The RecyclerView is also present in the layout which does not scroll.

Root layout is CoordinatorLayout which encapsulates -> i) DrawerLayout & ii) NestedScrollView (For bottom sheet behavior)

DrawerLayout encapsulates -> i) LinearLayout & ii) NavigationView

LinearLayout encapsulates -> i) Toolbar ii) ConstraintLayout

ConstraintLayout encapsulates -> i) ViewPager ii) TextView iii) RecyclerView (This does not scroll)

Below is my xml :

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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:fitsSystemWindows="true"
    tools:context="e.a.exlorista_customer.MainActivity">

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/colorActionBarBackground"
                android:elevation="4dp"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

            <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <android.support.v4.view.ViewPager
                    android:id="@+id/imgSliderVP"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <TextView
                    android:id="@+id/nearByStoresTV"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:text="Nearby Stores"
                    android:textSize="20sp"
                    android:textStyle="bold"
                    app:layout_constraintTop_toBottomOf="@+id/imgSliderVP"
                    app:layout_constraintStart_toStartOf="parent"
                    android:layout_marginStart="10dp"
                    android:layout_marginLeft="10dp"/>

                <android.support.v7.widget.RecyclerView
                    <!-- This does not scroll -->
                    android:id="@+id/storeRV"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_marginTop="10dp"
                    android:layout_marginStart="10dp"
                    android:layout_marginLeft="10dp"
                    app:layout_constraintTop_toBottomOf="@+id/nearByStoresTV" />

            </android.support.constraint.ConstraintLayout>

        </LinearLayout>

        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/nav_header"
            app:menu="@menu/drawer_menu" />

    </android.support.v4.widget.DrawerLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/cartSummary_MainActivityNSV"
        android:layout_width="match_parent"
        android:layout_height="65sp"
        android:clipToPadding="true"
        android:background="@color/colorActionBarBackground"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

        <LinearLayout
            android:id="@+id/cartSummary_MainActivityLL"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="10dp"
            android:layout_marginLeft="10dp">
            <TextView
                android:id="@+id/cartItemCount_MainActivityTV"
                android:layout_height="match_parent"
                android:layout_width="0dp"
                android:layout_weight="0.4"
                android:textColor="@color/colorActionBarTitleText"
                android:textStyle="bold"
                android:textSize="20sp"
                android:gravity="center_vertical"
                android:text="1 item"/>
            <Button
                android:id="@+id/proceedToCart_MainActivityB"
                android:layout_height="match_parent"
                android:layout_width="0dp"
                android:layout_weight="0.6"
                android:text="Proceed to cart"/>
        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

What I've tried :

  1. Encapsulated ConstraintLayout inside NestedScrollView
  2. Encapsulated RecyclerView inside NestedScrollView

Neither 1 nor 2 worked.

I'm new in Android so please bear with my naivety If this question involves any.


Solution

  • ConstraintLayout encapsulates -> i) ViewPager ii) TextView iii) RecyclerView (This does not scroll)

    Of course it won't.

    CoordinatorLayout should be a direct child of DrawerLayout. Also if you're using CoordinatorLayout and NestedScrollView, you may have to add AppBarLayout inside the CoordinatorLayout.

    Example layout:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context=".ui.MainActivity">
    
        <androidx.coordinatorlayout.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true">
    
            <com.google.android.material.appbar.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:elevation="0dp">
    
                <com.google.android.material.appbar.CollapsingToolbarLayout
                    android:layout_width="match_parent"
                    android:layout_height="250dp"
                    android:overScrollMode="never"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed"
                    app:titleEnabled="false">
    
                    <androidx.appcompat.widget.Toolbar
                        android:id="@+id/myToolbar"
                        android:layout_width="match_parent"
                        android:layout_height="?attr/actionBarSize"
                        app:layout_collapseMode="pin"
                        app:popupTheme="@style/AppTheme" />
    
                </com.google.android.material.appbar.CollapsingToolbarLayout>
    
            </com.google.android.material.appbar.AppBarLayout>
    
            <!-- Your nestedScrollview here which should have this attribute 
                app:layout_behavior="@string/appbar_scrolling_view_behavior" /> -->
    
        </androidx.coordinatorlayout.widget.CoordinatorLayout>
    
        <com.google.android.material.navigation.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_view_header"
            app:menu="@menu/main_drawer" />
    
    </androidx.drawerlayout.widget.DrawerLayout>
    

    Please read the documentations first. Also, nowadays you can use AndroidX namespace. You can migrate now.