最近項(xiàng)目中用到了AppbarLayout的一些效果,功能簡單的實(shí)現(xiàn)了一下,但是為了弄清楚每個(gè)flag的情況,專門寫了一個(gè)demo來測試:
https://github.com/lijingnan/CoordinatorTest
這個(gè)Demo主要是用來測試CoordinatorLayout中AppbarLayout的ScrollFlags效果。
ScrollFlags的五個(gè)類型:
-
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ì)跟著滑出。
-
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,但是只顯示折疊后的高度。
-
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)直到折疊。
-
SCROLL_FLAG_SCROLL
The view will be scroll in direct relation to scroll events.
這個(gè)View將會(huì)響應(yīng)Scroll事件
-
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ū)別,用通俗的語言再描述一次:
-
SCROLL_FLAG_ENTER_ALWAYS
當(dāng)RecyclerView上滑時(shí)Toolbar隱藏,當(dāng)RecyclerView下滑時(shí)Toolbar顯示;
-
SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED
當(dāng)RecyclerView上滑時(shí)Toolbar隱藏,當(dāng)RecyclerView下滑到頂部時(shí)Toolbar顯示;(注意是頂部)
-
SCROLL_FLAG_EXIT_UNTIL_COLLAPSED
只有AppBarLayout的子View大于1的時(shí)候才有效
-
SCROLL_FLAG_SCROLL
要想使用其他4個(gè)flag,必須同時(shí)使用這個(gè)scroll;
-
SCROLL_FLAG_SNAP
這個(gè)的效果跟SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED基本一樣,只是在Toolbar顯示和隱藏的時(shí)候有點(diǎn)區(qū)別:如果Toolbar顯示"過半"則全部顯示,隱藏"過半"則全部隱藏;
當(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è)置方式
- 在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"
/>
- 在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);