Android自定義AppBarLayout,讓它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);}
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(), // curr0, Math.round(velocityY), // velocity.0, 0, // xminOffset, maxOffset); // yif (mScroller.computeScrollOffset()) {mFlingRunnable = new FlingRunnable(coordinatorLayout, layout);ViewCompat.postOnAnimation(layout, mFlingRunnable);return true;} else {onFlingFinished(coordinatorLayout, layout);return false;}}
private class FlingRunnable implements Runnable {private final CoordinatorLayout mParent;private final V mLayout;FlingRunnable(CoordinatorLayout parent, V layout) {mParent = parent;mLayout = layout;}@Overridepublic void run() {if (mLayout != null && mScroller != null) {if (mScroller.computeScrollOffset()) {setHeaderTopBottomOffset(mParent, mLayout, mScroller.getCurrY());// Post ourselves so that we run on the next animationViewCompat.postOnAnimation(mLayout, this);} else {onFlingFinished(mParent, mLayout);}}}}
具體實現(xiàn)

通過前面三塊代碼,我們知道AppBarLayout的Fling效果是通過scroller實現(xiàn)的,滑動的邊界時通過minOffset和maxOffset來控制的,當(dāng)滑動的offset超出范圍時,scroller調(diào)用computeScrollerOffset就為false,頂部view就停止移動了。
mScroller.fling(0, getTopAndBottomOffset(), // curr0, Math.round(velocityY), // velocity.0, 0, // xminOffset-5000, maxOffset); // 設(shè)置一個很大的值,在向上滑動時不會因為低于minOffset而停止滑動
class FlingRunnable implements Runnable {private final CoordinatorLayout mParent;private final V mLayout;private int minOffset;FlingRunnable(CoordinatorLayout parent, V layout, int min) {mParent = parent;mLayout = layout;minOffset = min;}@Overridepublic void run() {if (mLayout != null && mScroller != null) {if (mScroller.computeScrollOffset()) {int currY = mScroller.getCurrY();if (currY < 0 && currY < minOffset) {scrollNext(minOffset - currY);setHeaderTopBottomOffset(mParent, mLayout, minOffset);} else {setHeaderTopBottomOffset(mParent, mLayout, currY);}// Post ourselves so that we run on the next animationViewCompat.postOnAnimation(mLayout, this);} else {onFlingFinished(mParent, mLayout);}}}}
在構(gòu)造FlingRunnable時傳入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(), // curr0, Math.round(velocityY), // velocity.0, 0, // xminOffset-5000, maxOffset); // yif (mScroller.computeScrollOffset()) {mFlingRunnable = new FlingRunnable(coordinatorLayout, layout, minOffset);ViewCompat.postOnAnimation(layout, mFlingRunnable);return true;} else {...}}
class FlingRunnable implements Runnable {private final CoordinatorLayout mParent;private final V mLayout;private int minOffset;private ScrollItem scrollItem;FlingRunnable(CoordinatorLayout parent, V layout, int min) {mParent = parent;mLayout = layout;minOffset = min;initNextScrollView(parent);}private void initNextScrollView(CoordinatorLayout parent) {int count = parent.getChildCount();for (int i = 0; i < count; i++) {View v = parent.getChildAt(i);CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) v.getLayoutParams();if (lp.getBehavior() instanceof AppBarLayout.ScrollingViewBehavior) {scrollItem = new ScrollItem(v);}}@Overridepublic void run() {if (mLayout != null && mScroller != null) {if (mScroller.computeScrollOffset()) {int currY = mScroller.getCurrY();if (currY < 0 && currY < minOffset) {scrollItem.scroll(minOffset - currY); //處理邏輯在ScrollItem中setHeaderTopBottomOffset(mParent, mLayout, minOffset);} else {setHeaderTopBottomOffset(mParent, mLayout, currY);}// Post ourselves so that we run on the next animationViewCompat.postOnAnimation(mLayout, this);} else {onFlingFinished(mParent, mLayout);}}}}
public class ScrollItem {private int type; //1: NestedScrollView 2:RecyclerViewprivate WeakReference<NestedScrollView> scrollViewRef;private WeakReference<LinearLayoutManager> layoutManagerRef;public ScrollItem(View v) {findScrollItem(v);}/*** 查找需要滑動的scroll對象** @param v*/protected boolean findScrollItem(View v) {if (findCommonScroll(v)) return true;if (v instanceof ViewPager) {View root = ViewPagerUtil.findCurrent((ViewPager) v);if (root != null) {View child = root.findViewWithTag("fling");return findCommonScroll(child);}}return false;}private boolean findCommonScroll(View v) {if (v instanceof NestedScrollView) {type = 1;scrollViewRef = new WeakReference<NestedScrollView>((NestedScrollView) v);stopScroll(scrollViewRef.get());return true;}if (v instanceof RecyclerView) {RecyclerView.LayoutManager lm = ((RecyclerView) v).getLayoutManager();if (lm instanceof LinearLayoutManager) {LinearLayoutManager llm = (LinearLayoutManager) lm;type = 2;layoutManagerRef = new WeakReference<LinearLayoutManager>(llm);stopScroll((RecyclerView) v);return true;}}return false;}/*** 停止NestedScrollView滾動** @param v*/private void stopScroll(NestedScrollView v) {try {Field field = ReflectUtil.getDeclaredField(v, "mScroller");if (field == null) return;field.setAccessible(true);OverScroller scroller = (OverScroller) field.get(v);if (scroller != null) scroller.abortAnimation();} catch (Exception e) {e.printStackTrace();}}/*** 停止RecyclerView滾動** @param*/private void stopScroll(RecyclerView rv) {try {Field field = ReflectUtil.getDeclaredField(rv, "mViewFlinger");if (field == null) return;field.setAccessible(true);Object obj = field.get(rv);if (obj == null) return;Method method = obj.getClass().getDeclaredMethod("stop");method.setAccessible(true);method.invoke(obj);} catch (Exception e) {e.printStackTrace();}}public void scroll(int dy) {if (type == 1) {scrollViewRef.get().scrollTo(0, dy);} else if (type == 2) {layoutManagerRef.get().scrollToPositionWithOffset(0, -dy);}}}
public class ViewPagerUtil {public static View findCurrent(ViewPager vp) {int position = vp.getCurrentItem();PagerAdapter adapter = vp.getAdapter();if (adapter instanceof FragmentStatePagerAdapter) {FragmentStatePagerAdapter fsp = (FragmentStatePagerAdapter) adapter;return fsp.getItem(position).getView();} else if (adapter instanceof FragmentPagerAdapter) {FragmentPagerAdapter fp = (FragmentPagerAdapter) adapter;return fp.getItem(position).getView();}return null;}}
好了,基本的滑動邏輯處理完了,我們自己的AppBarLayout可以慣性fling了。會看ScrollItem代碼,我加了stopScroll的邏輯。那是因為在底部recyclerView或NestedScrollView快速向下滑動至AppBarLayout展開,而這時在AppBarLayout想要快速向上滑動,應(yīng)為底部正在滑動,導(dǎo)致兩者沖突,不能正常向上滑動,所以AppBarLayout在向上快速滑動時,要停止底部滑動。通過NestedScrollView和RecyclerView的源碼,我們找到控制滑動邏輯的OverScroller和ViewFlinger,我們可以通過反射來停止對應(yīng)的滑動。
源碼地址:
評論
圖片
表情
