recyclerview No adapter attached; skipping layout

后端 未结 30 3088
走了就别回头了
走了就别回头了 2020-11-21 04:51

Just implemented RecyclerView in my code, replacing ListView.

Everything works fine. The data is displayed.

But error messages are

相关标签:
30条回答
  • 2020-11-21 05:17

    Make sure you set the layout manager for your RecyclerView by:

    mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
    

    Instead of LinearLayoutManager, you can use other layout managers too.

    0 讨论(0)
  • 2020-11-21 05:17

    First initialize the adapter

    public void initializeComments(){
        comments = new ArrayList<>();
    
        comments_myRecyclerView = (RecyclerView) findViewById(R.id.comments_recycler);
        comments_mLayoutManager = new LinearLayoutManager(myContext);
        comments_myRecyclerView.setLayoutManager(comments_mLayoutManager);
    
        updateComments();
        getCommentsData();
    
    }
    
    public void updateComments(){
    
        comments_mAdapter = new CommentsAdapter(comments, myContext);
        comments_myRecyclerView.setAdapter(comments_mAdapter);
    }
    

    When ever there is a change in the dataset set, just call the updateComments method.

    0 讨论(0)
  • 2020-11-21 05:19

    This happens because the actual inflated layout is different from that which is being referred by you while finding the recyclerView. By default when you create the fragment, the onCreateView method appears as follows: return inflater.inflate(R.layout.<related layout>,container.false);

    Instead of that, separately create the view and use that to refer to recyclerView View view= inflater.inflate(R.layout.<related layout>,container.false); recyclerview=view.findViewById(R.id.<recyclerView ID>); return view;

    0 讨论(0)
  • 2020-11-21 05:20

    These Lines must be in OnCreate:

    mmAdapter = new Adapter(msgList);
    mrecyclerView.setAdapter(mmAdapter);
    
    0 讨论(0)
  • 2020-11-21 05:22

    For those who use the RecyclerView within a fragment and inflate it from other views: when inflating the whole fragment view, make sure that you bind the RecyclerView to its root view.

    I was connecting and doing everything for the adapter correctly, but I never did the binding. This answer by @Prateek Agarwal has it all for me, but here is more elaboration.

    Kotlin

        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;
    }
    
    0 讨论(0)
  • 2020-11-21 05:22

    Just add the following to RecyclerView

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

    Example:

       <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>
    
    0 讨论(0)
提交回复
热议问题