首页 > 解决方案 > How to Trigger a Scroll Behavior on a Toolbar inside a CoordinatorLayout

问题描述

I have a [PDFView][1] as the direct child of a CoordinatorLayout as in:

enter image description here

I tried to add app:layout_behavior="@string/appbar_scrolling_view_behavior" to the PDFView but it's not compatible so it isn't triggering any scroll behavior on the Toolbar.

The Toolbar has app:layout_scrollFlags="scroll|enterAlways|snap" and I know it works because I already tested it with a NestedScrollView instead of the PDFView.

So I want to achieve this programatically, but I can't find any documentation on how to let the CoordinatorLayout know there's a scroll going on and it should animate the Toolbar going away.

How can I trigger this behavior programatically? The PDFView has a ScrollListner I just don't know how to connect it to the CoordinatorLayout.

标签: androidandroid-animation

解决方案


首先,PDF 视图没有实现 NestedScrollingChild2 ,因此您需要实现它。实现它之后,你必须实现五个 方法。还要创建一个实例NestedScrollingChildHelper并将实现的方法委托给该实例。

视图子类应在构造时将此类的最终实例实例化为字段。对于在此类中具有匹配方法签名的每个 View 方法,将操作委托给重写方法实现中的帮助程序实例。这实现了嵌套滚动的标准框架策略。

让我知道它是否有效。


推荐阅读