Does not hide BottomAppBar when scrolling

时光毁灭记忆、已成空白 提交于 2021-01-29 11:36:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!