“RecyclerView: No Adapter attached; skipping layout” for recyclerview in fragment [duplicate]

僤鯓⒐⒋嵵緔 提交于 2019-12-30 06:07:17

问题


I'm getting this error but I don't know what is causing it...perhaps something to do with the fact that this is being initialized in a fragment and not in the activity itself. Edit: included StatsAdapter code

public class StatsFragment extends Fragment {

    private RecyclerView mRecyclerView;
    private LinearLayoutManager mLinearLayoutManager;
    private RecyclerView.Adapter mAdapter;
    private String[] myDataset = new String[]{"hello", "world", "yolo"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_stats, parent, false);
        mRecyclerView = (RecyclerView) v.findViewById(R.id.cardList);
        mRecyclerView.setHasFixedSize(true);
        mAdapter = new StatsAdapter(myDataset);
        mRecyclerView.setAdapter(mAdapter);
        mLinearLayoutManager = new LinearLayoutManager(getActivity());
        mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        return v;
    }
}



public class StatsAdapter extends RecyclerView.Adapter<StatsAdapter.ViewHolder> {
    private String[] mDataset;

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextView;
        public ViewHolder(View v) {
            super(v);
            mTextView = (TextView) v.findViewById(R.id.title);
        }
    }

    public StatsAdapter(String[] myDataset) {
        mDataset = myDataset;
    }

    @Override
    public StatsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_stats, parent, false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.mTextView.setText(mDataset[position]);
    }

    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}

回答1:


Move your setLayoutManager line before setAdapter




回答2:


Create the adapter and the "non-view" related objects in onCreate. Then use your adapter for the RecyclerView.

Also, clean up your code by initiating the objects in a clear order based on their use and the Activity lifecycle.

public class StatsFragment extends Fragment {

    private RecyclerView mRecyclerView;
    private LinearLayoutManager mLinearLayoutManager;
    private StatsAdapter mAdapter; // was RecyclerView.Adapter mAdapter;
    private String[] myDataset = new String[]{"hello", "world", "yolo"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 1.
        mAdapter = new StatsAdapter(myDataset);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_stats, parent, false);

        // 2.
        mLinearLayoutManager = new LinearLayoutManager(getActivity());
        mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        // 3.
        mRecyclerView = (RecyclerView) v.findViewById(R.id.cardList);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        mRecyclerView.setAdapter(mAdapter);

        return v;
    }
}



回答3:


One way to fix this issue is to attach an empty adapter to the RecyclerView

void initializeRecyclerView() {

   recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
   recyclerView.setAdapter(new SampleAdapter(getCurrentActivity()));
   recyclerView.setLayoutManager(new LinearLayoutManager(getCurrentActivity()));
   recyclerView.setHasFixedSize(true);
}


来源:https://stackoverflow.com/questions/30895580/recyclerview-no-adapter-attached-skipping-layout-for-recyclerview-in-fragmen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!