滚动

时间:2017-11-20 10:49:43

标签: java android

我正在尝试为用户向下滚动时应该消失的BottomNavigationView创建行为,该解决方案应该像Google Material Design Guide一样工作。我遇到的问题是dyConsumed中的变量OnNestedScroll()保持不变,尽管用户当前正在滚动并且输入了该函数,但仍为0。是什么导致dyConsumed保持不变?为什么在向上或向下滚动时值没有变化?

继承BottomNavigationView

的行为
public class BottomNavigationViewBehaviour 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, @NonNull 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, 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);
    }
}   

这是布局的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="56dp"
            android:fillViewport="true"
            app:layout_constraintBottom_toTopOf="parent"
            app:layout_constraintTop_toTopOf="parent">

        </android.support.v4.widget.NestedScrollView>
        <include layout="@layout/navigation_bottom"/>

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

</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

此问题现已解决。问题是带有GridView的片段被加载到NestedScrollView中,这显然会干扰滚动功能。而不是GridView我在正在加载的片段中实现了RecyclerView GridLayoutManager。现在它完美无缺!