NestedScrolling嵌套滚动

#一、什么是NestedScrolling?

Android在Lollipop版本中引入了NestedScrolling——嵌套滚动机制。在Android的事件处理机制中,事件序列只能由父View和子View中的一个处理。在嵌套滚动机制中,子View处理事件前会将事件传给父View处理,两者协作配合处理事件。

在嵌套滚动机制中,父View需实现NestedScrollingParent接口,子View需要实现NestedScrollingChild接口。从Lollipop起View都已经实现了NestedScrollingChild的方法。嵌套滚动过程如下:

  1. 开始滚动前,子View调用startNestedScroll方法。该方法会调用父View的onStartNestedScroll方法并返回onStartNestedScroll的值。如果返回true,则表示父View愿意接收后续的滚动事件,此时父View的onNestedScrollAccepted会被调用。该方法一般是在子View处理DOWN事件时调用。
  2. 子View滚动某个距离前,调用dispatchNestedPreScroll方法,把滚动距离传给父View。该方法回调父View的onNestedPreScroll方法,如果父View需要消耗滚动距离,只需要把需要消耗的距离赋给onNestedPreScroll方法的参数consumed。该参数是一个数组,consumed[0]表示消耗的水平滚动距离,consumed[1]表示消耗的垂直滚动距离。dispatchNestedPreScroll返回true则表示父View消耗了部分或者全部滚动距离。
  3. 子View滚动某个距离后,调用dispatchNestedScroll方法。如果该方法返回true则表示,子View会调用父View的onNestedScroll方法,把已消耗和未消耗的滚动距离传给父View。
  4. 子View处理Fling事件前,调用dispatchNestedPreFling方法。该方法会调用父View的onNestedPreFling并返回onNestedPreFling的值。如果true,则表示父View处理消耗了该Fling事件,则子View不应该处理该Fling事件。
  5. 如果dispatchNestedPreFling方法返回false,子View在处理Fling事件后会调用dispatchNestedFling方法,该方法会调用父View的onNestedFling方法。onNestedFling方法返回true表示父View消耗或处理了Fling事件。
  6. 当子View停止滚动时,调用stopNestedScroll方法。该方法会调用父View的onStopNestedScroll方法。

上面提及的各个方法的具体用法请参考官方文档。

二、怎么实现NestedScrollingChild?

Android为NestedScrollingChild提供了一个代理类NestedScrollingChildHelper。所以,NestedScrollingChild的最简单的实现如下。

public class NestedScrollingChildView extends FrameLayout implements NestedScrollingChild {
	
	private final NestedScrollingChildHelper mChildHelper;

	public NestedScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mChildHelper = new NestedScrollingChildHelper(this);
    }

    @Override
    public void setNestedScrollingEnabled(boolean enabled) {
        mChildHelper.setNestedScrollingEnabled(enabled);
    }

    @Override
    public boolean isNestedScrollingEnabled() {
        return mChildHelper.isNestedScrollingEnabled();
    }

    @Override
    public boolean startNestedScroll(int axes) {
        return mChildHelper.startNestedScroll(axes);
    }

    @Override
    public void stopNestedScroll() {
        mChildHelper.stopNestedScroll();
    }

    @Override
    public boolean hasNestedScrollingParent() {
        return mChildHelper.hasNestedScrollingParent();
    }

    @Override
    public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed,
            int dyUnconsumed, int[] offsetInWindow) {
        return mChildHelper.dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed,
                offsetInWindow);
    }

    @Override
    public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) {
        return mChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow);
    }

    @Override
    public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) {
        return mChildHelper.dispatchNestedFling(velocityX, velocityY, consumed);
    }

    @Override
    public boolean dispatchNestedPreFling(float velocityX, float velocityY) {
        return mChildHelper.dispatchNestedPreFling(velocityX, velocityY);
    }
}
复制代码

然后,在适当的时机调用如下方法: 1.startNestedScroll 2.dispatchNestedPreScroll 3.dispatchNestedScroll 4.dispatchNestedPreFling 5.dispatchNestedFling 6.stopNestedScroll

三、怎么实现NestedScrollingParent?

Android为NestedScrollingParent提供了一个代理类NestedScrollingParentHelper。NestedScrollingParent的最简单实现如下。

public class NestedScrollView extends FrameLayout implements NestedScrollingParent {
	
	private final NestedScrollingParentHelper mParentHelper;

	public NestedScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mParentHelper = new NestedScrollingParentHelper(this);
    }

    @Override
    public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
    	...
        return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
    }

    @Override
    public void onNestedScrollAccepted(View child, View target, int axes) {
        mParentHelper.onNestedScrollAccepted(child, target, axes);
        ...
    }

    @Override
    public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
    	...
    }

    @Override
    public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
    	...
    }

    @Override
    public boolean onNestedPreFling(View target, float velocityX, float velocityY) {
        ...
        return false;
    }

    @Override
    public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) {
    	...
        return false;
    }

    @Override
    public void onStopNestedScroll(View child) {
        mParentHelper.onStopNestedScroll(child);
    }

    @Override
    public int getNestedScrollAxes() {
        return mParentHelper.getNestedScrollAxes();
    }
}
复制代码
四、NestedScrollingChildHelper的代码分析
public boolean startNestedScroll(int axes) {
        if (hasNestedScrollingParent()) {
            // Already in progress
            return true;
        }
        if (isNestedScrollingEnabled()) {
            ViewParent p = mView.getParent();
            View child = mView;
            while (p != null) {
                if (ViewParentCompat.onStartNestedScroll(p, child, mView, axes)) {
                    mNestedScrollingParent = p;
                    ViewParentCompat.onNestedScrollAccepted(p, child, mView, axes);
                    return true;
                }
                if (p instanceof View) {
                    child = (View) p;
                }
                p = p.getParent();
            }
        }
        return false;
    }
