“Cannot call this method while RecyclerView is computing a layout”异常解决

异常信息

java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling androidx.recyclerview.widget.RecyclerView{b2ebaa8 VFED..... ......ID 0,30-1080,1227 #7f09039c app:id/rv_fragment_buy_cart}, adapter:com.exinetian.app.ui.client.adapter.BuyCartAdapter@5b4d9c1, layout:androidx.recyclerview.widget.LinearLayoutManager@49fb566, context:com.exinetian.app.ui.client.MainActivity@52b2556
        at androidx.recyclerview.widget.RecyclerView.assertNotInLayoutOrScroll(RecyclerView.java:2880)
        at androidx.recyclerview.widget.RecyclerView$RecyclerViewDataObserver.onChanged(RecyclerView.java:5281)
        at androidx.recyclerview.widget.RecyclerView$AdapterDataObservable.notifyChanged(RecyclerView.java:11997)
        at androidx.recyclerview.widget.RecyclerView$Adapter.notifyDataSetChanged(RecyclerView.java:7070)

异常源码分析

参考【只看源码分析结论是错误的】: IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling 文章

问题本质是

setChecked方法触发onCheckedChanged问题,onCheckedChanged又嵌套setChecked方法

导致RecylerView一直刷新,而我们又要强行要他在刷一次

解决

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    //区分是人为触发,还是代码setChecked,阻断嵌套循环
    if(!buttonView.isPressed())
        return; 
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值