记录一个AppBarLayout的bugfix修复

问题现象: CoordinatorLayout嵌套 上半部分AppBarLayout+下半部分RecyclerView, 对下半部分RecyclerView的显示隐藏做操作(如吟唱recyclerView展示一个ErrorLayout or EmptyLayout, 会导致AppBarLayout区域不再响应滑动, 这个bug较容易忽略, 因为滑动界面上的下半部分立即可以恢复滑动使现象消失.

这个问题是由系统源码导致, 里记录一下原因分析:

从CoordinatorLayout看起:

HeaderBehavior:

AppBarlayout:

lastNestedScrollingChildRef的赋值只有两处,

less 复制代码
@Override
public boolean onStartNestedScroll(
    @NonNull CoordinatorLayout parent,
    @NonNull T child,
    @NonNull View directTargetChild,
    View target,
    int nestedScrollAxes,
    int type) {
  //省略...
  // A new nested scroll has started so clear out the previous ref
  lastNestedScrollingChildRef = null;
  //省略...
  return started;
}

@Override
public void onStopNestedScroll(
    CoordinatorLayout coordinatorLayout, @NonNull T abl, View target, int type) {

  //省略... 
  // Keep a reference to the previous nested scrolling child
  lastNestedScrollingChildRef = new WeakReference<>(target);
}

所以lastNestedScrollingChildRef得含义和名字一样, 是上一次的嵌套滚动view, 如果我们滑动了RecyclerView, 那么这个值就是RecyclerView, 如果此时把RecyclerView隐藏, 再滑动AppBarLayout区域, 就会滑不动了, 因为AppBarLayout中CanDragView中判断RecyclerView isShown()为false.

为什么滑动一下AppBarLayout下面的错误界面或空界面区域就会恢复呢? 因为在View中有如下一段代码:

scss 复制代码
public boolean dispatchTouchEvent(MotionEvent event) {
    //...
    final int actionMasked = event.getActionMasked();
    if (actionMasked == MotionEvent.ACTION_DOWN) {
        // Defensive cleanup for new gesture
        stopNestedScroll();
    }
    //...

由前面源码得知, stopNestedScroll();中会对lastNestedScrollingChildRef重新赋值. 这也是为什么是在 lastNestedScrollingChildRef是在stopNestedScroll();中赋值, 而每次开始滑动是lastNestedScrollingChildRef总是不为空.

至此, AppBarLayout偶现滑不动的原因就清楚了.

相关推荐
安卓理事人3 小时前
安卓图表MpAndroidChart使用
android
奋斗的小鹰4 小时前
在已有Android工程中添加Flutter模块
android·flutter
介一安全5 小时前
【Frida Android】实战篇13:企业常用非对称加密场景 Hook 教程
android·网络安全·逆向·安全性测试·frida
lin62534226 小时前
Android右滑解锁UI,带背景流动渐变动画效果
android·ui
鹏多多8 小时前
Flutter输入框TextField的属性与实战用法全面解析+示例
android·前端·flutter
2501_916008898 小时前
iOS 开发者工具全景图,构建从编码、调试到性能诊断的多层级工程化工具体系
android·ios·小程序·https·uni-app·iphone·webview
Winter_Sun灬9 小时前
CentOS 7 编译安卓 arm64-v8a 版 OpenSSL 动态库(.so)
android·linux·centos
柯南二号9 小时前
【大前端】【Android】用 Python 脚本模拟点击 Android APP —— 全面技术指南
android·前端·python
龚礼鹏9 小时前
图像显示框架六——SurfaceFlinger的初始化以及任务调度(基于Android 15源码分析)
android
壮哥_icon9 小时前
Android 使用 PackageInstaller 实现静默安装,并通过 BroadcastReceiver 自动重启应用
android·gitee·android-studio·android系统