问题:APPBarlayout+RecyclerView滑动后,item在一段时间内无法点击
或:使用AppBarLayout和RecyclerView时,RecyclerView上拉加载会出现不能及时监听到达底部
都是这个原因
猜测:RecyclerView滑动时可能与AppBarLayout发生冲突
尝试:去掉AppBarLayout时,能够及时监听
定位:那么应该是和 NestScroll 相关了,重写 AppBarLayout.Behavior 打印 log,发现在快速滑动到顶部和底部之后,AppBarLayout 在一段时间内还处于 Fling 状态,那么我们想办法把这段无效的 Fling 干掉就好了。
解决:最后翻找 google 的时候发现这是 google 在修复上个版本嵌套滑动的时候引进来的新 bug。。。
public class FixAppBarLayoutBehavior extends AppBarLayout.Behavior {
public FixAppBarLayoutBehavior() {
super();
}
public FixAppBarLayoutBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target,
int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) {
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed,
dxUnconsumed, dyUnconsumed, type);
stopNestedScrollIfNeeded(dyUnconsumed, child, target, type);
}
@Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child,
View target, int dx, int dy, int[] consumed, int type) {
super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
stopNestedScrollIfNeeded(dy, child, target, type);
}
private void stopNestedScrollIfNeeded(int dy, AppBarLayout child, View target, int type) {
if (type == ViewCompat.TYPE_NON_TOUCH) {
final int currOffset = getTopAndBottomOffset();
if ((dy < 0 && currOffset == 0)
|| (dy > 0 && currOffset == -child.getTotalScrollRange())) {
ViewCompat.stopNestedScroll(target, ViewCompat.TYPE_NON_TOUCH);
}
}
}
}
最后在 AppBarLayout中使用
<android.support.design.widget.AppBarLayout
android:id="@+id/layout_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
app:layout_behavior="包名.FixAppBarLayoutBehavior"
app:elevation="0dp">
转载:https://blog.csdn.net/ganyao939543405/article/details/80651582