复制代码

startNestedScroll方法从NestedScrollingChild向上查找愿意接收嵌套滚动事件的父View,如果找到了则调用父View的onNestedScrollAccepted方法。ViewParentCompat是父View的兼容类,该类会判断版本,如果在Lollipop及以上则调用View自带的方法。否则,调用NestedScrollingParent的接口方法。

public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) {
        if (isNestedScrollingEnabled() && mNestedScrollingParent != null) {
            if (dx != 0 || dy != 0) {
                int startX = 0;
                int startY = 0;
                if (offsetInWindow != null) {
                    mView.getLocationInWindow(offsetInWindow);
                    startX = offsetInWindow[0];
                    startY = offsetInWindow[1];
                }

                if (consumed == null) {
                    if (mTempNestedScrollConsumed == null) {
                        mTempNestedScrollConsumed = new int[2];
                    }
                    consumed = mTempNestedScrollConsumed;
                }
                consumed[0] = 0;
                consumed[1] = 0;
                ViewParentCompat.onNestedPreScroll(mNestedScrollingParent, mView, dx, dy, consumed);

                if (offsetInWindow != null) {
                    mView.getLocationInWindow(offsetInWindow);
                    offsetInWindow[0] -= startX;
                    offsetInWindow[1] -= startY;
                }
                return consumed[0] != 0 || consumed[1] != 0;
            } else if (offsetInWindow != null) {
                offsetInWindow[0] = 0;
                offsetInWindow[1] = 0;
            }
        }
        return false;
    }
复制代码

调用父View的onNestedPreScroll方法并记录滚动偏移量。参数offsetInWindow是一个长度为2的一位数组,记录滚动的偏移量,用来修改Touch事件的坐标,保证下次滚动的准确性。dispatchNestedScroll方法也同理。 ####五、举个例子 实现一个简单的NestedScrollingParent。该View包含一个头部View和RecyclerView。RecyclerView已经实现了NestedScrollingChild接口方法。向上滚动时,如果头部没有完全收起,则向上滚动头部。如果头部收起才滚动RecyclerView。向下滚动时,如果头部收起,则向下滚动头部,否则滚动RecyclerView。

public class HeaderLayout extends LinearLayout implements NestedScrollingParent {

    private NestedScrollingParentHelper mParentHelper;

    private int headerH;

    private ScrollerCompat mScroller;

    private boolean resetH = false;

    public HeaderLayout(Context context) {
        this(context, null);
    }

    public HeaderLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HeaderLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mParentHelper = new NestedScrollingParentHelper(this);
        mScroller = ScrollerCompat.create(this.getContext());
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        headerH = getChildAt(0).getMeasuredHeight();
    }

    @Override
    public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
        return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
    }

    @Override
    public void onNestedScrollAccepted(View child, View target, int axes) {
        mParentHelper.onNestedScrollAccepted(child, target, axes);
    }

    @Override
    public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
        int scrollY = getScrollY();
        if (dy > 0 && scrollY < headerH && scrollY >= 0) {
            int consumedY = Math.min(dy, headerH - scrollY);
            consumed[1] = consumedY;
            scrollBy(0, consumedY);

            if (!resetH) {
                resetH = true;
                int w = getWidth();
                int h = getHeight() + headerH;
                setLayoutParams(new FrameLayout.LayoutParams(w, h));
            }
        } else if (dy < 0 && scrollY == headerH) {
            consumed[1] = dy;
            scrollBy(0, dy);
        } else if (dy < 0 && scrollY < headerH && scrollY > 0) {
            int consumedY = Math.max(dy, -scrollY);
            consumed[1] = consumedY;
            scrollBy(0, consumedY);
        }
    }

    @Override
    public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
    }

    @Override
    public boolean onNestedPreFling(View target, float velocityX, float velocityY) {
        int scrollY = getScrollY();
        if (velocityY > 0 && scrollY < headerH && scrollY > 0) {
            if (!mScroller.isFinished()) {
                mScroller.abortAnimation();
            }
            mScroller.fling(0, scrollY, (int)velocityX, (int)velocityY, 0, 0, 0, headerH);
            ViewCompat.postInvalidateOnAnimation(this);
            return true;
        }
        return false;
    }

    @Override
    public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) {
        return false;
    }

    @Override
    public void onStopNestedScroll(View child) {
        mParentHelper.onStopNestedScroll(child);
    }

    @Override
    public int getNestedScrollAxes() {
        return mParentHelper.getNestedScrollAxes();
    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            postInvalidate();
        }
    }
}

复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值