Hide both BottomAppBar (in Activity) and Appbarlayout(in fragment) when recyclerview is scrolled

521 views Asked by At

I'm using BottomAppbar (in Activity) and Appbarlayout(in fragment) I want to hide both when recycler view is scrolled. Right now only above toolbar in AppbarLayout in hiding.

activity_main.xml

<?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"
    tools:context=".ui.mainscreen.MainActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="com.skapps.android.liboapp.ui.mainscreen.HomeFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/navigation_graph"/>

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottom_app_bar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:layout_gravity="bottom"
        app:fabCradleMargin="8dp"
        app:fabCradleRoundedCornerRadius="32dp"
        android:backgroundTint="@color/colorPrimary"
        app:menu="@menu/bottom_app_bar_menu"
        app:fabAlignmentMode="end"
        app:hideOnScroll="true"
        app:navigationIcon="@drawable/ic_baseline_menu"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_add_black_24dp"
        android:backgroundTint="@color/colorAccent"
        app:layout_anchor="@id/bottom_app_bar"
        app:maxImageSize="35dp"
        android:tint="@android:color/white"/>
    
</androidx.coordinatorlayout.widget.CoordinatorLayout>

fragment_home.xml

<?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:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/app_bar_shape"
        android:elevation="8dp"
        android:stateListAnimator="@null">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            app:layout_scrollFlags="scroll|enterAlways">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/pinBar"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <HorizontalScrollView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_marginEnd="8dp"
                    android:scrollbars="none"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent">

                    <com.google.android.material.chip.ChipGroup
                        android:id="@+id/chip_group_main"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="8dp"
                        android:layout_marginTop="2dp"
                        app:chipSpacingHorizontal="8dp" />
                </HorizontalScrollView>

                <ImageButton
                    android:id="@+id/chips_select"
                    android:layout_width="48dp"
                    android:layout_height="48dp"
                    android:background="@drawable/circle"
                    android:elevation="4dp"
                    android:contentDescription="@string/add_pin"
                    android:src="@drawable/ic_add_black_24dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

            </androidx.constraintlayout.widget.ConstraintLayout>

        </androidx.appcompat.widget.Toolbar>
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

Currently, only the above toolbar is hiding on scroll, but if I remove app:layout_scrollFlags="scroll|enterAlways" from toolbar (in fragment) then only bottomAppbar hides on scroll...I've no idea. I tried a lot, please help.

1

There are 1 answers

3
Mustaqode On BEST ANSWER

Add an interface.

interface OnScrollListener {

   fun onScrolled() 

}

And implement this interface in your activity.

class Activity1 : AppCompatActivity(), OnScrollListener {

  override fun onScrolled() {
    hideBottomAppbar()
 }

}

Add this interface in fragment's onAttach() method, and invoke the listener when you write the implementation to hide the appBarLayout in fragment.

class fragment1: Fragment() {

var listener: OnScrollListener? = null

override fun onAttach(context: Context) {
        super.onAttach(context)
        if (context == OnScrollListener)
            listener = OnScrollListener()
    }
}

//While scrolling, call this method
fun someMethod() {
  listener.onScrolled()
}

}