异常信息
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;
}