BottomNavigationView没有响应协调器布局

时间:2017-10-10 06:12:34

标签: android android-viewpager android-coordinatorlayout bottomnavigationview

我的活动中页面底部有ViewPagerBottomNavigationViewViewPager由几个片段组成。 以下是活动的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include layout="@layout/toolbar_with_tab_strip_layout" />

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinatorlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <com.tf.eros.faythTv.widgets.NonScrollingViewPager
        android:id="@+id/home_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation_home"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@color/white"
        app:elevation="16dp"
        app:itemIconTint="@drawable/home_bottombar_icon_color_selector"
       app:itemTextColor="@drawable/home_bottombar_icon_color_selector"
        app:menu="@menu/bottom_navigation_main" />
    </android.support.design.widget.CoordinatorLayout>
</LinearLayout>

我还添加了一个BottomNavigationViewBehavior类,用于在viewpager中滚动片段时隐藏/取消隐藏BottomNavigationView

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(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) {
        slideDown(child);
    } else if (dy < 0) {
        slideUp(child);
    }
}

@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, BottomNavigationView child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
    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);
}
}

在活动中,我在onCreate方法

中添加了这两行
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) bottomNavigationView.getLayoutParams();  
layoutParams.setBehavior(new BottomNavigationViewBehavior()); 

目的是在页面向下滚动时隐藏BottomNavigationView(dy> 0)并在向上滚动时使其显示(dy <0)。

现在代码的问题是隐藏和取消隐藏BottomNavigationView并不是自发的滚动。滚动时有时它根本不隐藏。

0 个答案:

没有答案