[英]E/RecyclerView: No adapter attached; skipping layout Android RecycleView
I'm stuck in E/RecyclerView: No adapter attached; 我被卡在E / RecyclerView中:未连接适配器; Skipping layout error please help me to solve the error.
跳过布局错误,请帮助我解决错误。 I read most of related answers which are already present but error is still present.
我阅读了大多数已经存在的相关答案,但仍然存在错误。 Help me please give me some possible solutions.
帮帮我,请给我一些可能的解决方案。 Thanks in advance.
提前致谢。
One way to fix this issue is to attach an empty adapter to the RecyclerView
: 解决此问题的一种方法是将一个空适配器附加到
RecyclerView
:
void initializeRecyclerView() {
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
recyclerView.setAdapter(new SampleAdapter(getCurrentActivity()));
recyclerView.setLayoutManager(new LinearLayoutManager(getCurrentActivity()));
recyclerView.setHasFixedSize(true);
}
Call this method in onCreateView()
or onCreate()
methods. 在
onCreateView()
或onCreate()
方法中调用此方法。
Are you setting the layout manager for the RecyclerView ? 您是否正在为RecyclerView设置布局管理器?
LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(manager);
recyclerView.setAdapter(adapter);
Make sure you call these methods in the onCreate()
for your activity. 确保在活动的
onCreate()
调用这些方法。
When the Views are attach they go through layout-phase. 附加视图后,它们将进入布局阶段。 Layout event is propagated to the children that are of type
ViewGroup
to start laying out their children too. 布局事件会传播到
ViewGroup
类型的子级,从而也开始布局其子级。 Which means that at that layout phase your RecyclerView
attempts to layout views from the adapter. 这意味着在该布局阶段,您的
RecyclerView
尝试从适配器布局视图。 If the adapter returns item count 0 or it isn't set at all, the layout phase does nothing for your RecyclerView
. 如果适配器返回的项目计数为0或根本没有设置,则布局阶段对
RecyclerView
。 Given this, RecyclerView
will requestLayout()
when the adapter is set and will start laying out views as soon as adapter has been provided. 鉴于此,设置适配器后,
RecyclerView
将requestLayout()
,并在提供适配器后立即开始布局视图。
That message does not contribute towards your application performance and it isn't critical. 该消息不会影响您的应用程序性能,这也不重要。 You can safely ignore it.
您可以放心地忽略它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.