前言:在项目中遇到需要一个需求是要用到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的。具体看参考链接。