user9305459
user9305459

Reputation:

Nested Scroll with Recycler View and AppBar

I'd like to achieve the flow like gmailenter image description here

i've tried this so far

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@android:color/transparent">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/story_toolbar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/measure_50dp"
            app:layout_scrollFlags="scroll|enterAlways" />

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

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

            <TextView
                android:id="@+id/tv_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </LinearLayout>

    </androidx.core.widget.NestedScrollView>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/measure_15dp" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Appbar hides while scroll up but doesnt appear on scroll down until its completely scrolled down. I'd like to achieve the flow like gmail, where the recyclerView is match_parent and it appears behind the toolbar when scrolled up, and toolbar hides/unhides on srcroll up and down in a nested scroll view. Any help is appreciated, Thanks!

Upvotes: 1

Views: 780

Answers (1)

Pritesh Parmar
Pritesh Parmar

Reputation: 83

You have to create nestedScrollview addOnScrollChangedListener like this

float viewScrolled = 0f

nestedScrollview.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver
.OnScrollChangedListener() {    
    if (viewScrolled < nestedScrollview.getScrollY()) {
        viewScrolled = nestedScrollview.getScrollY();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
               // Hide your app bar
            }
    }
    if (viewScrolled > nestedScrollview.getScrollY()) {
        viewScrolled = nestedScrollview.getScrollY();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (viewScrolled == 0F) {
                 // Show your app bar
            }
        }
    }

And also refer this link:-https://medium.com/@tonia.tkachuk/appbarlayout-scroll-behavior-with-layout-scrollflags-2eec41b4366b

Upvotes: 1

Related Questions