recyclerview No adapter attached; skipping layout

后端 未结 30 3165
走了就别回头了
走了就别回头了 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: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;
    }
    

提交回复
热议问题