刚刚在我的代码中实现了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附加了一个适配器。 为什么我总是得到这个错误呢?

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


当前回答

只需将以下内容添加到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并从其他视图中扩展它的人:当扩展整个片段视图时,确保将RecyclerView绑定到它的根视图。

我为适配器正确地连接和做了所有事情,但我从未进行绑定。 @Prateek Agarwal的回答已经为我提供了一切,但这里有更详细的说明。

科特林

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {

    val rootView =  inflater?.inflate(R.layout.fragment_layout, container, false)
    recyclerView = rootView?.findViewById(R.id.recycler_view_id)
    // rest of my stuff
    recyclerView?.setHasFixedSize(true)
    recyclerView?.layoutManager = viewManager
    recyclerView?.adapter = viewAdapter
    // return the root view
    return rootView
}

Java

  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView= inflater.inflate(R.layout.fragment_layout,container,false);
    recyclerview= rootView.findViewById(R.id.recycler_view_id);
    return rootView;
}

在您的RecyclerView适配器类中,例如MyRecyclerViewAdapter,使用以下参数创建一个构造函数。

MyRecyclerViewAdapter(Context context, List<String> data) {
    this.mInflater = LayoutInflater.from(context); // <-- This is the line most people include me miss
    this.mData = data;
}

mData是传递给适配器的数据。如果没有要传递的数据,则该选项是可选的。 mInflater是你创建的LayoutInflater对象,你在适配器的OnCreateViewHolder函数中使用。

在此之后,您可以将适配器附加到MainActivity或主/UI线程上您想要的任何地方

MyRecyclerViewAdapter recyclerAdapter;
OurDataStuff mData;

    ....
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Like this:

        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerAdapter = new RecyclerAdapter(this, mData); //this, is the context. mData is the data you want to pass if you have any
        recyclerView.setAdapter(recyclerAdapter);
    }
   ....
 override fun onCreateView(...  ): View? 
   {
        val rootview = inflater.inflate(R.layout.fragment_menu, 
        container,false)

        val recycler_menu_list = 
         rootview.findViewById(R.id.recycler_menu_list) as RecyclerView

        recycler_menu_list.layoutManager = LinearLayoutManager(activity)

        val data =  ...

        foodCardAdapter = FoodCardAdapter()

        recycler_menu_list.adapter=foodCardAdapter

        foodCardAdapter.submitList(data)      

       //make sure that the return view is one declared above up and is 
       //not the default 'inflater.inflate(R.layout.fragment_menu,         
       //container,false)' created by the ide

        return rootview    
    }

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

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

我有和你一样的情况,显示正常,但是定位器出现错误。 这就是我的解决方案: (1)在CREATE()上初始化RecyclerView并绑定适配器

RecyclerView mRecycler = (RecyclerView) this.findViewById(R.id.yourid);
mRecycler.setAdapter(adapter);

(2)当你得到数据时调用notifyDataStateChanged

adapter.notifyDataStateChanged();

在recyclerView的源代码中,有另一个线程来检查数据的状态。

public RecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    this.mObserver = new RecyclerView.RecyclerViewDataObserver(null);
    this.mRecycler = new RecyclerView.Recycler();
    this.mUpdateChildViewsRunnable = new Runnable() {
        public void run() {
            if(RecyclerView.this.mFirstLayoutComplete) {
                if(RecyclerView.this.mDataSetHasChangedAfterLayout) {
                    TraceCompat.beginSection("RV FullInvalidate");
                    RecyclerView.this.dispatchLayout();
                    TraceCompat.endSection();
                } else if(RecyclerView.this.mAdapterHelper.hasPendingUpdates()) {
                    TraceCompat.beginSection("RV PartialInvalidate");
                    RecyclerView.this.eatRequestLayout();
                    RecyclerView.this.mAdapterHelper.preProcess();
                    if(!RecyclerView.this.mLayoutRequestEaten) {
                        RecyclerView.this.rebindUpdatedViewHolders();
                    }

                    RecyclerView.this.resumeRequestLayout(true);
                    TraceCompat.endSection();
                }

            }
        }
    };

在dispatchLayout()中,我们可以发现其中有错误:

void dispatchLayout() {
    if(this.mAdapter == null) {
        Log.e("RecyclerView", "No adapter attached; skipping layout");
    } else if(this.mLayout == null) {
        Log.e("RecyclerView", "No layout manager attached; skipping layout");
    } else {