刚刚在我的代码中实现了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;}
}
我得到了相同的两个错误消息,直到我在我的代码中修复了两个东西:
默认情况下,当你在RecyclerView中实现方法时。它生成的适配器:
@Override
public int getItemCount() {
return 0;
}
确保你更新了你的代码,让它说:
@Override
public int getItemCount() {
return artists.size();
}
显然,如果你的项目中没有项目,那么屏幕上显示的东西就会是零。
(2)我没有这样做,如上面的答案所示:CardView layout_width="match_parent"不匹配父RecyclerView宽度
//correct
LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_listitem, parent, false);
//incorrect (what I had)
LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_listitem,null);
(3)编辑:奖金:
还要确保你像这样设置你的RecyclerView:
<android.support.v7.widget.RecyclerView
android:id="@+id/RecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
不是这样的:
<view
android:id="@+id/RecyclerView"
class="android.support.v7.widget.RecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
我看过一些使用后一种方法的教程。当它工作时,我认为它也会产生这个错误。