No adapter attached; skipping layout 原因、解决办法

简介: No adapter attached; skipping layout 原因、解决办法

当问题出现的时候不光要解决还要知道为什么

看一下源码:


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 异常


众所周知,RecyclerView的出现不光可以代替ListView,也可以代替GridView,所以啊大胸弟,你在用的时候要告诉RecyclerView你要代替的是哪个啊,就是所谓的初始化配置,不配置就会警告报错、不显示数据

分割线可以不设置,动画也可以不设置,但是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);
    }


当然,不要忘了 mRecyclerView.setAdapter(mAdapter);


几种LayoutManager


LinearLayoutManager 线性布局管理器


GridLayoutManager 表格布局管理器


StaggeredGridLayoutManager 瀑布流布局管理器

目录
相关文章
|
3月前
|
Java 开发工具 Android开发
解决flutter doctor出现Android license status unknown或cmdline-tools component is missing
解决flutter doctor出现Android license status unknown或cmdline-tools component is missing
79 4
解决flutter doctor出现Android license status unknown或cmdline-tools component is missing
|
3月前
|
Android开发
解决E/RecyclerView: No layout manager attached; skipping layout
解决E/RecyclerView: No layout manager attached; skipping layout
170 1
|
3月前
|
开发工具 Android开发
解决Manifest merger failed : android:exported needs to be explicitly specified for <activity>
解决Manifest merger failed : android:exported needs to be explicitly specified for <activity>
77 1
|
Android开发
Permission failure: android.permission.CAPTURE_AUDIO_OUTPUT 解决办法
Permission failure: android.permission.CAPTURE_AUDIO_OUTPUT 解决办法
395 0
|
编解码 机器人 Android开发
Android10.0 OTA 错误解决办法(@/cache/recovery/block.map‘ failed)
Android10.0 OTA 错误解决办法(@/cache/recovery/block.map‘ failed)
689 0
|
Android开发
清单文件合并失败Manifest merger failed : Attribute application@appComponentFactory value=(android.support.v4
清单文件合并失败Manifest merger failed : Attribute application@appComponentFactory value=(android.support.v4
清单文件合并失败Manifest merger failed : Attribute application@appComponentFactory value=(android.support.v4
|
Android开发
DSL element ‘android.dataBinding.enabled‘ is obsolete and has been replaced with ‘android.buildFeatu
DSL element ‘android.dataBinding.enabled‘ is obsolete and has been replaced with ‘android.buildFeatu
212 0