AppBarLayout+RecyclerView滑动趋势问题,一直是google官方的bug,至今尚未修复。
废话不多说,直接上代码
1、xml文件
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="xxxx.AppBarLayoutBehavior"
app:elevation="0dp">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="内容显示区域" />
<!-- 核心View,通过此View onTouch 来通知 RecyclerView 停止滑动趋势 -->
<LinearLayout
android:id="@+id/ll_stop_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</FrameLayout>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
2、自定义AppBarLayoutBehavior
public class AppBarLayoutBehavior extends AppBarLayout.Behavior {
private static final int TYPE_FLING = 1;
private boolean isFlinging;
private boolean shouldBlockNestedScroll;
public AppBarLayoutBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, AppBarLayout child, MotionEvent ev) {
shouldBlockNestedScroll = false;
if (isFlinging) {
shouldBlockNestedScroll = true;
}
switch (ev.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
stopAppbarLayoutFling(child); //手指触摸屏幕的时候停止fling事件
break;
}
return super.onInterceptTouchEvent(parent, child, ev);
}
/**
* 反射获取私有的flingRunnable 属性,考虑support 28以后变量名修改的问题
*
* @return Field
*/
private Field getFlingRunnableField() throws NoSuchFieldException {
try {
// support design 27及以下版本
Class<?> headerBehaviorType = this.getClass().getSuperclass().getSuperclass();
return headerBehaviorType.getDeclaredField("mFlingRunnable");
} catch (NoSuchFieldException e) {
// 可能是28及以上版本
Class<?> headerBehaviorType = this.getClass().getSuperclass().getSuperclass().getSuperclass();
return headerBehaviorType.getDeclaredField("flingRunnable");
}
}
/**
* 反射获取私有的scroller 属性,考虑support 28以后变量名修改的问题
*
* @return Field
*/
private Field getScrollerField() throws NoSuchFieldException {
try {
// support design 27及以下版本
Class<?> headerBehaviorType = this.getClass().getSuperclass().getSuperclass();
return headerBehaviorType.getDeclaredField("mScroller");
} catch (NoSuchFieldException e) {
// 可能是28及以上版本
Class<?> headerBehaviorType = this.getClass().getSuperclass().getSuperclass().getSuperclass();
return headerBehaviorType.getDeclaredField("scroller");
}
}
/**
* 停止appbarLayout的fling事件
*
* @param appBarLayout
*/
public void stopAppbarLayoutFling(AppBarLayout appBarLayout) {
//通过反射拿到HeaderBehavior中的flingRunnable变量
try {
Field flingRunnableField = getFlingRunnableField();
Field scrollerField = getScrollerField();
flingRunnableField.setAccessible(true);
scrollerField.setAccessible(true);
Runnable flingRunnable = (Runnable) flingRunnableField.get(this);
OverScroller overScroller = (OverScroller) scrollerField.get(this);
if (flingRunnable != null) {
appBarLayout.removeCallbacks(flingRunnable);
flingRunnableField.set(this, null);
}
if (overScroller != null && !overScroller.isFinished()) {
overScroller.abortAnimation();
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
@Override
public boolean onStartNestedScroll(CoordinatorLayout parent, AppBarLayout child, View directTargetChild, View target, int nestedScrollAxes, int type) {
stopAppbarLayoutFling(child);
return super.onStartNestedScroll(parent, child, directTargetChild, target, nestedScrollAxes, type);
}
@Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dx, int dy, int[] consumed, int type) {
//type返回1时,表示当前target处于非touch的滑动,
//该bug的引起是因为appbar在滑动时,CoordinatorLayout内的实现NestedScrollingChild2接口的滑动子类还未结束其自身的fling
//所以这里监听子类的非touch时的滑动,然后block掉滑动事件传递给AppBarLayout
if (type == TYPE_FLING) {
isFlinging = true;
}
if (!shouldBlockNestedScroll) {
super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
}
}
@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dxConsumed, int dyConsumed, int
dxUnconsumed, int dyUnconsumed, int type) {
if (!shouldBlockNestedScroll) {
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type);
}
}
@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, @NonNull AppBarLayout child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type, int[] consumed) {
if (!shouldBlockNestedScroll) {
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, type, consumed);
}
}
@Override
public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout abl, View target, int type) {
super.onStopNestedScroll(coordinatorLayout, abl, target, type);
isFlinging = false;
shouldBlockNestedScroll = false;
}
}
java代码:通过监听AppBarLayout 里面View的onTouch事件,来处理RecyclerView滑动趋势。
/**
* 通过监听 View 点击事件,来暂停RecyclerView 滑动趋势
*/
llStopScroll.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//recyclerView.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 0, 0, 0));
ViewCompat.stopNestedScroll(recyclerView);
}
return false;
}
});