最近产品提了个需求,要把商品列表作成相似淘宝的样式android
通常遇到这种需求,咱们首先会想到的是,拦截TouchEvent,而后本身来处理滑动,这种方法虽然行得通,可是代码写起来很是恶心,且滑动冲突会比较多,使用NestedScrolling API会简单优雅不少。git
先上效果图github
Parent接口共有如下几个方法面试
public interface NestedScrollingParent { //当子View开始滑动时,会触发这个方法,判断接下来是否进行嵌套滑动, //返回false,则表示不使用嵌套滑动 boolean onStartNestedScroll(@NonNull View child, @NonNull View target, @ScrollAxis int axes); //onStartNestedScroll若是返回true,那么接下来就会调用这个方法,用来作一些初始化操做,通常能够忽略 void onNestedScrollAccepted(@NonNull View child, @NonNull View target, @ScrollAxis int axes); //嵌套滑动结束时会触发这个方法 void onStopNestedScroll(@NonNull View target); //子View滑动时会触发这个方法,dyConsumed表明子View滑动的距离,dyUnconsumed表明子View本次滑动未消耗的距离,好比RecyclerView滑到了边界,那么会有一部分y未消耗掉 void onNestedScroll(@NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed); //子View开始滑动时,会触发这个回调,dy表示滑动的y距离,consumed数组表明父View要消耗的距离,假如consumed[1] = dy,那么子View就不会滑动了 void onNestedPreScroll(@NonNull View target, int dx, int dy, @NonNull int[] consumed); //当子View fling时,会触发这个回调,consumed表明速度是否被子View消耗掉,好比RecyclerView滑动到了边界,那么它显然无法消耗本次的fling boolean onNestedFling(@NonNull View target, float velocityX, float velocityY, boolean consumed); //当子View要开始fling时,会先询问父View是否要拦截本次fling,返回true表示要拦截,那么子View就不会惯性滑动了 boolean onNestedPreFling(@NonNull View target, float velocityX, float velocityY); //表示目前正在进行的嵌套滑动的方向,值有ViewCompat.SCROLL_AXIS_HORIZONTAL 或者ViewCompat.SCROLL_AXIS_VERTICAL或者SCROLL_AXIS_NONE @ScrollAxis int getNestedScrollAxes(); }
public interface NestedScrollingChild { //设置当前子View是否支持嵌套滑动 void setNestedScrollingEnabled(boolean enabled); //当前子View是否支持嵌套滑动 boolean isNestedScrollingEnabled(); //开始嵌套滑动,对应Parent的onStartNestedScroll boolean startNestedScroll(@ScrollAxis int axes); //中止本次嵌套滑动,对应Parent的onStopNestedScroll void stopNestedScroll(); //true表示这个子View有一个支持嵌套滑动的父View boolean hasNestedScrollingParent(); //通知父View子View开始滑动了,对应父View的onNestedScroll方法 boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, @Nullable int[] offsetInWindow); //通知父View即将开始滑动了,对应父View的onNestedPreScroll方法 boolean dispatchNestedPreScroll(int dx, int dy, @Nullable int[] consumed, @Nullable int[] offsetInWindow); //通知父View开始Fling了,对应Parent的onNestedFling方法 boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed); //通知父View要开始fling了,对应Parent的onNestedPreFling方法 boolean dispatchNestedPreFling(float velocityX, float velocityY); }
总体流程描述以下(以RecyclerView为例):数组
child.ACTION_DOWN
-> child.startNestedScroll
-> parent.onStartNestedScroll (若是返回false,则流程终止)
-> parent.onNestedScrollAccepted
-> child.ACTION_MOVE
-> child.dispatchNestedPreScroll
-> parent.onNestedPreScroll
-> child.ACTION_UP
-> chid.stopNestedScroll
-> parent.onStopNestedScroll
-> child.fling
-> child.dispatchNestedPreFling
-> parent.onNestedPreScroll
-> child.dispatchNestedFling
-> parent.onNestedFling性能优化
有兴趣的朋友能够直接查看 RecyclerView 的源码架构
子View向上传递事件时,是循环向上的,即 Parent 不须要是 Child 的直接 ViewParent,具体能够看代码,以startNestedScroll为例ide
public boolean startNestedScroll(int axes) { if (hasNestedScrollingParent()) { // Already in progress return true; } if (isNestedScrollingEnabled()) { ViewParent p = getParent(); View child = this; while (p != null) { try { if (p.onStartNestedScroll(child, this, axes)) { mNestedScrollingParent = p; p.onNestedScrollAccepted(child, this, axes); return true; } } catch (AbstractMethodError e) { Log.e(VIEW_LOG_TAG, "ViewParent " + p + " does not implement interface " + "method onStartNestedScroll", e); // Allow the search upward to continue } if (p instanceof View) { child = (View) p; } p = p.getParent(); } } return false; }
RV 嵌套 RV 时,内层 RV 是没法滑动的,然而,当外层RV在Fling时,若是咱们触摸到子RV,那么会有必定几率致使子RV接收到Touch事件并开始滚动,因此咱们须要同时拦截内层和外层的RV的事件。大概思路以下:性能
具体处理为,咱们在外层RV之上嵌套一层自定义的FrameLayout,并开启外层RV和内层RV的嵌套滑动功能,那么咱们就能在FrameLayout中接收到RV传递上来的scroll和fling事件优化
public class NestedScrollLayout extends FrameLayout { private View mChildView; /** * 最外层的RecyclerView */ private RecyclerView mRootList; /** * 子RecyclerView */ private RecyclerView mChildList; @Override public boolean onStartNestedScroll(@NonNull View child, @NonNull View target, int nestedScrollAxes) { //这里表示只有在纵向滑动时,咱们才拦截事件 return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL; } @Override public void onNestedPreScroll(@NonNull View target, int dx, int dy, @NonNull int[] consumed) { stopScroller(); //mChildView表示TabLayout和ViewPager的父View,好比说咱们用一个LinearLayout包裹住TabLayout和ViewPager if (mChildView == null) { return; } if (target == mRootList) { onParentScrolling(mChildView.getTop(), dy, consumed); } else { onChildScrolling(mChildView.getTop(), dy, consumed); } } /** * 父列表在滑动 * * @param childTop * @param dy * @param consumed */ private void onParentScrolling(int childTop, int dy, int[] consumed) { //列表已经置顶 if (childTop == 0) { if (dy > 0 && mChildList != null) { //还在向下滑动,此时滑动子列表 mChildList.scrollBy(0, dy); consumed[1] = dy; } else { if (mChildList != null && mChildList.canScrollVertically(dy)) { consumed[1] = dy; mChildList.scrollBy(0, dy); } } } else { if (childTop < dy) { consumed[1] = dy - childTop; } } } private void onChildScrolling(int childTop, int dy, int[] consumed) { if (childTop == 0) { if (dy < 0) { //向上滑动 if (!mChildList.canScrollVertically(dy)) { consumed[1] = dy; mRootList.scrollBy(0, dy); } } } else { if (dy < 0 || childTop > dy) { consumed[1] = dy; mRootList.scrollBy(0, dy); } else { //dy大于0 consumed[1] = dy; mRootList.scrollBy(0, childTop); } } } /** * 表示咱们只接收纵向的事件 * @return */ @Override public int getNestedScrollAxes() { return ViewCompat.SCROLL_AXIS_VERTICAL; } }
ViewGroup默认实现了Parent接口,这里咱们不须要再implement一次
当列表开始 Fling 时,咱们将会接收到相应的回调,这里咱们须要本身处理惯性滑动,使用 OverScroller 来替咱们模拟Fling
public class NestedScrollLayout extends FrameLayout { /** * 用来处理Fling */ private OverScroller mScroller; private int mLastY; @Override public boolean onNestedFling(@NonNull View target, float velocityX, float velocityY, boolean consumed) { return false; } @Override public boolean onNestedPreFling(@NonNull View target, float velocityX, float velocityY) { mLastY = 0; this.mScroller.fling(0, 0, (int) velocityX, (int) velocityY, Integer.MIN_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MAX_VALUE); invalidate(); return true; } @Override public void computeScroll() { if (mScroller.computeScrollOffset()) { int currY = mScroller.getCurrY(); int dy = currY - mLastY; mLastY = currY; if (dy != 0) { onFling(dy); } invalidate(); } super.computeScroll(); } private void onFling(int dy) { if (mChildView != null) { //子列表有显示 int top = mChildView.getTop(); if (top == 0) { if (dy > 0) { if (mChildList != null && mChildList.canScrollVertically(dy)) { mChildList.scrollBy(0, dy); } else { stopScroller(); } } else { if (mChildList != null && mChildList.canScrollVertically(dy)) { mChildList.scrollBy(0, dy); } else { mRootList.scrollBy(0, dy); } } } else { if (dy > 0) { if (top > dy) { mRootList.scrollBy(0, dy); } else { mRootList.scrollBy(0, top); } } else { if (mRootList.canScrollVertically(dy)) { mRootList.scrollBy(0, dy); } else { stopScroller(); } } } } else { if (!mRootList.canScrollVertically(dy)) { stopScroller(); } else { mRootList.scrollBy(0, dy); } } } }
到这里为止,咱们要的效果已经实现了,mChildView 和子RV什么时候赋值,参考Demo便可。
你觉得这样就完了?
谷歌在 26.1.0 的 support 包中加入了两个新的 API
这两个接口各自继承了NestedScrollingParent和NestedScrollingChild
public interface NestedScrollingParent2 extends NestedScrollingParent { boolean onStartNestedScroll(@NonNull View child, @NonNull View target, @ScrollAxis int axes, @NestedScrollType int type); void onNestedScrollAccepted(@NonNull View child, @NonNull View target, @ScrollAxis int axes, @NestedScrollType int type); void onStopNestedScroll(@NonNull View target, @NestedScrollType int type); void onNestedScroll(@NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, @NestedScrollType int type); void onNestedPreScroll(@NonNull View target, int dx, int dy, @NonNull int[] consumed, @NestedScrollType int type); }
public interface NestedScrollingChild2 extends NestedScrollingChild { boolean startNestedScroll(@ScrollAxis int axes, @NestedScrollType int type); void stopNestedScroll(@NestedScrollType int type); boolean hasNestedScrollingParent(@NestedScrollType int type); boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, @Nullable int[] offsetInWindow, @NestedScrollType int type); boolean dispatchNestedPreScroll(int dx, int dy, @Nullable int[] consumed, @Nullable int[] offsetInWindow, @NestedScrollType int type); }
在新的API中去掉了 fling 回调,而且增长了 type 参数,type分为两种
//表示当前事件是由用户手指触摸产生的 public static final int TYPE_TOUCH = 0; //表示当前事件不是用户手指触摸产生的,通常是fling public static final int TYPE_NON_TOUCH = 1;
Parent2具体流程以下:
child.ACTION_DOWN
-> child.startNestedScroll (TYPE_TOUCH)
-> parent.onStartNestedScroll (TYPE_TOUCH) (若是返回false,则流程终止)
-> parent.onNestedScrollAccepted (TYPE_TOUCH)
-> child.ACTION_MOVE
-> child.dispatchNestedPreScroll (TYPE_TOUCH)
-> parent.onNestedPreScroll (TYPE_TOUCH)
-> child.ACTION_UP
-> chid.stopNestedScroll (TYPE_TOUCH)
-> parent.onStopNestedScroll (TYPE_TOUCH)
-> child.fling
-> child.startNestedScroll (TYPE_NON_TOUCH)
-> parent.onStartNestedScroll (TYPE_NON_TOUCH) (若是返回false,则流程终止)
-> parent.onNestedScrollAccepted (TYPE_NON_TOUCH)
-> child.dispatchNestedPreScroll (TYPE_NON_TOUCH)
-> parent.onNestedPreScroll (TYPE_NON_TOUCH)
-> child.dispatchNestedScroll (TYPE_NON_TOUCH)
-> parent.onNestedScroll (TYPE_NON_TOUCH)
-> child.stopNestedScroll (TYPE_NON_TOUCH)
-> parent.onStopNestedScroll (TYPE_NON_TOUCH)
如上所示,当 RV 开始 Fling 时,每一帧 Fling 的距离,都会通知到 Parent2,由 Parent2 判断是否拦截处理,那么咱们就不须要本身使用 OverScroller 来模拟惯性滑动了,代码能够更少。具体实现以下:
public class NestedScrollLayout2 extends FrameLayout implements NestedScrollingParent2 { private View mChildView; /** * 最外层的RecyclerView */ private RecyclerView mRootList; /** * 子RecyclerView */ private RecyclerView mChildList; private NestedViewModel mScrollViewModel; private int mAxes; public NestedScrollLayout2(@NonNull Context context) { super(context); } public NestedScrollLayout2(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); } public void setTarget(LifecycleOwner target) { if (target instanceof FragmentActivity) { mScrollViewModel = ViewModelProviders.of((FragmentActivity) target).get(NestedViewModel.class); } else if (target instanceof Fragment) { mScrollViewModel = ViewModelProviders.of((Fragment) target).get(NestedViewModel.class); } else { throw new IllegalArgumentException("target must be FragmentActivity or Fragment"); } mScrollViewModel.getChildView().observe(target, new Observer<View>() { @Override public void onChanged(@Nullable View view) { mChildView = view; } }); mScrollViewModel.getChildList().observe(target, new Observer<View>() { @Override public void onChanged(@Nullable View view) { mChildList = (RecyclerView) view; } }); } public void setRootList(RecyclerView recyclerView) { mRootList = recyclerView; } @Override public boolean onStartNestedScroll(@NonNull View child, @NonNull View target, int axes, int type) { return axes == ViewCompat.SCROLL_AXIS_VERTICAL; } @Override public void onNestedScrollAccepted(@NonNull View child, @NonNull View target, int axes, int type) { mAxes = axes; } @Override public void onStopNestedScroll(@NonNull View target, int type) { mAxes = SCROLL_AXIS_NONE; } @Override public void onNestedScroll(@NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) { } @Override public void onNestedPreScroll(@NonNull View target, int dx, int dy, @NonNull int[] consumed, int type) { if (mChildView == null) { return; } if (target == mRootList) { onParentScrolling(mChildView.getTop(), dy, consumed); } else { onChildScrolling(mChildView.getTop(), dy, consumed); } } /** * 父列表在滑动 * * @param childTop * @param dy * @param consumed */ private void onParentScrolling(int childTop, int dy, int[] consumed) { //列表已经置顶 if (childTop == 0) { if (dy > 0 && mChildList != null) { //还在向下滑动,此时滑动子列表 mChildList.scrollBy(0, dy); consumed[1] = dy; } else { if (mChildList != null && mChildList.canScrollVertically(dy)) { consumed[1] = dy; mChildList.scrollBy(0, dy); } } } else { if (childTop < dy) { consumed[1] = dy - childTop; } } } private void onChildScrolling(int childTop, int dy, int[] consumed) { if (childTop == 0) { if (dy < 0) { //向上滑动 if (!mChildList.canScrollVertically(dy)) { consumed[1] = dy; mRootList.scrollBy(0, dy); } } } else { if (dy < 0 || childTop > dy) { consumed[1] = dy; mRootList.scrollBy(0, dy); } else { //dy大于0 consumed[1] = dy; mRootList.scrollBy(0, childTop); } } } @Override public int getNestedScrollAxes() { return mAxes; } }
有人可能会问,既然有新 API,为啥还要用 OverScroller。
由于,咱们项目工程里的 RV 版本较低,没有实现 NestedScrollingChild2,而新版本的 RV 已经实现了Child2,因此,你们有空必定要多升级 Support,真的好用。
最后献上Demo地址,欢迎你们参考。
Demo地址:
https://github.com/xue5455/Ne...