原代码如下:
JSONObject ret = new JSONObject(event.info());
mWifiScanResult = new JSONArray(ret.getString("data"));
mRecyclerView.getAdapter().notifyDataSetChanged();
其中mWifiScanResult为RecyclerView的数据源,为JSON格式。然而在Android模拟器上执行后并没有刷新数据,需要手动滚动一下才刷新数据。
解决方法:
不能修改完数据就马上调用notifyDataSetChanged(),而是应该延迟一下,如:
JSONObject ret = new JSONObject(event.info());
mWifiScanResult = new JSONArray(ret.getString("data"));
myHandler.postDelayed(new Runnable() {
@Override
public void run() {
mRecyclerView.getAdapter().notifyDataSetChanged();
}
}, 500);
问题完美解决。但到底是什么原理呢?