【问题标题】:How AppBarLayout.ScrollingViewBehavior's math works?AppBarLayout.ScrollingViewBehavior 的数学是如何工作的?
【发布时间】:2017-02-22 14:59:33
【问题描述】:

我会在运行滚动更改时观察 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 内容的回复

【问题讨论】:

  • 最好解释一下你的最终目标,而不是你计划实现它的方式。
  • 最终目标是了解滚动更改时行为的工作原理
  • AppBarLayout.ScrollingViewBehavior 类复制到您的项目中,进行更改,将您的AppBarLayoutapp:layout_behavior 指向它,获利。
  • 我正在尝试,但是对于父 HeaderScrollingViewBehavior 和 AppBarLayout.Behavior 中的 mOffsetDelta,原始包中没有可见性。
  • 我非常想在AppBarLayout.ScrollingViewBehavior 的子类中使用offsetChildAsNeeded 的结果。有这方面的更新吗?

标签: android android-coordinatorlayout android-appbarlayout


【解决方案1】:

您可以对这段代码进行逆向工程,但最终它是学术性的,因为我们这些普通(即非 Google)程序员无法访问此处显示的值和方法。我猜他们认为我们可以使用的库越少,我们提交的错误报告就越少。叹息。

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

首先是代码行

    final int offset = ablBehavior.getTopBottomOffsetForScrollingSibling();

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

ViewCompat.offsetTopAndBottom() 不是集合(绝对)运算,而是加法(相对)运算。因此,让我们假设正常逻辑并考虑这种行为本质上将滚动视图直接放在应用栏布局下方。通常,应用栏的底部和滚动视图的顶部具有相同的值。由于应用栏布局(依赖)发生了变化,而滚动视图(子)还没有(还),那么

    dependency.getBottom() - child.getTop()

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

如果我对代码的阅读是正确的,那么应用栏布局中的 mOffsetDelta 仅在应用栏布局具有偏移插值器的情况下才非零。通常,应用栏本身不会以视差方式移动,因此对于我们关心的几乎所有情况,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);
    }

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-25
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多