gpt4 book ai didi

android - AppBarLayout.ScrollingViewBehavior 是如何运作的?

转载 作者:可可西里 更新时间:2023-11-01 19:07:38 25 4
gpt4 key购买 nike

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

因为它是私有(private)方法,私有(private)方法是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

的内容

最佳答案

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

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

首先是那行代码

    final int offset = ablBehavior.getTopBottomOffsetForScrollingSibling();

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

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

    dependency.getBottom() - child.getTop()

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

如果我对代码的解读是正确的,那么只有在应用栏布局具有偏移插值器的情况下,应用栏布局行为中的 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);
}

我发现使用绝对偏移比使用相对偏移更容易一些。因此,实现滚动行为主要是确定依赖 View 的位置以及 ScrollView 需要基于该 View 的位置。

关于android - AppBarLayout.ScrollingViewBehavior 是如何运作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40021797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com