有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java OnNestedScroll值在滚动时不更改

我试图为一个BottomNavigationView创建一个行为,该行为应该在用户向下滚动时显示,该解决方案应该像Google Material设计指南一样工作。我遇到的问题是OnNestedScroll()中的变量dyConsumed保持不变且为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"?>
<安卓.support.constraint.ConstraintLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:app="http://schemas.安卓.com/apk/res-auto"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent">

    <安卓.support.design.widget.CoordinatorLayout
        安卓:layout_width="match_parent"
        安卓:layout_height="match_parent">

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

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

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

    </安卓.support.design.widget.CoordinatorLayout>

</安卓.support.constraint.ConstraintLayout>

共 (1) 个答案

  1. # 1 楼答案

    这个问题现在已经解决了。问题是带有GridView的片段被加载到NestedScrollView中,这显然干扰了滚动功能。而不是GridView,我在加载的片段中实现了RecyclerViewGridLayoutManager。现在它工作得很好