AppBarLayout.ScrollingViewBehavior 的数学原理是怎样的?

How AppBarLayout.ScrollingViewBehavior's math works?

我会在 运行 滚动变化时观察 AppBarLayout.ScrollingViewBehavior 中 offsetChildAsNeeded 方法的数学流程。

因为它是私有方法而私有是mOffsetDelta,如何以编程方式监控它们?

(不清楚该方法是如何使用offset的。)

private void offsetChildAsNeeded(CoordinatorLayout parent, View child, View dependency) {
    final CoordinatorLayout.Behavior behavior =
                ((CoordinatorLayout.LayoutParams) dependency.getLayoutParams()).getBehavior();
    if (behavior instanceof Behavior) {
        // Offset the child, pinning it to the bottom the header-dependency, maintaining
        // any vertical gap, and overlap
        final Behavior ablBehavior = (Behavior) behavior;
        final int offset = ablBehavior.getTopBottomOffsetForScrollingSibling();
        ViewCompat.offsetTopAndBottom(child, (dependency.getBottom() - child.getTop())
                + ablBehavior.mOffsetDelta
                + getVerticalLayoutGap()
                - getOverlapPixelsForOffset(dependency));
        }
    }

注意:欢迎并接受那些解释 getTopBottomOffsetForScrollingSibling()(dependency.getBottom() - child.getTop()) 的数学逻辑细节的回复mOffsetDelta

的内容

您可以 reverse-engineer 此代码,但最终它是学术性的,因为我们凡人(即 non-Google)程序员无法访问此处显示的值和方法。我猜他们认为我们实际上可以 使用 的库越少,我们提交的错误报告就越少。叹息

但这里有一个简短的解释:

首先是代码行

    final int offset = ablBehavior.getTopBottomOffsetForScrollingSibling();

似乎是早期修订的残留物,因为 offset 从未实际使用过。新的表达式在更多情况下必须更准确一些。

ViewCompat.offsetTopAndBottom()不是集合(绝对)运算,而是加法(相对)运算。因此,让我们假设正常的逻辑,并考虑这种行为实质上将滚动视图直接放在应用栏布局的下方。通常,应用栏的底部和滚动视图的顶部具有相同的值。由于应用栏布局(依赖项)已更改并且滚动视图(child)尚未(尚未),因此

    dependency.getBottom() - child.getTop()

是child的垂直偏移需要调整的相对量。

如果我对代码的理解是正确的,那么在应用栏布局具有偏移插值器的情况下,应用栏布局行为中的 mOffsetDelta 只是 non-zero。通常,应用栏 本身 不会以视差方式移动,因此对于我们关心的几乎所有情况,mOffsetDelta 都是零。 getVerticalLayoutGapgetOverlapPixelsForOffset 处理像 overlapTop.

这样的布局参数

但事实证明,只需这样做,您就可以在自己的行为子类中没有这些边缘情况的情况下完成大部分工作:

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child,
                                          View dependency) {
        // get the bottom of the app bar layout
        int bottom = dependency.getBottom();

        // position the top of the scrolling view there
        return setTopAndBottomOffset(bottom);
    }

我发现使用绝对偏移比使用相对偏移更容易一些。所以实现滚动行为主要是确定依赖视图在哪里以及滚动视图需要基于它的位置。