本文介绍了使用AppBarLayout.Behavior使用NestedScrollView平稳地移动AppBarLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AppBarLayout和NestedScrollView.无论何时向下滚动,我都希望NestedScrollView,AppBarLayout也应该优雅地展开,而NestedScrollView不会在AppBarLayout展开之前停止;要完成该操作,需要第二个Flight/Scroll.

I have an AppBarLayout and NestedScrollView. I want the NestedScrollView whenever it scroll down, the AppBarLayout should also expand gracefully, without the NestedScrollView stop right before the AppBarLayout Expand; a second Flight/Scroll is required to get that done.

我检查了stackoverflow,发现此解决方案非常相关,可以使用.但是,如果使用NestedScrollView,则为RecyclerView.它位于 https://stackoverflow.com/a/32454407/3286489

I check stackoverflow and found this solution pretty related, and could be used. But instead if NestedScrollView, it is RecyclerView. It is in https://stackoverflow.com/a/32454407/3286489

我基本上采用代码并将其稍作更改,并用于检查速度> 8000,以考虑同时将AppBarLayout转换为以下代码.

I basically take the code and changed it slightly, and used to check the velocity >8000 to consider also Fling the AppBarLayout as code below.

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;
    }
}

这有效,但不理想.当NestedScrollView到达其滚动的顶部时,我只想在AppBarLayout上开始(继续)加载(即返回consumed = false).我如何在onNestedFling中检查呢?

This works, but not ideal. I'm only want to start (continue) the Fling on the AppBarLayout (i.e. return consumed = false), when the NestedScrollView has reach the top of it's scroll. How could I check that in onNestedFling?

谢谢.

推荐答案

此存储库中的库已解决了问题.

( https://developer.android.com/topic/library/support-library/setup.html )

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
}

这篇关于使用AppBarLayout.Behavior使用NestedScrollView平稳地移动AppBarLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 08:29