本篇帖子用于记录在开发过程中有关recyclerView的错误总结,会慢慢更新...
1.非法状态异常
java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling android.support.v7.widget.RecyclerView{a767943 VFED..... .F....ID 0,0-1080,1594 #7f07006c app:id/recyclerShopcart}, adapter:com.bawie.bawaymall160a.shopcart.adapter.ShopcartRecyclerAdapter@a363bc0, layout:android.support.v7.widget.LinearLayoutManager@5cc20f9, context:com.bawie.bawaymall160a.shopcart.activity.ShopcartActivity@6408c0
at android.support.v7.widget.RecyclerView.assertNotInLayoutOrScroll(RecyclerView.java:2880)
at android.support.v7.widget.RecyclerView$RecyclerViewDataObserver.onChanged(RecyclerView.java:5281)
at android.support.v7.widget.RecyclerView$AdapterDataObservable.notifyChanged(RecyclerView.java:11997)
at android.support.v7.widget.RecyclerView$Adapter.notifyDataSetChanged(RecyclerView.java:7070)
at com.bawie.bawaymall160a.shopcart.adapter.ShopcartRecyclerAdapter$MyViewHodler$1.onCheckedChanged(ShopcartRecyclerAdapter.java:134)
at android.widget.CompoundButton.setChecked(CompoundButton.java:156)
at com.bawie.bawaymall160a.shopcart.adapter.ShopcartRecyclerAdapter.onBindViewHolder(ShopcartRecyclerAdapter.java:63)
at com.bawie.bawaymall160a.shopcart.adapter.ShopcartRecyclerAdapter.onBindViewHolder(ShopcartRecyclerAdapter.java:24)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
关键词:Cannot call this method while RecyclerView is computing a layout or scrolling
大概意思是说无法调用这个方法当recyclerView正在计算布局或者滚动的时候。
这个异常通常是在我们在动态更新recyclerView的布局的时候出现,常常伴随这样的代码
class MyViewHodler extends RecyclerView.ViewHolder {
TextView tvShopName;
RecyclerView recyclerGoods;
CheckBox checkBox;
public MyViewHodler(@NonNull View itemView) {
super(itemView);
tvShopName = itemView.findViewById(R.id.tvShopName);
recyclerGoods = itemView.findViewById(R.id.recyclerGoods);
checkBox = itemView.findViewById(R.id.checkbox);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//各种处理数据的代码
//处理完数据去更新recyclerView,此时会报上述异常
notifyDataSetChanged();
}
});
}
}
解决方案:
class MyViewHodler extends RecyclerView.ViewHolder {
TextView tvShopName;
RecyclerView recyclerGoods;
CheckBox checkBox;
public MyViewHodler(@NonNull View itemView) {
super(itemView);
tvShopName = itemView.findViewById(R.id.tvShopName);
recyclerGoods = itemView.findViewById(R.id.recyclerGoods);
checkBox = itemView.findViewById(R.id.checkbox);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//各种处理数据的代码
//更新数据的时候使用handler
new Handler().post(new Runnable() {
@Override
public void run() {
notifyDataSetChanged();
}
});
}
});
}
}
2.非法状态异常
FATAL EXCEPTION: main
java.lang.IllegalArgumentException: view is not a child, cannot hideandroid.widget.LinearLayout{ef2ld2d V.E............I.0,1926-1080,2076}
FATAL EXCEPTION: main
Process:com.wuba,PID:3847
java,lang.IllegalArgumentException: view is not a child, cannot hideandroid.widget.LinearLayout{ef21d2d V.E...........I.8,1926-1080,2076}at androidx.recyclerview.widget.ChildHelper.unhide(ChildHelper.java:352)
at
androidx. recyclerview.widget.RecyclerView$Recycler.getscrapOrHiddenOrCachedHolderForPosition(RecyclerView.java:6857)
at
androidx. recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6337)
at
androidx. recyclerview.widget.GapWorker.prefetchPositionWithDeadline(GapWorker.java:288)
at androidx. recyclerview.widget.GapWorker.flushTaskwithDeadline(GapWorker.java:345)at androidx.recyclerview.widget.GapWorker.flushTasksWithDeadline(GapWorker.java:361)at androidx.recyclerview.widget.GapWorker.prefetch(GapWorker.java:368)
at androidx.recyclerview.widget.GapWorker.run(GapWorker.java:399)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:259)
at android.app.ActivityThread.main(ActivityThread.java:8208)
at java.lang.reflect.Method.invoke(Native Method)at com.android,internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.iava:592)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:952)
关键词:view is not a child, cannot hide android.widget.LinearLayout
出现异常的代码是这样调用的
public void addItemsData(List<ListItemDataBean> list) {
if (list == null || list.size() == 0) return;
addItems(list);
notifyItemRangeChanged(getItems().size() - 1,list.size());
}
修改后
public void addItemsData(List<ListItemDataBean> list) {
if (list == null || list.size() == 0) return;
addItems(list);
if (list.size() > 1) {
notifyItemRangeChanged(getItems().size() - 1, list.size());
} else {
notifyDataSetChanged();
}
}