问题
I can't figure out how to hide the BottomAppBar while scrolling the ViewPager content. One of the fragments for ViewPager looks like this:
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/days_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/RVDays"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
and so the layout with ViewPager looks like:
<androidx.constraintlayout.widget.ConstraintLayout 
.................>
    <com.google.android.material.tabs.TabLayout
................./>
    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@android:color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/sliding_tabs" />
    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent">
        <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/bottom_app_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:backgroundTint="@color/colorPrimary"
            app:fabAlignmentMode="center"
            app:menu="@menu/days_bottomappbar_menu"
            app:hideOnScroll="true"
            app:layout_scrollFlags="scroll|enterAlways"/>
        <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_24px"
            app:layout_anchor="@id/bottom_app_bar" />
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
I would like to achieve hiding when scrolling RecyclerView, please tell me what you need to do to get the result?
回答1:
In the fragment with RecyclerView I find BottomAppBar and put the scroll listener. Depending on the scrolling, I hide or show the BottomAppBar.
In the onCreateView method:
   mBottomAppBar = getActivity().findViewById(R.id.bottom_app_bar);
   mDayRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener(){
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy)
        {
            if (dy>0)
            {
                mBottomAppBar.performHide();
            }
            if (dy<0)
            {
                mBottomAppBar.performShow();
            }
        }
    });
来源:https://stackoverflow.com/questions/59522241/does-not-hide-bottomappbar-when-scrolling