onNestedScroll未使用自定义BottomNavigationViewBehavior调用

时间:2018-03-24 13:33:22

标签: android android-layout android-coordinatorlayout bottomnavigationview

我想在滚动BottomNavigationView上实现隐藏。谷歌寻找解决方案,因为似乎没有Android框架提供的解决方案,开箱即用,我当前的设置如下:

<android.support.design.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">

    <GridView
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="auto_fit"
        android:gravity="center"/>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottomNavigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:menu="@menu/menu_main">
    </android.support.design.widget.BottomNavigationView>
</android.support.design.widget.CoordinatorLayout>

MainActivity

CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) bottomNavigation.getLayoutParams();
layoutParams.setBehavior(new BottomNavigationViewBehavior());

BottomNavigationViewBehavior

public class BottomNavigationViewBehavior extends CoordinatorLayout.Behavior<BottomNavigationView> {

    private int height;

    @Override
    public boolean onLayoutChild(CoordinatorLayout parent, BottomNavigationView child, int layoutDirection) {
        height = child.getHeight();
        return super.onLayoutChild(parent, child, layoutDirection);
    }

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

    @Override
    public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, 
    @NonNull BottomNavigationView child,
                           @NonNull View target, int dxConsumed, int dyConsumed,
                           int dxUnconsumed, int dyUnconsumed,
                           @ViewCompat.NestedScrollType int type) {
        if (dyConsumed > 0) {
            slideDown(child);
        } else if (dyConsumed < 0) {
            slideUp(child);
        }
    }

    private void slideUp(BottomNavigationView child) {
        child.clearAnimation();
        child.animate().translationY(0).setDuration(200);
    }

    private void slideDown(BottomNavigationView child) {
        child.clearAnimation();
        child.animate().translationY(height).setDuration(200);
    }
}

每次调用onLayoutChild,父级为CoordinatorLayout,子级为BottomNavigationView。但是从不调用onStartNestedScrollonNestedScroll,因此底部导航视图不会在滚动时隐藏。尝试切换布局 - 片段,没有片段,尝试通过xml布局设置行为,没有任何效果。 我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

您的xml应该如下所示:

<android.support.design.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">

    <GridView
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="auto_fit"
        android:gravity="center"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:nestedScrollingEnabled="true"/>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottomNavigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:menu="@menu/menu_main"
        app:layout_behavior="BottomNavigationViewBehavior ">
    </android.support.design.widget.BottomNavigationView>
</android.support.design.widget.CoordinatorLayout>

然后抛弃这部分:

    CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) bottomNavigation.getLayoutParams();
    layoutParams.setBehavior(new BottomNavigationViewBehavior());

您有一个类似的示例here

P.S。您还应该更改GridView并将RecyclerView与GridLayoutManager结合使用,就像看到here