如何真实有效的用代码滚动AppBarLayout

转载请注明出处:
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;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值