简体   繁体   English

如何在CoordinatorLayout内部的工具栏上触发滚动行为

[英]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: 我有一个[PDFView][1]作为CoordinatorLayout的直接子代,如下所示:

在此处输入图片说明

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 . 我试图将app:layout_behavior="@string/appbar_scrolling_view_behavior"PDFView但是它不兼容,因此不会触发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 . Toolbarapp:layout_scrollFlags="scroll|enterAlways|snap" ,我知道它的工作原理是因为我已经使用NestedScrollView而不是NestedScrollView进行了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. 所以我想以编程方式实现这一目标,但是我找不到任何有关如何让CoordinatorLayout知道正在进行滚动的文档,它应该使Toolbar动画消失。

How can I trigger this behavior programatically? 如何以编程方式触发此行为? The PDFView has a ScrollListner I just don't know how to connect it to the CoordinatorLayout . PDFView有一个ScrollListner我只是不知道如何将其连接到CoordinatorLayout

First the PDF view does not implement NestedScrollingChild2 , so you need to implement it. 首先,PDF视图未实现NestedScrollingChild2,因此您需要实现它。 After implementing it , you have to implement five methods. 实现它之后,您必须实现五个方法。 Also create an instance NestedScrollingChildHelper and delegate the implemented methods to this instance. 还创建一个实例NestedScrollingChildHelper ,并将实现的方法委托给该实例。

View subclasses should instantiate a final instance of this class as a field at construction. View子类应在构造时将此类的最终实例实例化为字段。 For each View method that has a matching method signature in this class, delegate the operation to the helper instance in an overridden method implementation. 对于此类中具有匹配方法签名的每个View方法,在重写的方法实现中将操作委托给帮助程序实例。 This implements the standard framework policy for nested scrolling. 这实现了嵌套滚动的标准框架策略。

Let me know if it works. 让我知道它是否有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM