Search code examples
androidkotlinandroid-recyclerviewandroid-coordinatorlayout

Scroll Views above RecyclerView


I have some view above my RecyclerView and i want to make them all scrollable as follows:

Recyclerview

And when i scroll, the toggle button should scroll as shown here:

Scrolled

My layout is as follows:

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

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <ToggleButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textOn="Active"
                android:textOff="Completed"/>

        <fragment
                android:id="@id/Container_fromHomeActivity_BottomAppBarFragments"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:defaultNavHost="@bool/Navigation_NavigationHost_Default"
                app:navGraph="@navigation/bottomappbar_navigation"
                tools:layout="@layout/fragment_to_do"/>

    </LinearLayout>

    <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@id/BottomAppBar_fromHomeActivity_Main"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:fabAlignmentMode="center"
            app:navigationIcon="@drawable/ic_menu_dark"
            app:menu="@menu/menu_bottomappbar_main"/>


    <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@id/FAB_fromHomeActivity_BottomAppBarAttached"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_add_dark"
            android:backgroundTint="@color/colorAccent"
            app:layout_anchor="@id/BottomAppBar_fromHomeActivity_Main"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

But when i am trying to scroll, the toggle button is staying fixed on the screen. Only the recyclerview is scrolling behind the toggle button.

Can anyone help ?


Solution

  • First, determine whether your recylerview is scrolling up or down. and the Hide/show and animate the toggle layout according to that scrolling.

    for example

    • Animation up for scrolling up and
    • Animation down for scrolling down.

    Here is my code for determining recylerviews up and down

    mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if (dy > 0 && toggelLayout.getVisibility() == View.VISIBLE) {
                //Hide
            } else if (dy < 0 && toggelLayout.getVisibility() !=View.VISIBLE) {
               //Show
            }
        }
    });