Отображение и скрытие BottomAppBar на свитке

Я пытаюсь реализовать BottomAppBar где я буду обрабатывать навигацию WebView,

<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=".MainActivity">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <WebView
            android:id="@+id/web_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottom_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:layout_behavior="com.google.android.material.bottomappbar.BottomAppBar$Behavior"
        app:hideOnScroll="true"
        app:fabAttached="true"
        app:fabAlignmentMode="end"
        app:layout_scrollFlags="scroll|enterAlways"/>

</android.support.design.widget.AppBarLayout>

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_share"
    android:tint="#fff"
    app:layout_anchor="@id/bottom_bar"
    app:backgroundTint="@color/colorPrimary"/>

Все отлично работает, но я хочу иметь возможность показать и скрыть BottomAppBar когда пользователи прокручивают вниз и вверх соответственно, как это происходит здесь.

Я не мог найти учебник по этому вопросу, поэтому кто-то реализовал решение для него?

3 ответа

Попробуйте решить app:hideOnScroll без настройки app:layout_behavior или же app:layout_scrollFlags, И удалить AppBarLayout родительский вид, так что BottomAppBar просто прямое дитя CoordinatorLayout,

вы можете использовать это, когда вы не хотите скрывать или показывать свой BottomNavigationView или BottomAppBar, добавьте одну из этих строк в свой метод прокрутки:

      ..
int height = bottomAppBar.getHeight();

//Hide
bottomAppBar.clearAnimation();
bottomAppBar.animate().translationY(height).setDuration(200);
//Show
bottomAppBar.clearAnimation();                    
bottomAppBar.animate().translationY(0).setDuration(200);
..

HideOnScroll не работает с ScrollView, используйте NestedScrollView.

Другие вопросы по тегам