appbarlayout折叠_Android自定义AppBarLayout,让它Fling起来更流畅

我们知道,Desgin包中的AppBarLayout配合CollapsingToolbarLayout可以实现折叠效果。但是顶部在快速滑动到折叠状态时,底部的NestedScrollChild不会因为惯性跟着滑动,整个滑动过程瞬间停止,给人一种很不流畅的感觉。为了能让我们的AppBarLayout能Fling更流畅,我们需要在重新修改源码,定制一个FlingAppBarLayout,能够实现类似饿了么首页效果

d979cf2b7e63f829afc92e48a025662d.gif

思路

我们知道AppBarLayout之所以能够有折叠效果,是因为有一个默认的Behavior,而且AppBarLayout在快速滑动时,布局也能够快速展开和收缩,因此可以猜测内部有可能处理了Fling事件。通过源码,找到对应的Behavior,它继承自HeaderBehavior,通过onTouchEvent方法,找到了对应对于Fling事件的处理
    case MotionEvent.ACTION_UP:                if (mVelocityTracker != null) {
                        mVelocityTracker.addMovement(ev);                    mVelocityTracker.computeCurrentVelocity(1000);                    float yvel = mVelocityTracker.getYVelocity(mActivePointerId);                    fling(parent, child, -getScrollRangeForDragFling(child), 0, yvel);                }
进入fling方法,找到了scroller对象,AppBarLayout的快速滑动效果就是通过它来实现的。至于为什么AppBarLayout向上快速滑动到边界时,突然停止,没有惯性滑动,是因为scroller在调用fling方法时设置了minOffset(向上滑动边界)
    final boolean fling(CoordinatorLayout coordinatorLayout, V layout, int minOffset,            int maxOffset, float velocityY) {
            if (mFlingRunnable != null) {
                layout.removeCallbacks(mFlingRunnable);            mFlingRunnable = null;        }        if (mScroller == null) {
                mScroller = new OverScroller(layout.getContext());        }        mScroller.fling(                0, getTopAndBottomOffset(), // curr                0, Math.round(velocityY), // velocity.                0, 0, // x                minOffset, maxOffset); // y        if (mScroller.computeScrollOffset()) {
                mFlingRunnable = new FlingRunnable(coordinatorLayout, layout);            ViewCompat.postOnAnimation(layout, mFlingRunnable);            return true;        } else {
                onFlingFinished(coordinatorLayout, layout);            return false;        }    }
而具体的view的移动,则是通过FlingRunnable来实现。
 private class FlingRunnable implements Runnable {
            private final CoordinatorLayout mParent;        private final V mLayout;        FlingRunnable(CoordinatorLayout parent, V layout) {
                mParent = parent;            mLayout = layout;        }        @Override        public void run() {
                if (m
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值