1. 程式人生 > >AppBarLayout+CollapsingToolbarLayout+TabLayout+ViewPager 組合使用

AppBarLayout+CollapsingToolbarLayout+TabLayout+ViewPager 組合使用

前言:在專案中遇到需要一個需求是要用到AppBarLayout和CollapsingToolBarLayout實現上下滑動頭部佈局隨之展開和收縮的效果,頭佈局下面 需要結合tablayout和viewPager和Fragment點選切換。Fragment中包含RecyclerView 或者NestScrollView。(這裡是不帶toolBar的效果)

大概就這樣子吧

先貼我的佈局吧:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" android:fitsSystemWindows="true" > <android.support.design.widget
.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#7f000000" app:layout_behavior="包名.FlingBehavior" android:fitsSystemWindows="true"> <android.support.design.widget.CollapsingToolbarLayout android:layout_width="match_parent"
android:layout_height="match_parent" app:layout_scrollFlags="scroll|exitUntilCollapsed" app:titleEnabled="false"> <include layout="@layout/head_view" /> </android.support.design.widget.CollapsingToolbarLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.design.widget.TabLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" app:tabSelectedTextColor="@color/textColor_FF5722" app:tabTextColor="@color/black" /> </LinearLayout> </android.support.design.widget.AppBarLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" app:layout_behavior="@string/appbar_scrolling_view_behavior" > <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </android.support.design.widget.CoordinatorLayout>

在使用appBarLayout的時候需要注意新增一下屬性

1.在CollapsingToolbarLayout中需要新增

app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:titleEnabled="false"//可選

2.在需要滑動的佈局中新增


app:layout_behavior="@string/appbar_scrolling_view_behavior"

可能出現的問題

1.RecyclerView所在的Fragment可能存在 RV向下滑動的時候 頭佈局滑動不流暢,這樣的問題也存在於NestScrollView向上滑動的時候。

就上面提及的問題解決方法:

1.RecyclerView

問題應該是 appBarLayout沒有獲得fling指令導致卡頓,應該是recyclerView在分發fling的時候出現了錯誤。
在StackOverFlow上有人提出瞭解決辦法:
自定義Behavior

copy程式碼—>在佈局檔案中新增屬性app:layout_behavior="包名.Behavior名稱"

2.NestScrollView

自定義一個NestScrollView。
將NestScrollView中的程式碼全部copy—->並在onTouchEvent()方法的case MotionEvent.ACTION_UP: 中去掉if (mIsBeingDragged) 判斷。

原因:`case MotionEvent.ACTION_UP:

            if (mIsBeingDragged) {
                final VelocityTracker velocityTracker = mVelocityTracker;

                velocityTracker.addMovement(vtev);

                velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
                int initialVelocity = (int) VelocityTrackerCompat.getYVelocity(velocityTracker,
                        mActivePointerId);
                if ((Math.abs(initialVelocity) > mMinimumVelocity)) {
                    flingWithNestedDispatch(-initialVelocity);
                } else if (mScroller.springBack(getScrollX(), getScrollY(), 0, 0, 0,
                        getScrollRange())) {
                    ViewCompat.postInvalidateOnAnimation(this);
                }
            }
            mActivePointerId = INVALID_POINTER;
            endDrag();
            break;`

觸發up事件的時候會來處理fling事件。flingWithNestedDispatch(-initialVelocity) 用來發送fling事件和處理fling事件。出現卡頓的時候就是if (mIsBeingDragged) 這個判斷為false的時候。

總結:很多時候遇到問題記下來印象會深很多,雖然很多也都是照著其他的部落格copy的。具體看參考連結。

參考: