ADK
ADK

Reputation: 513

In custom LayoutManager (extends RecyclerView.LayoutManager) RecyclerView.getChildViewHolderInt NullPointerException issue

After trying similar to this (simplify version):

    @Override
    public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
         ....
         addView(new TextView(mRecyclerView.getContext()))
    }

I have an exception:

    java.lang.NullPointerException
        at android.support.v7.widget.RecyclerView.getChildViewHolderInt(RecyclerView.java:2497)
        at android.support.v7.widget.RecyclerView$LayoutManager.addViewInt(RecyclerView.java:4807)
        at android.support.v7.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:4803)
        at android.support.v7.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:4791)

I can't add to RecyclerView child view without ViewHolder?

Upvotes: 0

Views: 1475

Answers (1)

ADK
ADK

Reputation: 513

New children should never be added to a RecyclerView directly in the layout manager. The views it adds should always be obtained from the attached RecyclerView.Adapter, so that they are properly accounted for and have a valid ViewHolder attached.

Upvotes: 2

Related Questions