刚刚在我的代码中实现了RecyclerView,取代了ListView。

一切都很好。显示数据。

但是错误消息正在被记录:

15:25:53.476 E/RecyclerView: No adapter attached; skipping layout

15:25:53.655 E/RecyclerView: No adapter attached; skipping layout

对于以下代码:

ArtistArrayAdapter adapter = new ArtistArrayAdapter(this, artists);
recyclerView = (RecyclerView) findViewById(R.id.cardList);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

正如您所看到的,我已经为RecyclerView附加了一个适配器。 为什么我总是得到这个错误呢?

我读过与这个问题相关的其他问题,但没有一个有用。


当前回答

我有这个问题,一些时间的问题是recycleView放在ScrollView对象

检查实施后,原因似乎如下。如果RecyclerView被放到ScrollView中,那么在测量步骤中,它的高度是未指定的(因为ScrollView允许任何高度),结果是,它等于最小高度(根据实现),显然是零。

你有两个选项来解决这个问题:

设置一个高度为RecyclerView 设置滚动视图。fillViewport为true 或者保持RecyclerView在ScrollView之外。在我看来,这是目前为止最好的选择。如果RecyclerView的高度没有限制(当它被放入ScrollView中时就是这种情况),那么所有Adapter的视图在垂直方向上都有足够的位置,并且可以一次性创建。不再有视图回收,这有点破坏了RecyclerView的目的。

(可用于android.support.v4.widget。以及NestedScrollView)

其他回答

我有这个错误,我试着修复了一段时间,直到我找到解决方案。

我做了一个私有方法buildRecyclerView,我调用它两次,第一次在onCreateView,然后在我的回调(其中我从API获取数据)。这是我的方法buildRecyclerView在我的片段:

private void buildRecyclerView(View v) {
        mRecyclerView = v.findViewById(R.id.recycler_view_loan);
        mLayoutManager = new LinearLayoutManager(getActivity());
        ((LinearLayoutManager) mLayoutManager).setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mAdapter = new LoanAdapter(mExampleList);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
}

此外,我必须修改我的适配器中的get-Item-Count方法,因为On - On - create - view列表是空的,它通过一个错误。所以,我的get-Item-Count如下所示:

@Override
    public int getItemCount() {
        try {
            return mLoanList.size();
        } catch (Exception ex){return 0;}

    }

我因为这个问题失去了16分钟的生命,所以我要承认这个令人难以置信的尴尬的错误,我正在使用Butterknife,我在这个片段中绑定onCreateView中的视图。

花了很长时间才弄清楚为什么我没有布局管理器-但显然,视图是注入的,所以它们实际上不会为空,所以回收器永远不会为空。哎呀!

@BindView(R.id.recycler_view)
RecyclerView recyclerView;

    @Override
public View onCreateView(......) {
    View v = ...;
    ButterKnife.bind(this, v);
    setUpRecycler()
 }

public void setUpRecycler(Data data)
   if (recyclerView == null) {
 /*very silly because this will never happen*/
       LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
       //more setup
       //...
    }
    recyclerView.setAdapter(new XAdapter(data));
}

如果你遇到这样的问题,跟踪你的视图,使用uiautomatorviewer之类的东西

检查是否错过了在适配器中调用此方法

@Override
public int getItemCount() {
    return list.size();
}

只需将以下内容添加到RecyclerView

app:layoutManager="android.support.v7.widget.LinearLayoutManager"

例子:

   <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        app:layout_constraintBottom_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </android.support.v7.widget.RecyclerView>

确保你为你的RecyclerView设置了布局管理器:

mRecyclerView.setLayoutManager(new LinearLayoutManager(context));

除了LinearLayoutManager,你也可以使用其他的布局管理器。