您的位置:首页 > 移动开发

AppBarLayout.OnOffsetChangedListener的使用

2016-03-23 21:15 369 查看
先看看官网对于AppBarLayout.OnOffsetChangedListener的解释:

Interface definition for a callback to be invoked when an AppBarLayout's
vertical offset changes.

AppBarLayout垂直方向上的偏移量发生改变时,为触发一个回调方法定义的接口。

触发的回调方法是:

onOffsetChanged(AppBarLayout appBarLayout,
int verticalOffset)

再来看看对于onOffsetChanged的解释:

Called when the AppBarLayout's
layout offset has been changed. This allows child views to implement custom behavior based on the offset (for instance pinning a view at a certain y value).

AppBarLayout的布局偏移量发生改变时被调用。这个方法允许子view根据偏移量实现自定义的行为(比如在特定Y值的时候固定住一个View)。

解释的非常清楚了,不过我们需要点例子才好理解。

就以stackoverflow上的一个问题为例吧。

http://stackoverflow.com/questions/30779667/android-collapsingtoolbarlayout-and-swiperefreshlayout/30785823#30785823


问题描述

当我把CollapsingToolbarLayout,CollapsingToolbarLayout以及SwipeRefreshLayout用在一起的时候:

java代码:

如何才能让滑动刷新只发生在collapsing toolbar完全展开并且 scrollview (或者recyclerview)在顶部 的时候呢?类似于g+和Inbox中的效果。

错误的效果:



正确的效果:




回答:

可以尝试为AppBarLayout添加 offset change listener同时根据情况启用或者关闭滑动刷新,需要增加的代码在这里:

https://gist.github.com/blackcj/001a90c7775765ad5212

关键改动:

点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: