Recycle View清空上一次的显示的数据
原创
©著作权归作者所有:来自51CTO博客作者wx63637f8e01d6a的原创作品,请联系作者获取转载授权,否则将追究法律责任
尝试失败的方法
//失败原因:你没判断recycleview是否有item view,没有的时候会崩溃
//mRecyclerView.removeAllViews();
//失败原因:上一个item是否存在的问题依旧存在,notifyAll()是合适的同时数据更改
//mRecyclerView.removeAllViews();
//mRecyclerView.notifyAll();
//失败原因:数据是被清空了,但是在第二次填充数据的时候崩溃
//mRecyclerView.setAdapter(new CustomRecycleAdapter(SearchActivity.this,null));
//失败原因:缺少第一个判断irem是否存在的问题
//adapter.updateData(new ArrayList<RecycleDao>());
正确方法
- 判断recycleview是否存在item view
getChildCount>0 - 移除所有view
removeAllViews - 通知数据更新
notifyDataSetChanged()
//如果有数据或者recycle view有item view就删除;否则程序崩溃,找不到item
if (mRecyclerView.getChildCount() > 0 ) {
mRecyclerView.removeAllViews();
adapter.updateData(null);
}
/**
* 更新数据
* @param data
*/
public void updateData(List<RecycleDao> data){
this.mData =data;
notifyDataSetChanged();
}