2

I have an Activity with DrawerLayout which contains a Fragment containing a RecyclerView. How can I let the ToolBar scroll away on scrolling in the RecyclerView without having to load all items in the RecyclerView?

Is that even possible?

I tried to wrap the CoordinatorLayout and the FrameLayout (fragment container) into a NestedScrollView and disabling nested scrolling in the RecyclerView, which then caused that the RecyclerView loaded all content and was much slower.

activity_main.xml

<?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:openDrawer="start">

    <include
            layout="@layout/app_bar_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header_main"
            app:menu="@menu/activity_main_drawer"/>

</androidx.drawerlayout.widget.DrawerLayout>

app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        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"
        tools:context=".ui.MainActivity">

        <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
        >

            <androidx.coordinatorlayout.widget.CoordinatorLayout
                    android:id="@+id/toolbar_container"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
            >
                    <com.google.android.material.appbar.AppBarLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:theme="@style/AppTheme.AppBarOverlay"
                            android:elevation="0dp"
                    >

                        <androidx.appcompat.widget.Toolbar
                                android:id="@+id/toolbar"
                                android:layout_width="match_parent"
                                android:layout_height="?attr/actionBarSize"
                                android:background="?attr/colorPrimary"
                                app:popupTheme="@style/AppTheme.PopupOverlay"
                                app:layout_scrollFlags="scroll"
                        />
                    </com.google.android.material.appbar.AppBarLayout>

            </androidx.coordinatorlayout.widget.CoordinatorLayout>

            <FrameLayout
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    app:layout_constraintTop_toBottomOf="@id/toolbar_container"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
            >
                <include layout="@layout/content_main"/>
            </FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

content_main.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"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:showIn="@layout/app_bar_main"
        tools:context=".ui.MainActivity">

    <FrameLayout
            android:id="@+id/container"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
    />

</androidx.constraintlayout.widget.ConstraintLayout>

fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        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"
>

    <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/alimentaries"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:nestedScrollingEnabled="false"
            tools:listitem="@layout/item_alimentary"
    />

</androidx.constraintlayout.widget.ConstraintLayout>
sneps
  • 387
  • 4
  • 13
  • https://stackoverflow.com/questions/13559275/hiding-the-actionbar-on-recyclerview-listview-onscroll/17767691#17767691 I think this link will help you out!!! – Dipakk Sharma Aug 23 '19 at 05:27
  • `ConstrainLayout` have no implementation about toolbar scroll behavior. Set the behavior into your recycler-view and run. And also you need to use `CollapsingToolbarLayout` to get the scroll behavior. :) – Md Sufi Khan Aug 23 '19 at 05:46
  • I forgot to mention that it should work like on current Gmail app for example – sneps Aug 23 '19 at 12:46

1 Answers1

0

I think there are two ways of achieving the behavior that you want. You can use a collapsing toolbar layout and the other way is to make your activity fullscreen and add the layout that you want to collapse on a scroll, as the header of the RecyclerView.

Here is a nice example of implementing a collapsing toolbar in Android.

In the second approach, you need to have a fullscreen activity first and then you have to implement your RecyclerView in such a way that is has a header. Here's how you can add a header to your RecyclerView. Add the following in your AndroidManifest.xml to have the fullscreen activity.

<activity android:name=".ActivityName"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"/>
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98