使用AppBarLayout.Behavior使用NestedScrollView顺利运行AppBarLayout

Ely*_*lye 13 android android-appbarlayout nestedscrollview android-nestedscrollview

我有一个AppBarLayout和NestedScrollView.我想要NestedScrollView,只要它向下滚动,AppBarLayout也应该正常展开,而不会在AppBarLayout展开之前停止NestedScrollView; 完成第二次飞行/滚动需要完成.

我检查stackoverflow,发现这个解决方案非常相关,可以使用.但是如果是NestedScrollView,那就是RecyclerView.它位于/sf/answers/2271808521/

我基本上拿了代码并稍微改了一下,并用来检查速度> 8000以考虑将AppBarLayout作为下面的代码.

public final class FlingBehavior extends AppBarLayout.Behavior {
    private boolean isPositive;

    public FlingBehavior() {
    }

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

    @Override
    public boolean onNestedFling(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, float velocityX, float velocityY, boolean consumed) {
        if (velocityY > 0 && !isPositive || velocityY < 0 && isPositive) {
            velocityY = velocityY * -1;
        }

        if (target instanceof NestedScrollView && Math.abs(velocityY) > 8000) {
            consumed = false;
        }
        return super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed);
    }

    @Override
    public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dx, int dy, int[] consumed) {
        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed);
        isPositive = dy > 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

这有效,但并不理想.我只想在AppBarLayout(即返回consumed = false)上启动(继续)Fling ,当NestedScrollView到达其顶部的滚动时.我怎么能在onNestedFling中检查它?

谢谢.

小智 1

您应该检查 NestedScrollView 和 NestedScrollingChild

    if (target instanceof NestedScrollView && Math.abs(velocityY) > 8000) {
        consumed = false;
    }


    if (target instanceof NestedScrollingChild && Math.abs(velocityY) > 8000) {
        consumed = false;
    }
Run Code Online (Sandbox Code Playgroud)