recyclerview No adapter attached; skipping layout

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

    1) Create ViewHolder that does nothing :)

    // SampleHolder.java
    public class SampleHolder extends RecyclerView.ViewHolder {
        public SampleHolder(View itemView) {
            super(itemView);
        }
    }
    

    2) Again create RecyclerView that does nothing :)

    // SampleRecycler.java
    public class SampleRecycler extends RecyclerView.Adapter {
        @Override
        public SampleHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return null;
        }
    
        @Override
        public void onBindViewHolder(SampleHolder holder, int position) {
    
        }
    
        @Override
        public int getItemCount() {
            return 0;
        }
    }
    

    3) Now when your real recycler is not ready just use the sample one like below.

    RecyclerView myRecycler = (RecyclerView) findViewById(R.id.recycler_id);
    myRecycler.setLayoutManager(new LinearLayoutManager(this));
    myRecycler.setAdapter(new SampleRecycler());
    

    This is not best solution though but it works! Hope this is helpful.

提交回复
热议问题