'I want to hide my appBarLayout and BottomNavigationView on scroll of fragment recycler?

I want to hide my appBarLayout and BottomNavigationView at the same time when I scroll up the recycler and show again when I scroll down the recycler.

In my case I have an activity in which appBarLayout and BottomNavigation is but my recycler is in fragment which is replaced at the container place, when I scroll recycler from fragment I want to hide appBarLayout and BottomNavigationView from my main activity.

Here is my MainActivity class XML file.

<?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_student"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

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

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/mainToolbarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="visible"
            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="?attr/actionBarSize"
                android:background="@color/colorPrimary"
                app:contentInsetLeft="0dp"
                app:contentInsetStart="0dp"
                android:visibility="visible"
                app:contentInsetStartWithNavigation="0dp"
                app:elevation="8dp"
                app:layout_scrollFlags="scroll|enterAlways|snap"
                app:titleTextColor="@color/white">

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

                    <ImageView
                        android:id="@+id/actionback"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:adjustViewBounds="true"
                        android:onClick="onClick"
                        android:padding="@dimen/_18sp"
                        android:tint="@color/white"
                        android:visibility="gone"
                        app:srcCompat="@drawable/ic_back_arrow"
                        tools:visibility="visible" />

                    <ImageView
                        android:id="@+id/actionSideMenu"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:adjustViewBounds="true"
                        android:padding="@dimen/_14dp"
                        android:tint="@color/white"
                        android:visibility="gone"
                        app:srcCompat="@drawable/ic_menu"
                        tools:visibility="visible" />

                    <LinearLayout
                        android:id="@+id/llSearchButton"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="horizontal"
                        android:visibility="visible">

                        <TextView
                            android:id="@+id/titleText"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:gravity="center|start"
                            android:paddingLeft="@dimen/_12dp"
                            android:paddingRight="@dimen/_12dp"
                            android:singleLine="true"
                            android:textColor="@color/white"
                            android:textSize="@dimen/_20sp" />

                        <ImageView
                            android:id="@+id/actionSearch"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:adjustViewBounds="true"
                            android:paddingHorizontal="10dp"
                            android:paddingVertical="@dimen/_16dp"
                            android:tint="@color/white"
                            android:visibility="gone"
                            app:srcCompat="@drawable/ic_search"
                            tools:visibility="visible" />

                        <FrameLayout
                            android:id="@+id/notificationFrameLayout"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:visibility="gone"
                            tools:visibility="visible">

                            <ImageView
                                android:id="@+id/actionNotification"
                                android:layout_width="wrap_content"
                                android:layout_height="match_parent"
                                android:adjustViewBounds="true"
                                android:paddingHorizontal="10dp"
                                android:paddingVertical="@dimen/_16dp"
                                android:tint="@color/white"
                                app:srcCompat="@drawable/ic_notification"
                                tools:visibility="visible" />

                            <TextView
                                android:id="@+id/notification_badge"
                                android:layout_width="20dp"
                                android:layout_height="20dp"
                                android:layout_gravity="right|end|top"
                                android:layout_marginTop="5dp"
                                android:layout_marginEnd="5dp"
                                android:layout_marginRight="-5dp"
                                android:background="@drawable/badge_background"
                                android:gravity="center"
                                android:padding="3dp"
                                android:text="999"
                                android:textColor="@android:color/white"
                                android:textSize="10sp"
                                android:visibility="gone" />

                        </FrameLayout>

                        <ImageView
                            android:id="@+id/actionFilter"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:adjustViewBounds="true"
                            android:paddingHorizontal="10dp"
                            android:paddingVertical="@dimen/_16dp"
                            android:tint="@color/white"
                            android:visibility="gone"
                            app:srcCompat="@drawable/ic_filter"
                            tools:visibility="gone" />

                        <ImageView
                            android:id="@+id/actionReadAllNotification"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:adjustViewBounds="true"
                            android:paddingHorizontal="10dp"
                            android:paddingVertical="@dimen/_16dp"
                            android:tint="@color/white"
                            android:visibility="visible"
                            app:srcCompat="@drawable/ic_read_all_notifications"
                            tools:visibility="visible" />


                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/llSearchRow"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@color/colorPrimary_student"
                        android:gravity="center_vertical"
                        android:orientation="horizontal"
                        android:visibility="gone"
                        app:elevation="0dp">

                        <RelativeLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="10dp"
                            android:layout_marginTop="5dp"
                            android:layout_marginRight="10dp"
                            android:layout_marginBottom="5dp"
                            android:background="@drawable/bg_rounded_white">

                            <ImageView
                                android:id="@+id/ivSearch"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_centerVertical="true"
                                android:paddingLeft="5dp"
                                android:src="@drawable/ic_search"
                                android:tint="@color/white" />

                            <EditText
                                android:id="@+id/etSearch"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_centerVertical="true"
                                android:layout_toLeftOf="@+id/ivClearTxt"
                                android:layout_toRightOf="@+id/ivSearch"
                                android:background="@color/transparent"
                                android:ellipsize="end"
                                android:hint="Search"
                                android:imeOptions="actionSearch"
                                android:maxLength="50"
                                android:padding="7dp"
                                android:singleLine="true"
                                android:textSize="15sp" />

                            <ImageView
                                android:id="@+id/ivClearTxt"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_alignParentRight="true"
                                android:layout_centerVertical="true"
                                android:padding="5dp"
                                android:src="@drawable/ic_close"
                                android:tint="@color/dividerColor"
                                android:visibility="visible" />

                        </RelativeLayout>

                    </LinearLayout>

                </LinearLayout>

            </androidx.appcompat.widget.Toolbar>

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

