RecyclerView中assertNotInLayoutOrScroll()和isComputingLayout()

isComputingLayout()函数的官方解释:

Returns whether RecyclerView is currently computing a layout.

If this method returns true, it means that RecyclerView is in a lockdown state and any
attempt to update adapter contents will result in an exception because adapter contents
cannot be changed while RecyclerView is trying to compute the layout.

It is very unlikely that your code will be running during this state as it is called by the framework when a layout traversal happens or RecyclerView starts to scroll
in response to system events (touch, accessibility etc).

This case may happen if you have some custom logic to change adapter contents in
response to a View callback (e.g. focus change callback) which might be triggered during a
layout calculation. In these cases, you should just postpone the change using a Handler or a similar mechanism.

@return true if RecyclerView is currently computing a layout, false otherwise

简单翻译就是:

这个函数返回RecyclerView当前是否正在computing a layout,计算布局。也就是说是否还未绘制完成。

如果是的话,就意味着当前Recyclerview处在一个lockdown state,锁定状态,任何update adapter contents更新adapter内容的尝试,都将会导致一个意外,因为当RecyclerView正在试着测量布局时,无法改变adapter的内容。

当a layout traversal(布局穿越?)发生或者你的RecyclerView在系统事件(如:touch事件,辅助功能事件等)下开始scroll滚动时被framework框架调用,在这些情况下你的代码是不太可能正在运行的。

如果你在一个在layout测量计算可能触发的View callback界面回调(如:焦点改变的回调)中有一些change adapter contents改变adapter内容的常见逻辑,那么,就有可能导致这个函数的调用,有可能throw 一个 IllegalStateException(“Cannot call this method while RecyclerView is computing a layout or scrolling”);

在这些情况下,你应该利用Handler或者一个类似的机制来推迟change改变的发生

assertNotInLayoutOrScroll(),这是一个断言。

Checks if RecyclerView is in the middle of a layout or scroll and throws an IllegalStateException if it is。

检查recyclerview是否正在布局或者滚动过程中,如果是的话,跑出一个IllegalStateException意外

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值