转载请注明出处:
http://blog.csdn.net/aa464971/article/details/85246559
Github地址:https://github.com/xiandanin/AndroidViewHelper
效果图
Gradle 引入
implementation 'com.dyhdyh:view-helper:1.0.3'
调用DesignViewHelper
//滚动AppBarLayout
DesignViewHelper.setAppBarLayoutOffset(appBar, offsetY);
//以动画的形式滚动AppBarLayout
DesignViewHelper.setAppBarLayoutOffsetWithAnimate(appBar, offsetY, duration);
//滚动AppBarLayout到顶部
DesignViewHelper.scrollAppBarTop(appBar, isAnimate);
篇外话
得吐槽一下国内的技术文章环境是真的烂,这个问题查来查去不是你抄他就是他抄你,没有一篇能够有效的让AppBarLayout滚动,那没办法只能自己解决了。
翻了老半天的源码,最终在HeaderBehavior
中找到了setHeaderTopBottomOffset
,过程很艰辛,结果很简单,但是这个方法不是public
的调不到,所以要用反射去调,已经封装在DesignViewHelper
,可以直接使用。
int setHeaderTopBottomOffset(CoordinatorLayout parent, V header, int newOffset) {
return this.setHeaderTopBottomOffset(parent, header, newOffset, -2147483648, 2147483647);
}
int setHeaderTopBottomOffset(CoordinatorLayout parent, V header, int newOffset, int minOffset, int maxOffset) {
int curOffset = this.getTopAndBottomOffset();
int consumed = 0;
if (minOffset != 0 && curOffset >= minOffset && curOffset <= maxOffset) {
newOffset = MathUtils.clamp(newOffset, minOffset, maxOffset);
if (curOffset != newOffset) {
this.setTopAndBottomOffset(newOffset);
consumed = curOffset - newOffset;
}
}
return consumed;
}