当for循环遍历完成,立刻更新适配器adapter.notifyDataSetChanged();,出现了这个错java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling android.support.v7.widget.RecyclerView
非法状态异常:当recyclerview正在计算布局或滚动条时,无法调用此方法。
解决方法:
new Handler().post(new Runnable() {
@Override
public void run() {
// 刷新操作
adapter.notifyDataSetChanged();
}
});
以上是在主线程中,如果在子线程中就会报java.lang.RuntimeException: Can’t create handler inside thread that has not called Looper.prepare(),其实就是如果在主线程中创建handler时,系统会自动创建Looper,但是在子线程中创建handler时,是不会自动创建Looper的,此时如果不手动创建Looper,系统就会崩溃。
在子线程中是这样:
new Thread(new Runnable() {
@Override
public void run() {
Looper.prepare();
Looper.loop();
new Handler().post(new Runnable() {
@Override
public void run() {
// 刷新操作
adapter.notifyDataSetChanged();
}
});
}
}).start();