簡體   English   中英

使用AppBarLayout在CoordinatorLayout中滾動顯示/隱藏BottomNavigationView

[英]Show/hide BottomNavigationView on scroll in CoordinatorLayout with AppBarLayout

我試圖在單個CoordinatorLayout同時使用AppBarLayoutBottomNavigationLayout ,並且我很難根據材料指南隱藏BottomNavigationLayout

我的意思是這樣的:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="false">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_insetEdge="top"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:layout_scrollFlags="scroll|enterAlways"/>
    </android.support.design.widget.AppBarLayout>


    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_gravity="bottom"
        app:menu="@menu/menu_bottom_navigation"/>

    <FrameLayout
        android:id="@+id/content_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

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

如您所見,我還有一個FrameLayout ,用於包含具有實際內容的片段。 目前, BottomNavigationView沒有默認/內置行為 - 既不是視圖本身,也不是它的兄弟。 現有的appbar_scrolling_view_behavior與appbar協調處理內容視圖,但忽略其他兄弟。

我正在尋找一種隱藏的方法,並在滾動時顯示appbar和底部導航視圖。

經過一兩天的搜索后,我決定使用附加到BottomNavigationView的自定義Behavior 它的主要思想是檢測何時滾動BottomNavigationView的兄弟,以便它可以隱藏BottomNavigationView。 像這樣的東西:

public class BottomNavigationBehavior extends CoordinatorLayout.Behavior<BottomNavigationView> {

    public BottomNavigationBehavior() {
        super();
    }

    public BottomNavigationBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, BottomNavigationView child, View dependency) {
        boolean dependsOn = dependency instanceof FrameLayout;
        return dependsOn;
    }

    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, BottomNavigationView child, View directTargetChild, View target, int nestedScrollAxes) {
        return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
    }

    @Override
    public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, BottomNavigationView child, View target, int dx, int dy, int[] consumed) {
        if(dy < 0) {
            showBottomNavigationView(child);
        }
        else if(dy > 0) {
            hideBottomNavigationView(child);
        }
    }

    private void hideBottomNavigationView(BottomNavigationView view) {
        view.animate().translationY(view.getHeight());
    }

    private void showBottomNavigationView(BottomNavigationView view) {
        view.animate().translationY(0);
    }
}

如您所見,我正在使用簡單的ViewPropertyAnimator ,它是使用子視圖的animate方法獲得的。 這導致了一個簡單的動畫,它與AppBarLayout的行為並不完全匹配,但它足夠好看,同時它很容易實現。

我希望在某些時候Android團隊會在支持庫中為BottomNavigationView添加一個默認行為,所以我認為花更多的時間來完全復制AppBarLayout的行為是不合理的。

編輯 (2018年4月):有關onStartNestedScrollonNestedPreScroll及其新版本的詳細說明,請參閱注釋部分。

您還可以使用HideBottomViewOnScrollBehavior 此行為基本上以相同的方式工作,但也處理取消正在運行的任何現在應該更好的性能動畫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM