问题说明:
开发中经常会用到RecyclerView做布局的情况,然后在启动界面的时候因为是从数据库或网络等地方获取数据有延迟,在没有获取到数据时候就开始加载RcyclerView就会出现RecyclerView: No adapter attached; skipping layout致使APP无缘无故崩溃。
解决办法:
先将RecyclerView的布局setVisibility(View.GONE)隐藏,优化一些则加上一个加载画面,数据加载完成则再将RecyclerView的布局setVisibility(View.VISIBLE),加载画面隐藏。
以下用伪代码简单展示一下:
onCrete{
pbLoading.setVisibility(View.VISIBLE);
RecyclerView.setVisibility(View.GONE);
监听数据加载完成后
pbLoading.setVisibility(View.GONE);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(activity);
RecyclerView.setLayoutManager(linearLayoutManager);
RecyclerViewAdapter = new RecyclerViewAdapter(this,数据);
RecyclerView.setVisibility(View.VISIBLE);
RecyclerView.setAdapter(calendarAdapter);
}