<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Android自定義AppBarLayout,讓它Fling起來更流暢

          共 11419字,需瀏覽 23分鐘

           ·

          2021-07-17 07:02

          我們知道,Desgin包中的AppBarLayout配合CollapsingToolbarLayout可以實現(xiàn)折疊效果。但是頂部在快速滑動到折疊狀態(tài)時,底部的NestedScrollChild不會因為慣性跟著滑動,整個滑動過程瞬間停止,給人一種很不流暢的感覺。為了能讓我們的AppBarLayout能Fling更流暢,我們需要在重新修改源碼,定制一個FlingAppBarLayout,能夠?qū)崿F(xiàn)類似餓了么首頁效果


          思路


          我們知道AppBarLayout之所以能夠有折疊效果,是因為有一個默認的Behavior,而且AppBarLayout在快速滑動時,布局也能夠快速展開和收縮,因此可以猜測內(nèi)部有可能處理了Fling事件。通過源碼,找到對應(yīng)的Behavior,它繼承自HeaderBehavior,通過onTouchEvent方法,找到了對應(yīng)對于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的快速滑動效果就是通過它來實現(xiàn)的。至于為什么AppBarLayout向上快速滑動到邊界時,突然停止,沒有慣性滑動,是因為scroller在調(diào)用fling方法時設(shè)置了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來實現(xiàn)。
           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 (mLayout != null && mScroller != null) { if (mScroller.computeScrollOffset()) { setHeaderTopBottomOffset(mParent, mLayout, mScroller.getCurrY()); // Post ourselves so that we run on the next animation ViewCompat.postOnAnimation(mLayout, this); } else { onFlingFinished(mParent, mLayout); } } } }

          通過這個FlingRunnable類,我們知道AppBarLayout能快速展開和收縮,就是通過它實現(xiàn)的。

          具體實現(xiàn)


          首先,我們把design中的AppBarLayout源碼復(fù)制到自己的package中,引入報紅的相關(guān)文件,具體如下:


          其中ScrollItem,ReflectUtil,ViewPagerUtil為我們自己定義的,其他都是design包拷貝的。

          通過前面三塊代碼,我們知道AppBarLayout的Fling效果是通過scroller實現(xiàn)的,滑動的邊界時通過minOffset和maxOffset來控制的,當(dāng)滑動的offset超出范圍時,scroller調(diào)用computeScrollerOffset就為false,頂部view就停止移動了。

          因此為了能讓AppBarLayout在向上滑動到minOffset邊界時不停止移動,把這個minOffset保存到FlingRunnable中,在scroller.fling方法中這個更小的offset,這個在滑動到minOffset時,computeScrollerOffset就不會為false,并且在FlingRunnable中因為有minOffset,我們可以在mScroller.computeScrollOffset里判斷是否滑出邊界,通過差值,繼續(xù)滑動底部的可滑動布局。
            mScroller.fling(                0, getTopAndBottomOffset(), // curr                0, Math.round(velocityY), // velocity.                0, 0, // x                minOffset-5000, maxOffset); // 設(shè)置一個很大的值,在向上滑動時不會因為低于minOffset而停止滑動


          在FlingRunnable中新增minOffset字段,run方法中,如果currY<minOffset表示AppBarLayout向上滑動值收縮狀態(tài),可以滑動底部布局了,scrollNext(),傳入偏移量minOffset-currY
           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; } @Override public 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 animation ViewCompat.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(), // curr                0, Math.round(velocityY), // velocity.                0, 0, // x                minOffset-5000, maxOffset); // y
          if (mScroller.computeScrollOffset()) { mFlingRunnable = new FlingRunnable(coordinatorLayout, layout, minOffset); ViewCompat.postOnAnimation(layout, mFlingRunnable); return true; } else { ... } }


          接著就是具體scrollNext方法了,具體就是找到底部的NestedScrollingChild(如RecyclerView,NestedScrollView,ViewPager,主要是這三個)。在FlingRunnable中新增ScrollItem字段用于處理scroll邏輯

          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); } } @Override public 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 animation ViewCompat.postOnAnimation(mLayout, this); } else { onFlingFinished(mParent, mLayout); } } }}


          而在新增的ScrollItem中,我們來處理對應(yīng)的scroll操作(NestedScrollView可以通過scrollTo,而RecyclerView則需要用LinearLayoutManager來控制了)
          public class ScrollItem {    private int type; //1: NestedScrollView   2:RecyclerView    private 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); } }
          }


          至于ViewPager,因為getChildAt會有空值問題,這里是通過adapter獲取fragment然后獲取rootView做處理
          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;    }}

          這里暫時沒做PagerAdapter的處理邏輯,ViewPager找到當(dāng)前item界面rootView后,需要找到需要繼續(xù)慣性滑動到RecyclerView或NestedScrollView,為方便查找,我們給fragment布局中需要滑動的組件添加tag:“fling”,這樣就可以通過findViewWithTag("fling")找到它。

          好了,基本的滑動邏輯處理完了,我們自己的AppBarLayout可以慣性fling了。會看ScrollItem代碼,我加了stopScroll的邏輯。那是因為在底部recyclerView或NestedScrollView快速向下滑動至AppBarLayout展開,而這時在AppBarLayout想要快速向上滑動,應(yīng)為底部正在滑動,導(dǎo)致兩者沖突,不能正常向上滑動,所以AppBarLayout在向上快速滑動時,要停止底部滑動。通過NestedScrollView和RecyclerView的源碼,我們找到控制滑動邏輯的OverScroller和ViewFlinger,我們可以通過反射來停止對應(yīng)的滑動。

          源碼地址:
          https://github.com/iamyours/FlingAppBarLayout


          到這里就結(jié)束啦。
          瀏覽 37
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产精品色| 亚洲脚交| 国产精品人妻无码久久久苍井空 | 五月丁香婷婷国产 | 大香蕉社区 |