<!--        <com.google.android.material.card.MaterialCardView-->
<!--            android:layout_width="match_parent"-->
<!--            android:layout_height="wrap_content"-->
<!--            android:elevation="10dp"-->
<!--            android:layout_gravity="center"-->
<!--            android:layout_marginBottom="@dimen/_60dp"-->
<!--            app:cardBackgroundColor="@color/Grey_200"-->
<!--            android:layout_marginTop="@dimen/_50dp"-->
<!--            app:cardCornerRadius="@dimen/_20dp">-->

            <FrameLayout
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">

            </FrameLayout>
<!--        </com.google.android.material.card.MaterialCardView>-->

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNav"
            android:layout_width="match_parent"
            android:layout_alignParentBottom="true"
            android:layout_height="?attr/actionBarSize"
            android:layout_gravity="bottom"
            android:background="@color/white"
            app:itemIconTint="@color/bottom_nav_selector"
            app:layout_behavior=".test.BottomNavigationFABBehavior"
            app:menu="@menu/bottom_menu_student" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

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

</androidx.drawerlayout.widget.DrawerLayout>


and here is my fragment XML file

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <include layout="@layout/student_view_purchase_plan"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
            android:id="@+id/main_swiperefresh"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.digibooks.elearning.Student.videoplayer.VideoPlayerRecyclerView
                android:id="@+id/main_recycler"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clipToPadding="false" />

        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

        <ProgressBar
            android:id="@+id/main_progress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" />

        <include layout="@layout/student_error_layout" />

        <com.digibooks.elearning.Student.FAB.FloatingActionButton
            android:id="@+id/fabNewPost"
            style="@style/FabButtonsStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/fab_add"
            android:layout_gravity="end|bottom"
            android:layout_marginRight="@dimen/_12dp"
            android:visibility="gone"
            android:layout_marginBottom="@dimen/_12dp"
            fab:fab_label="@string/MyDoubt" />


        <FrameLayout
            android:id="@+id/flt_scan_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:onClick="onClick"
            android:visibility="gone"
            android:layout_alignParentRight="true"
            android:layout_gravity="bottom|right">

            <com.google.android.material.floatingactionbutton.FloatingActionButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="25dp"
                android:elevation="10dp"
                android:tint="@color/colorPrimaryDark" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Scan"
                android:elevation="16dp"
                android:textSize="@dimen/_16dp"
                android:textColor="@android:color/white"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </FrameLayout>

    </FrameLayout>

</LinearLayout>

and through this method I am sending callback of recycler scroll to my main activity

            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                setVideoPlayer(true);
                Log.i(TAG, "onScrolled: dy :- "+dy);
                Log.i(TAG, "onScrolled: recyclerView :- "+recyclerView);
                Log.i(TAG, "onScrolled: dx :- "+dx);
                if (dy > 0){
                    mainStudentActivity.hideToolbar();
                }else{
                    mainStudentActivity.showToolbar();
                }
            }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source