国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > 综合技术 > 6AppBarLayout与scrollFlags

6AppBarLayout与scrollFlags

来源:程序员人生   发布时间:2016-12-14 08:53:58 阅读次数:2738次

6AppBarLayout与scrollFlags

AppBarLayout分组

在这里说过AppBarLayout可以分为可滑出和不可滑出上下2部份,其实细致1点可以分3部份,以下图所示,下滑最后出现(part 1),下滑立刻出现(part2),没法滑出(part3),其中part1和2合起来就是可以滑出的部份。

xml代码以下

<android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay" android:layout_height="wrap_content"> <TextView android:gravity="center" app:layout_scrollFlags="scroll" android:textSize="20sp" android:text="下滑最后出现" android:background="#447700" android:layout_width="match_parent" android:layout_height="70dp" /> <TextView android:gravity="center" app:layout_scrollFlags="scroll|enterAlways" android:textSize="20sp" android:text="下滑立刻出现" android:background="#004477" android:layout_width="match_parent" android:layout_height="100dp" /> <TextView android:gravity="center" android:textSize="20sp" android:text="没法滑出去" android:background="#ff0000" android:layout_width="match_parent" android:layout_height="100dp" /> </android.support.design.widget.AppBarLayout>

主要关注layout_scrollFlags,可以看到part3无scroll标志,代表没法滚出;part2是scroll|enterAlways代表下滑立刻出现;part1是scroll下滑的时候最后出现。
为何会这样,主要和mDownPreScrollRange、mDownScrollRange有关,可以看下边代码。mDownPreScrollRange控制着嵌套滑动的父view的onNestedPreScroll部份可滑距离,mDownScrollRange控制着嵌套滑动的父view的onNestedScroll部份。

//AppBarLayout /** * Return the scroll range when scrolling down from a nested pre-scroll. */ private int getDownNestedPreScrollRange() { if (mDownPreScrollRange != INVALID_SCROLL_RANGE) { // If we already have a valid value, return it return mDownPreScrollRange; } int range = 0; for (int i = getChildCount() - 1; i >= 0; i--) { final View child = getChildAt(i); final LayoutParams lp = (LayoutParams) child.getLayoutParams(); final int childHeight = child.getMeasuredHeight(); final int flags = lp.mScrollFlags; if ((flags & LayoutParams.FLAG_QUICK_RETURN) == LayoutParams.FLAG_QUICK_RETURN) { // First take the margin into account range += lp.topMargin + lp.bottomMargin; // The view has the quick return flag combination... if ((flags & LayoutParams.SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED) != 0) { // If they're set to enter collapsed, use the minimum height range += ViewCompat.getMinimumHeight(child); } else if ((flags & LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED) != 0) { // Only enter by the amount of the collapsed height range += childHeight - ViewCompat.getMinimumHeight(child); } else { // Else use the full height range += childHeight; } } else if (range > 0) { // If we've hit an non-quick return scrollable view, and we've already hit a // quick return view, return now break; } } return mDownPreScrollRange = Math.max(0, range - getTopInset()); } /** * Return the scroll range when scrolling down from a nested scroll. */ private int getDownNestedScrollRange() { if (mDownScrollRange != INVALID_SCROLL_RANGE) { // If we already have a valid value, return it return mDownScrollRange; } int range = 0; for (int i = 0, z = getChildCount(); i < z; i++) { final View child = getChildAt(i); final LayoutParams lp = (LayoutParams) child.getLayoutParams(); int childHeight = child.getMeasuredHeight(); childHeight += lp.topMargin + lp.bottomMargin; final int flags = lp.mScrollFlags; if ((flags & LayoutParams.SCROLL_FLAG_SCROLL) != 0) { // We're set to scroll so add the child's height range += childHeight; if ((flags & LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED) != 0) { // For a collapsing exit scroll, we to take the collapsed height into account. // We also break the range straight away since later views can't scroll // beneath us range -= ViewCompat.getMinimumHeight(child) + getTopInset(); break; } } else { // As soon as a view doesn't have the scroll flag, we end the range calculation. // This is because views below can not scroll under a fixed view. break; } } return mDownScrollRange = Math.max(0, range); }

实际效果以下所示

scrollFlags

-scroll代表可转动,被标注后算到mTotalScrollRange里,要写其他flag必须先写scroll才有效
-enterAlways下滑,这个view立刻跑出来,算在mDownPreScrollRange内
-enterAlwaysCollapsed下滑的时候在onNestedPreScroll阶段先滑出1个最小高度,这个参数我试了下都存在1定问题,没找到1个适合的场景。用enterAlwaysCollapsed必须先写 scroll和enterAlways
-exitUntilCollapsed 向上转动直到折叠,常常用于CollapsingToolbarLayout内,后边会有介绍

生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生