刷新Adapter报java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing

今天在做EditText文本监控TextWatcher时,刷新Adapter,系统崩溃,报如下异常,再此记录下修改的方法刷新Adapter报java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing
如上图所示,
错误翻译:RecyclerView正在计算布局或滚动应用程序时,无法调用此方法
报错原因为:在一个ViewHolder.onBind()里面用通过bus发消息去通知刷新列表notifyDataSetChanged(),这个时候刚好列表在滚动,那么就会报这个错。
解决方案:使用Handler,加在需要刷新的地方
刷新Adapter报java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing
原理:
主线程刷新UI是通过消息队列,当列表正在滚动或者layout时调用notifyDataSetChanged(),
那么notifyDataSetChanged()里面的代码是和正在滚动或者layout同一消息里面的,如果加上Handler.post(),那么就是新建立消息放入消息队列末尾,这样两个刷新不在同一个消息,就完美避开了这个问题。

灵感来源