自定义AppBarLayout.Behavior
package com.tencent.cloud.baicorv.base.behavior;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.OverScroller;
import com.google.android.material.appbar.AppBarLayout;
import com.tencent.cloud.iov.util.log.LogUtils;
import java.lang.reflect.Field;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
public class CustomBehavior extends AppBarLayout.Behavior {
private static final String TAG = "AppbarLayoutBehavior";
private static final int TYPE_FLING = 1;
/**
* 记录是否有fling
*/
private boolean isFlinging;
/**
* 记录是否
*/
private boolean shouldBlockNestedScroll;
public CustomBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 是否拦截触摸事件
* @param parent CoordinatorLayout
* @param child AppBarLayout
* @param ev ev
* @return
*/
@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, AppBarLayout child, MotionEvent ev) {
LogUtil.d(TAG, "onInterceptTouchEvent:" + child.getTotalScrollRange());
shouldBlockNestedScroll = isFlinging;
switch (ev.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
//手指触摸屏幕的时候停止fling事件
stopAppbarLayoutFling(child);
break;
default:
break;
}
return super.onInterceptTouchEvent(parent, child, ev);
}
/**
* 反射获取私有的flingRunnable 属性,考虑support 28以后变量名修改的问题
* @return Field
* @throws NoSuchFieldException
*/
private Field getFlingRu