
看一下源码:
void dispatchLayout() {
if (mAdapter == null) {
Log.e(TAG, "No adapter attached; skipping layout");
// leave the state in START
return;
}
if (mLayout == null) {
Log.e(TAG, "No layout manager attached; skipping layout");
// leave the state in START
return;
}
....
}当Adapter和LayoutManager 都没有的时候,就会抛出No … attached; skipping layout 异常
LayoutManager必须设置。随意示范一下
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(linearLayoutManager);上面用的是LinearLayoutManager的第二个构造方法,必要的参数都有了,当然也可以用第一个构造,贴一下这个构造的代码:
/**
* @param context Current context, will be used to access resources.
* @param orientation Layout orientation. Should be {@link #HORIZONTAL} or {@link
* #VERTICAL}.
* @param reverseLayout When set to true, layouts from end to start.
*/
public LinearLayoutManager(Context context, int orientation, boolean reverseLayout) {
setOrientation(orientation);
setReverseLayout(reverseLayout);
setAutoMeasureEnabled(true);
}或者 简单版,默认 VERTICAL
mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext)); /**
* Creates a vertical LinearLayoutManager
*
* @param context Current context, will be used to access resources.
*/
public LinearLayoutManager(Context context) {
this(context, VERTICAL, false);
}几种LayoutManager