记录一个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偶现滑不动的原因就清楚了.

相关推荐
诸神黄昏EX6 小时前
Android Build系列专题【篇四:编译相关语法】
android
雨白8 小时前
优雅地处理协程:取消机制深度剖析
android·kotlin
leon_zeng08 小时前
更改 Android 应用 ID (ApplicationId) 后遭遇记
android·发布
2501_9160074710 小时前
iOS 混淆工具链实战,多工具组合完成 IPA 混淆与加固(iOS混淆|IPA加固|无源码混淆|App 防反编译)
android·ios·小程序·https·uni-app·iphone·webview
Jeled11 小时前
Retrofit 与 OkHttp 全面解析与实战使用(含封装示例)
android·okhttp·android studio·retrofit
ii_best14 小时前
IOS/ 安卓开发工具按键精灵Sys.GetAppList 函数使用指南:轻松获取设备已安装 APP 列表
android·开发语言·ios·编辑器
2501_9159090614 小时前
iOS 26 文件管理实战,多工具组合下的 App 数据访问与系统日志调试方案
android·ios·小程序·https·uni-app·iphone·webview
limingade15 小时前
手机转SIP-手机做中继网关-落地线路对接软交换呼叫中心
android·智能手机·手机转sip·手机做sip中继网关·sip中继
RainbowC016 小时前
GapBuffer高效标记管理算法
android·算法
程序员码歌16 小时前
豆包Seedream4.0深度体验:p图美化与文生图创作
android·前端·后端