CoordinatorLayout中AppbarLayout的ScrollFlags效果

最近項(xiàng)目中用到了AppbarLayout的一些效果,功能簡單的實(shí)現(xiàn)了一下,但是為了弄清楚每個(gè)flag的情況,專門寫了一個(gè)demo來測試:

https://github.com/lijingnan/CoordinatorTest

這個(gè)Demo主要是用來測試CoordinatorLayout中AppbarLayout的ScrollFlags效果。

ScrollFlags的五個(gè)類型:

  1. SCROLL_FLAG_ENTER_ALWAYS

    When entering (scrolling on screen) the view will scroll on any downwards scroll event, regardless of whether the scrolling view is also scrolling.

    當(dāng)((entering) / (scrolling on screen))下拉的時(shí)候,這個(gè)View也會(huì)跟著滑出。

  2. SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED

    An additional flag for 'enterAlways' which modifies the returning view to only initially scroll back to it's collapsed height.

    另一種enterAlways,但是只顯示折疊后的高度。

  3. SCROLL_FLAG_EXIT_UNTIL_COLLAPSED

    When exiting (scrolling off screen) the view will be scrolled until it is 'collapsed'.

    當(dāng)((exiting) / (scrolling off screen))上拉的時(shí)候,這個(gè)View會(huì)跟著滑動(dòng)直到折疊。

  4. SCROLL_FLAG_SCROLL

    The view will be scroll in direct relation to scroll events.

    這個(gè)View將會(huì)響應(yīng)Scroll事件

  5. SCROLL_FLAG_SNAP

    Upon a scroll ending, if the view is only partially visible then it will be snapped and scrolled to it's closest edge.

    在Scroll滑動(dòng)事件結(jié)束以前 ,如果這個(gè)View部分可見,那么這個(gè)View會(huì)停在最接近當(dāng)前View的位置

結(jié)合項(xiàng)目

直接看上邊5中flag的表述不容易理解每個(gè)的作用,結(jié)合本demo則可以直觀的看出他們區(qū)別,用通俗的語言再描述一次:

  1. SCROLL_FLAG_ENTER_ALWAYS

    當(dāng)RecyclerView上滑時(shí)Toolbar隱藏,當(dāng)RecyclerView下滑時(shí)Toolbar顯示;

  2. SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED

    當(dāng)RecyclerView上滑時(shí)Toolbar隱藏,當(dāng)RecyclerView下滑到頂部時(shí)Toolbar顯示;(注意是頂部)

  3. SCROLL_FLAG_EXIT_UNTIL_COLLAPSED

    只有AppBarLayout的子View大于1的時(shí)候才有效

  4. SCROLL_FLAG_SCROLL

    要想使用其他4個(gè)flag,必須同時(shí)使用這個(gè)scroll;

  5. SCROLL_FLAG_SNAP

    這個(gè)的效果跟SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED基本一樣,只是在Toolbar顯示和隱藏的時(shí)候有點(diǎn)區(qū)別:如果Toolbar顯示"過半"則全部顯示,隱藏"過半"則全部隱藏;

  6. 當(dāng)AppBarLayout有多個(gè)子View都設(shè)置了ScrollFlag時(shí),滑動(dòng)事件會(huì)按照順序依次判斷對該view執(zhí)行那種flag,例如demo中有5個(gè)子view,當(dāng)上滑時(shí)是從上往下依次執(zhí)行對應(yīng)的flag,當(dāng)下滑時(shí)也是依次執(zhí)行對應(yīng)的flag;

ScrollFlags的設(shè)置方式

  1. 在xml代碼里直接設(shè)置
   <android.support.v7.widget.Toolbar
       android:id="@+id/toolbar"
       android:layout_width="match_parent"
       android:layout_height="?attr/actionBarSize"
       android:background="?attr/colorPrimary"
       app:layout_scrollFlags="scroll|enterAlwaysCollapsed"
       app:popupTheme="@style/AppTheme.PopupOverlay"
       />
  1. 在java代碼中設(shè)置
    Toolbar mToolbar = findViewById(R.id.toolbar);
    
    AppBarLayout.LayoutParams mLayoutParams = (AppBarLayout.LayoutParams) mToolbar.getLayoutParams();
    
    mLayoutParams.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL
                        | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
                        
    mToolbar.setLayoutParams(mLayoutParams);
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • CoordinatorLayout與滾動(dòng)的處理 CoordinatorLayout實(shí)現(xiàn)了多種Material De...
    cxm11閱讀 6,811評論 1 15
  • 簡單復(fù)習(xí) 這篇也就簡單使用,和前面關(guān)系不大就暫時(shí)不復(fù)習(xí)了這篇可能有點(diǎn)啰嗦,并且只是使用,沒有難度熟悉的同學(xué)略過前面...
    dodo_lihao閱讀 6,145評論 1 19
  • 最新項(xiàng)目中用到了些Material效果,在此對自己的學(xué)習(xí)做個(gè)小結(jié)。 首先養(yǎng)成良好的學(xué)習(xí)習(xí)慣-----看源碼: Co...
    風(fēng)少俠閱讀 4,940評論 5 37
  • 為了使得Toolbar有滑動(dòng)效果,必須做到如下三點(diǎn) 1. CoordinatorLayout作為布局的父布局容器。...
    零寬度接合閱讀 620評論 0 0
  • 讓你迷茫的原因只有一個(gè):說的太多,做的太少,你要知道,只有行動(dòng)才能造就一個(gè)人。 不記得在哪里看到的這句話,好像有很...
    行小星閱讀 358評論 10 1

友情鏈接更多精彩內(nèi)容